Structured output — LLM JSON responses to typed models.
Generates JSON schema from dataclass fields or Pydantic models and parses LLM responses back into typed instances.
ai._structured
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Return True if cls is a supported structured-output target.
Generate a JSON schema for a dataclass or Pydantic model.
Generate a JSON schema from a frozen dataclass.
Generate a JSON schema from a PydanticBaseModelsubclass.
Parse an LLM text response into a dataclass or Pydantic model.
Build a follow-up user message asking the model to repair bad JSON.
Parse JSON dict into a Pydantic model instance.
Flatten a Pydantic$defsschema into a single object schema.
Convert a Python type annotation to a JSON schema fragment.
Extract JSON from LLM text, handling markdown code fences.
is_structured_type
function
def is_structured_type(cls: type[Any]) -> bool
Return True if cls is a supported structured-output target.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[Any]
|
— |
schema_for_type
function
def schema_for_type(cls: type[Any]) -> dict[str, Any]
Generate a JSON schema for a dataclass or Pydantic model.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[Any]
|
— |
dataclass_to_schema
function
def dataclass_to_schema(cls: type[T]) -> dict[str, Any]
Generate a JSON schema from a frozen dataclass.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[T]
|
— |
pydantic_to_schema
function
def pydantic_to_schema(cls: type[Any]) -> dict[str, Any]
Generate a JSON schema from a PydanticBaseModelsubclass.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[Any]
|
— |
parse_structured
function
def parse_structured(cls: type[T], text: str) -> T
Parse an LLM text response into a dataclass or Pydantic model.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[T]
|
— | |
text
|
str
|
— |
structured_repair_prompt
function
def structured_repair_prompt(*, error: StructuredOutputError, bad_text: str) -> str
Build a follow-up user message asking the model to repair bad JSON.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
error
|
StructuredOutputError
|
— | |
bad_text
|
str
|
— |
_parse_pydantic_model
function
def _parse_pydantic_model(cls: type[Any], data: dict[str, Any]) -> Any
Parse JSON dict into a Pydantic model instance.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[Any]
|
— | |
data
|
dict[str, Any]
|
— |
_inline_json_schema
function
def _inline_json_schema(schema: dict[str, Any]) -> dict[str, Any]
Flatten a Pydantic$defsschema into a single object schema.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
schema
|
dict[str, Any]
|
— |
_type_to_schema
function
def _type_to_schema(annotation: Any) -> dict[str, Any]
Convert a Python type annotation to a JSON schema fragment.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
annotation
|
Any
|
— |
_extract_json
function
def _extract_json(text: str) -> str
Extract JSON from LLM text, handling markdown code fences.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
text
|
str
|
— |
View source · /home/runner/work/chirp/chirp/site/../src/chirp/ai/_structured.py:1