aix.structured¶
Structured output: a schema in, validated values out, enforced as far as the provider allows.
A response schema is a JSON Schema dict or a Pydantic model class. It is
turned into a ResponseSchema, which knows
how to ask the provider for it – the strongest mode the model supports:
"strict":response_format={"type": "json_schema", ..., "strict": true}, the provider constrains decoding to the schema. Used when LiteLLM reportssupports_response_schema(model)and the schema is strict-compatible (every object closesadditionalPropertiesand requires all its properties – Pydantic models are converted to that form automatically);"schema": the samejson_schemaformat, non-strict, for a schema-capable model and a schema that is not strict-compatible;"json_mode":{"type": "json_object"}plus the schema in the prompt, for models without schema support (or whose support is unknown);"prompt": the schema in the prompt only, for models LiteLLM knows do not accept aresponse_formatat all;
and how to validate a reply client-side, which happens on every path (it is cheap, and “the provider enforced it” is a claim worth checking).
Providers only accept an object at the root of a structured reply (strict
json_schema and JSON mode alike), so a schema whose root is not an object
(an array, a union, a scalar) is sent wrapped as {"value": <schema>} and
unwrapped after validation – transparently to the caller.
>>> spec = ResponseSchema.from_schema({"type": "object", "properties": {"x": {"type": "integer"}}, "required": ["x"]})
>>> spec.validate('{"x": 3}')
{'x': 3}
>>> spec.validate('{"x": "three"}')
Traceback (most recent call last):
...
aix.structured.SchemaValidationError: ...
Classes
|
A normalized response schema: its JSON Schema, name, and validator. |
Exceptions
|
A reply was not JSON, or did not match the response schema. |
- class aix.structured.ResponseSchema(name, json_schema, wire_schema, strict_compatible, source, wrapped=False, _coerce_json=None)[source]¶
Bases:
objectA normalized response schema: its JSON Schema, name, and validator.
Build it with
from_schema();aix.complete()does that for you fromresponse_schema=.- wire_schema¶
What is sent:
json_schemaitself, or wrapped as{"value": json_schema}when its root is not an object.- Type:
- classmethod from_schema(schema)[source]¶
Normalize a JSON Schema dict or a Pydantic model class.
- Raises:
TypeError – For anything else.
jsonschema.SchemaError – For a malformed JSON Schema dict.
- Return type:
- instruction()[source]¶
The prompt text that asks for this schema (JSON / prompt modes, repairs).
- Return type:
- mode_for(model)[source]¶
The strongest enforcement mode available for
modeland this schema.- Return type:
- validate(text)[source]¶
Parse and validate a reply; return the value (a model instance for Pydantic).
- Raises:
SchemaValidationError – If
textis not JSON or does not match.- Return type:
- exception aix.structured.SchemaValidationError(message, *, text='', detail='')[source]¶
Bases:
ValueErrorA reply was not JSON, or did not match the response schema.
- text¶
The reply that failed.
- detail¶
The parse / validation message (what a repair prompt is told).