core.structured_output
Pydantic-validated structured output for LLM responses.
Provides StructuredOutputHandler which converts Pydantic models into
JSON Schema instructions, validates LLM output against the schema, and
supports retry with error feedback on validation failure.
Pydantic-validated structured output for LLM responses.
Provides StructuredOutputHandler which converts Pydantic models into
JSON Schema instructions, validates LLM output against the schema, and
supports retry with error feedback on validation failure.
- class StructuredResult(parsed, raw_response, attempts, parsing_errors=<factory>)[source]
Bases:
objectOutcome of a structured-output validation attempt.
- parsed
Validated Pydantic model instance, or None on failure.
- Type:
pydantic.main.BaseModel | None
- class StructuredOutputHandler(max_retries=2)[source]
Bases:
objectValidates LLM responses against Pydantic models.
- Parameters:
max_retries (int) – Maximum number of re-prompt attempts after validation failure. Total attempts = max_retries + 1.
- build_response_format(model)[source]
Return the Pydantic model class for use as
response_format.LiteLLM’s
completion()acceptstype[BaseModel]directly in theresponse_formatparameter and handles provider-specific schema translation internally. Returning the model class instead of a hand-built dict avoids incompatible schemas across providers.
- build_system_suffix(model)[source]
Generate instruction text describing the expected JSON schema.
Intended to be appended to system instructions so the model knows the exact output shape.
- validate(response, model)[source]
Parse and validate an LLM response against a Pydantic model.
Uses
json_repairfor fault-tolerant JSON parsing, then Pydantic validation for type safety.- Parameters:
- Returns:
StructuredResultwithparsedset on success, orparsing_errorspopulated on failure.- Return type:
- prepare_retry_state(prompt, response_model)[source]
Initialize retry state for a structured output loop.
- process_attempt(response, response_model, prompt, attempt, all_errors, best_result)[source]
Process a single structured output attempt.
- Parameters:
response (str) – The raw LLM response.
response_model (type[BaseModel]) – Pydantic BaseModel subclass.
prompt (str) – The original resolved prompt (for feedback).
attempt (int) – Zero-indexed attempt number.
best_result (StructuredResult | None) – Best result so far.
- Returns:
Tuple of (updated_best_result, next_prompt, updated_errors, should_stop).
- Return type:
- finalize_retry(best_result, all_errors, max_attempts)[source]
Finalize after all retry attempts exhausted.
- Parameters:
best_result (StructuredResult | None) – Last validation result.
max_attempts (int) – Total attempts made.
- Returns:
Final
StructuredResult.- Return type:
Classes
- class StructuredOutputHandler(max_retries=2)[source]
Bases:
objectValidates LLM responses against Pydantic models.
- Parameters:
max_retries (int) – Maximum number of re-prompt attempts after validation failure. Total attempts = max_retries + 1.
- build_response_format(model)[source]
Return the Pydantic model class for use as
response_format.LiteLLM’s
completion()acceptstype[BaseModel]directly in theresponse_formatparameter and handles provider-specific schema translation internally. Returning the model class instead of a hand-built dict avoids incompatible schemas across providers.
- build_system_suffix(model)[source]
Generate instruction text describing the expected JSON schema.
Intended to be appended to system instructions so the model knows the exact output shape.
- validate(response, model)[source]
Parse and validate an LLM response against a Pydantic model.
Uses
json_repairfor fault-tolerant JSON parsing, then Pydantic validation for type safety.- Parameters:
- Returns:
StructuredResultwithparsedset on success, orparsing_errorspopulated on failure.- Return type:
- prepare_retry_state(prompt, response_model)[source]
Initialize retry state for a structured output loop.
- process_attempt(response, response_model, prompt, attempt, all_errors, best_result)[source]
Process a single structured output attempt.
- Parameters:
response (str) – The raw LLM response.
response_model (type[BaseModel]) – Pydantic BaseModel subclass.
prompt (str) – The original resolved prompt (for feedback).
attempt (int) – Zero-indexed attempt number.
best_result (StructuredResult | None) – Best result so far.
- Returns:
Tuple of (updated_best_result, next_prompt, updated_errors, should_stop).
- Return type:
- finalize_retry(best_result, all_errors, max_attempts)[source]
Finalize after all retry attempts exhausted.
- Parameters:
best_result (StructuredResult | None) – Last validation result.
max_attempts (int) – Total attempts made.
- Returns:
Final
StructuredResult.- Return type: