Parsers¶
Parsers validate and convert LLM responses back into structured objects.
Auto-detection¶
Use auto_parser_for_type to automatically select the right parser:
This automatically selects the best parser based on your model structure:
- For regular Pydantic
BaseModelclasses, it uses JSON parser - For
pydantic-xmlBaseXmlModelclasses, it uses XML parser - For plain
strtypes, it returns the response as-is
Available parsers¶
JSON¶
from sintezi.ai.parser import JsonParser, JsonResponseFormat
parser = JsonParser(model_type=MyResponseModel, response_format=JsonResponseFormat.json_schema)
Works with OpenAI's response_format for structured outputs.
XML¶
Requires pydantic-xml.
Plain text¶
For simple string responses.
Validation¶
All parsers automatically validate responses against your Pydantic schema. If validation fails, a ValueError is raised and the retry policy handles it.
Custom parsers¶
Implement ResponseParser[T]:
from sintezi.ai.parser import ResponseParser, ResponseFormatCapability
from typing import TypeVar
T = TypeVar("T")
class MyCustomParser(ResponseParser[str]):
@property
def returns(self) -> type[str]:
return str
def validate(self, content: str) -> str:
return content.strip()
def to_response_format(self, capabilities: set[ResponseFormatCapability]) -> dict | None:
return None
Next steps¶
- Formatters — input formatting
- Retry policies — handling validation failures