Skip to main content

Structured output

Pass a Pydantic model as output=. The runtime does its normal work, and its final answer is validated into result.output.

from typing import Literal
from pydantic import BaseModel

class Review(BaseModel):
verdict: Literal["approve", "request_changes"]
issues: list[str]

r = litellm.agent(
Harness.CLAUDE_CODE,
"Review the staged diff.",
output=Review,
permissions="read-only",
sandbox=box,
model="litellm_proxy/coder",
)
r.output.verdict # "request_changes"
HarnessHow the schema is enforced
CLAUDE_CODEinstruction to answer with one JSON object matching the schema, then validated
CODEXCodex's native --output-schema
OPENCODEinstruction to answer with one JSON object matching the schema, then validated
DEEPAGENTScreate_deep_agent(response_format=Review)

If validation fails, the call raises OutputInvalid. The exception carries .raw with the model's text and .result with the rest of the turn, so the work isn't lost.