llm_schema library
Zod-style schema validation for LLM structured outputs.
Define a schema once and use it to:
- validate JSON produced by a language model into typed Dart values, with precise, path-aware error messages you can feed back to the model for a retry;
- emit JSON Schema (
toJsonSchema) for tool-parameter definitions and structured-output APIs; - parse raw model text (
parseText) that wraps JSON in Markdown code fences, prose, or trailing commas.
Classes
-
AnyOfSchema<
T> - Matches if any of options matches; the first success wins.
- AnySchema
- Matches any JSON value unchanged.
- BoolSchema
- Matches JSON booleans.
-
DefaultSchema<
T> -
Supplies a default when input is
nullor absent. See Schema.withDefault. -
DescribedSchema<
T> - Attaches a JSON Schema description. See Schema.describe.
- DoubleSchema
- Matches JSON numbers (integers or doubles), producing a double.
-
EnumOfSchema<
T extends Enum> - Matches the names of a Dart enum, producing the enum value itself.
- EnumSchema
- Matches one of a fixed set of strings.
- IntSchema
-
Matches JSON integers. Whole-valued doubles such as
5.0— which some models emit for integer fields — are accepted and truncated toint. -
ListSchema<
T> - Matches JSON arrays whose every element matches items.
-
LiteralSchema<
T> -
Matches exactly one value, compared with
==. -
MapSchema<
V> - Matches JSON objects used as dictionaries: arbitrary string keys, all values matching values.
-
NullableSchema<
T> -
Accepts
nullin addition to the inner schema's values. - ObjectSchema
- Matches JSON objects with a fixed set of named fields.
-
OptionalSchema<
T> - Marks an object field as omittable. See Schema.optional.
-
RefineSchema<
T> - Adds a custom predicate check. See Schema.refine.
- S
-
Entry point for building schemas, in the spirit of TypeScript's
z. -
Schema<
T> - Base class for all schemas.
-
SchemaFailure<
T> - A failed validation carrying every SchemaIssue that was found.
- SchemaIssue
- A single validation problem found while checking input against a schema.
-
SchemaResult<
T> -
The outcome of validating a value against a
Schema<T>. -
SchemaSuccess<
T> - A successful validation carrying the parsed value.
- StringSchema
- Matches JSON strings, with optional length/pattern constraints.
-
TransformSchema<
I, O> - Maps a validated value to another type. See Schema.transform.
Functions
-
extractJson(
String text) → Object? -
Extracts and decodes the first JSON value found in
text.
Exceptions / Errors
- SchemaValidationException
-
Thrown by
Schema.parseandSchema.parseTextwhen validation fails.