ExamplescriptintermediateRunnableschema-validation
Validation: Ensures LLM outputs match predefined data schemas.
This component provides schema validation and structured data parsing to guarantee consistent data formats for downstream code.
Key Facts
- Level
- intermediate • Agent Building Blocks
- Runtime
- Python • OpenAI API
- Pattern
- Structured extraction with explicit acceptance criteria
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
4-validation.py
python
"""
Validation: Ensures LLM outputs match predefined data schemas.
This component provides schema validation and structured data parsing to guarantee consistent data formats for downstream code.
More info: https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses
"""
from openai import OpenAI
from pydantic import BaseModel
class TaskResult(BaseModel):
"""
More info: https://docs.pydantic.dev
"""
task: str
completed: bool
priority: int
def structured_intelligence(prompt: str) -> TaskResult:
client = OpenAI()
response = client.responses.parse(
model="gpt-4o",
input=[
{
"role": "system",
"content": "Extract task information from the user input.",
},
{"role": "user", "content": prompt},
],
text_format=TaskResult,
)
return response.output_parsed
if __name__ == "__main__":
result = structured_intelligence(
"I need to complete the project presentation by Friday, it's high priority"
)
print("Structured Output:")
print(result.model_dump_json(indent=2))
print(f"Extracted task: {result.task}")
Related principles
- P1delegationDesign for delegation rather than direct manipulationDesign experiences around the assignment of work, the expression of intent, the setting of constraints, and the review of results, rather than requiring users to execute each step manually.Open principle →
- P5delegationReplace implied magic with clear mental modelsThe product should help users understand what the system can do, what it is currently doing, what it cannot do, and what conditions govern its behaviour.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P8trustMake hand-offs, approvals, and blockers explicitWhen the system cannot proceed, the reason should be immediately visible, along with any action required from the user or another dependency.Open principle →
- P10delegationOptimise for steering, not only initiatingThe system should support users not only in starting tasks, but also in guiding, refining, reprioritising, and correcting work while it is underway.Open principle →