ExamplescriptbeginnerRunnablechat-lab
Quickstart
Runnable example (beginner) for script using pydantic.
Key Facts
- Level
- beginner
- Runtime
- Python • Pydantic
- Pattern
- Single-turn interaction with inspectable output
- 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
quickstart.py
python
from typing import Literal
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic import BaseModel
import nest_asyncio
nest_asyncio.apply() # Needed to run interactive python
# --------------------------------------------------------------
# Use a chat model by name
# --------------------------------------------------------------
agent = Agent(
model="openai:gpt-4o-mini", # Specify the model to use
instructions="Be concise, reply with one sentence.", # Specify the instructions (system prompt)
)
result = agent.run_sync(user_prompt='Where does "hello world" come from?')
print(result.output)
print(type(result))
# --------------------------------------------------------------
# Use a chat model by model object
# --------------------------------------------------------------
model = OpenAIChatModel("gpt-4o-mini") # Initialize the model
agent = Agent(
model,
instructions="Be concise, reply with one sentence.",
model_settings={
"temperature": 0.0, # Add model settings here
},
)
result = agent.run_sync(user_prompt='Where does "hello world" come from?')
print(result.output)
# --------------------------------------------------------------
# Using structured outputs
# --------------------------------------------------------------
class TicketCategory(BaseModel):
category: Literal["general", "order", "billing"]
agent = Agent(
model="openai:gpt-4o-mini",
output_type=TicketCategory,
instructions="Classify the following message into a category",
)
ticket = "I would like to place an order."
result: TicketCategory = agent.run_sync(user_prompt=ticket)
print(result.output)
assert result.output.category == "order"
Related principles
- P4trustApply progressive disclosure to system agencyProvide the minimum information necessary by default, while enabling users to inspect additional detail when confidence, understanding, or intervention is required.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 →
- P9orchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →