Skip to main content

Module json_schema

Module json_schema 

Source
Expand description

JSON Schema emitter for submilli:llm typed output.

llm.call<T>() sends a JSON Schema for T to the provider so the model is constrained to the shape the program asked for. The schema is advisory — every response is still verified by our own structural check — so the job here is to describe T in the narrow dialect every provider agrees on, and to refuse at compile time anything that dialect cannot carry.

Every definition is inlined; $ref/$defs are never emitted. Google’s provider strips $ref, silently degrading the referenced subschema to “any” — the response then validates against nothing. A recursive type has no finite inlining, so it is a compile error here rather than a ref.

The emitter carries its own gate rather than deferring to unsupported_cast_target_reason. The two agree on most variants but differ where it matters: the cast gate accepts Unknown (the runtime widen is a no-op), while a schema for unknown could only be {} — the same silent degeneration to “any” that $ref is rejected for. See SchemaReject.

Structs§

SchemaReject
A type that has no JSON Schema in the safe subset.

Functions§

json_schema
Emit an inlined JSON Schema for ty, or name the first field that has none.
json_schema_with
Like json_schema but resolves recursion back-edges (Type::AliasRef) one level through expand. A back-edge that expands to a type already open on the walk is a genuine cycle and is rejected.

Type Aliases§

AliasExpander
One-level expander for recursion back-edges, mirroring json_strategy::AliasExpander. Given a Type::AliasRef it returns the alias body (args substituted); for any other type it returns ty.peel() cloned. Lets the emitter resolve a named alias without owning the type namespace.