Structured Output (JSON Schema)
Constrain the agent's final response to a JSON schema by setting output_format. The structured result is available on ResultMessage.structured_output.
Basic example
from agentix import AgentixAgentOptions, AgentixClient, ResultMessage
schema = {
"type": "json_schema",
"json_schema": {
"name": "analysis_result",
"strict": True,
"schema": {
"type": "object",
"properties": {
"summary": {"type": "string"},
"sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]},
"confidence": {"type": "number", "minimum": 0, "maximum": 1},
},
"required": ["summary", "sentiment", "confidence"],
"additionalProperties": False,
},
},
}
options = AgentixAgentOptions(
name="analyst",
provider="openai",
output_format=schema,
)
async def main():
async with AgentixClient(options) as client:
async for msg in client.query("Analyse: 'I love this product!'"):
if isinstance(msg, ResultMessage):
result = msg.structured_output
print(result["sentiment"]) # "positive"
print(result["confidence"]) # e.g. 0.95
Accessing structured output
async for msg in client.query("..."):
if isinstance(msg, ResultMessage):
if msg.structured_output is not None:
# Parsed JSON object matching your schema
data = msg.structured_output
else:
# Fallback plain text
text = msg.result
Effort / thinking
For reasoning-intensive structured extraction, combine with effort:
options = AgentixAgentOptions(
output_format=schema,
effort="high", # maps to 10k thinking budget tokens (Anthropic)
)
Provider support
Structured output is supported on all providers. Implementation details:
- Anthropic — uses tool_use mode internally for constrained generation
- OpenAI — uses the
response_formatparameter withjson_schematype - Gemini — uses
response_schemageneration config - DeepSeek — uses JSON mode