baml.json.schema
Lowers a BAML type to ordinary, provider-neutral JSON Schema.
Signature
Lowers a BAML type to ordinary, provider-neutral JSON Schema.
The type-level counterpart to the value conversions above: they move a
value between BAML and json, while this describes the shape a value
of t would take.
Optional fields are omitted from required, maps use schema-valued
additionalProperties, and unions use anyOf. Enums lower to a string
enum of their variant names, uint8array to string, and unknown to
the empty schema {}. A nullable type becomes a ["T", "null"] type list
where it can, and anyOf otherwise. A nullable enum also lists null in
its enum, and a nullable literal uses anyOf, because enum and const
restrict the value whatever type says.
This pass does respect @alias and @skip, where the value conversions
do not — a schema is read by something outside BAML, so it must describe
the wire names. That is also why an enum variant lowers under its alias.
Documentation becomes description, with the text the output format
shows: a class's or a field's @description, then its /// docstring. A
field's description sits beside the $ref when the field is a class. An
enum's description holds its own documentation and one
- <variant>: <documentation> line per documented variant, because JSON
Schema has no description per enum value.
The root type is emitted inline, and every class and named alias beneath it
goes into $defs to be reached by $ref. That is what lets a recursive
graph terminate: a recursive root is emitted both inline and into $defs,
so the $ref back to itself resolves.
Throws
baml.errors.Unsupportedfor a type with no JSON Schema form, and for a class, enum, or alias the reflection context cannot resolve.
Source:<builtin>/baml/ns_json/json.bamlbytes 19654–19751