docs

Structured Output (SDK)

Pass output_type= to get typed, validated agent responses using Pydantic models or JSON schemas. The output is automatically parsed and the agent retries if validation fails.

With Pydantic models

Python
from pydantic import BaseModel
from ninetrix import Agent

class AnalysisResult(BaseModel):
    score: float
    issues: list[str]
    recommendation: str

agent = Agent(
    name="analyzer",
    provider="anthropic",
    model="claude-sonnet-4-6",
    output_type=AnalysisResult,
)

result = agent.run("Analyze the security of this API endpoint: /users/:id")

# result.output is an AnalysisResult instance (not a string)
print(result.output.score)           # 65.0
print(result.output.issues)          # ["No rate limiting", "Missing auth check"]
print(result.output.recommendation)  # "Add authentication middleware..."

With a JSON schema dict

You can also pass a raw JSON Schema dict if you don't want Pydantic:

Python
agent = Agent(
    name="extractor",
    provider="anthropic",
    model="claude-sonnet-4-6",
    output_type={
        "type": "object",
        "properties": {
            "entities": {"type": "array", "items": {"type": "string"}},
            "sentiment": {"type": "string", "enum": ["positive", "negative", "neutral"]},
        },
        "required": ["entities", "sentiment"],
    },
)

result = agent.run("Apple announced record iPhone sales today")
print(result.output)  # {"entities": ["Apple", "iPhone"], "sentiment": "positive"}

How it works

  1. The schema is appended to the system prompt so the agent knows the expected format
  2. After the agent's final response, the SDK extracts JSON (handles markdown fences)
  3. If output_type is a Pydantic model, the JSON is parsed into a model instance
  4. If validation fails, the error is sent back to the agent as a retry prompt
On this page