Loading…
Loading…
Features · Build · Structured output
Pass a Pydantic model or a JSON Schema, and the answer arrives in result.parsed. An answer that does not fit is sent back to the model with the error, in the same conversation, until it does.
from pydantic import BaseModelfrom shipit_agent import Agentfrom shipit_agent.llms import build_llm_from_envclass Finding(BaseModel): file: str line: int
Schema
Pydantic or JSON Schema
Model answers
as JSON
Validate
errors go back to the model
result.parsed
a typed object
The code is a checked example from the docs; the output is an example of what it prints.
from pydantic import BaseModel
from shipit_agent import Agent
from shipit_agent.llms import build_llm_from_env
class Finding(BaseModel):
file: str
line: int
problem: str
severity: str
class Review(BaseModel):
summary: str
findings: list[Finding]
agent = Agent.with_builtins(llm=build_llm_from_env(), project_root=".")
result = agent.run("Review src/payments for bugs.", output_schema=Review)
for finding in result.parsed.findings:
print(f"{finding.file}:{finding.line} [{finding.severity}] {finding.problem}")4 more ways to use it
Each example is from the docs and checked against the library.
The agent still uses its tools to do the work; only the final answer has to fit the schema. A JSON Schema dict works the same way, and `parsed` is then a dict.
from pydantic import BaseModel
from shipit_agent import Agent
from shipit_agent.llms import build_llm_from_env
class Finding(BaseModel):
file: str
line: int
problem: str
severity: str
class Review(BaseModel):
summary: str
findings: list[Finding]
agent = Agent.with_builtins(llm=build_llm_from_env(), project_root=".")
result = agent.run("Review src/payments for bugs.", output_schema=Review)
for finding in result.parsed.findings:
print(f"{finding.file}:{finding.line} [{finding.severity}] {finding.problem}")From an agent run
output_schema= on run(); the typed answer is result.parsed.
Self-correcting
Validation errors go back to the model in the same conversation.
Without an agent
Parse and validate a single model call the same way.
Streaming partial JSON
Watch fields fill in while the answer is still being written.