schemarail is a schema-first guardrail layer that turns messy model responses into validated TypeScript-safe objects.
It is designed for the practical path most teams need:
- keep your existing schema (Zod/Valibot-like
safeParseorparse) - recover JSON from noisy model output
- optionally run a single repair pass
- ship typed objects into production code with clear failure modes
LLMs are great at content, but not always at output discipline.
In real apps, response shapes drift. You get wrappers, markdown fences, comments, trailing commas, or partial JSON. schemarail gives you a deterministic layer between model text and app logic.
pnpm add @jamiojala/schemarailimport { z } from "zod";
import { createRail } from "@jamiojala/schemarail";
const SummarySchema = z.object({
title: z.string(),
sentiment: z.enum(["positive", "neutral", "negative"]),
confidence: z.number().min(0).max(1)
});
const rail = createRail({ schema: SummarySchema });
const output = await rail.safeParse(`
Here is your result:
\`\`\`json
{
"title": "Quarterly revenue update",
"sentiment": "positive",
"confidence": 0.92
}
\`\`\`
`);
if (!output.ok) {
console.error(output.issues);
return;
}
// output.data is strongly typed
console.log(output.data.confidence);For string inputs, schemarail tries deterministic candidates in this order:
- direct JSON parse
- fenced JSON block extraction
- balanced object/array extraction from surrounding text
- sanitized pass (BOM, comments, smart quotes, trailing commas)
Then each candidate is validated by your schema.
When built-in extraction still fails, attach a repair handler.
import { z } from "zod";
import { buildRepairPrompt, createRail } from "@jamiojala/schemarail";
const EventSchema = z.object({
type: z.string(),
timestamp: z.string()
});
const rail = createRail({
schema: EventSchema,
repair: async ({ current, issues }) => {
const raw = typeof current === "string" ? current : JSON.stringify(current);
const prompt = buildRepairPrompt({
raw,
issues,
schemaHint: '{ type: string; timestamp: string }'
});
// call your model and return raw JSON string or object
return callRepairModel(prompt);
}
});createRail({ schema, repair?, maxRepairAttempts? })rail.safeParse(input, options?)rail.parse(input, options?)buildRepairPrompt(...)formatIssuesForPrompt(...)
- tiny public API
- deterministic defaults
- strong TypeScript inference
- clean drop-in adoption path
- production-minded docs and tests
pnpm install
pnpm checkMIT