Skip to main contentSkip to footer
ExamplescriptintermediateRunnableguided-flow

Dependencies

Runnable example (intermediate) for script using pydantic.

Key Facts

Level
intermediate
Runtime
Python • Pydantic
Pattern
Inspectable flow with visible system boundaries
Interaction
Live sandbox • Script
Updated
14 March 2026

Navigate this example

High-level flow

How this example moves from input to execution and reviewable output
Dependencies -> Constrain output schema -> Render the visible result -> core concepts

Start

Dependencies

Checkpoint

Constrain output schema

Outcome

Render the visible result

Why this page exists

This example is shown as both real source code and a product-facing interaction pattern so learners can connect implementation, UX, and doctrine without leaving the library.

Visual flowReal sourceSandbox or walkthroughMCP access
How should this example be used in the platform?

Use the sandbox to understand the experience pattern first, then inspect the source to see how the product boundary, model boundary, and doctrine boundary are actually implemented.

UX pattern: Inspectable flow with visible system boundaries
Apply progressive disclosure to system agency
Establish trust through inspectability
Make hand-offs, approvals, and blockers explicit
Source references
Library entry
frameworks-pydantic-ai-3-core-concepts-2-dependencies
Source path
content/example-library/sources/frameworks/pydantic-ai/3-core-concepts/2-dependencies.py
Libraries
pydantic
Runtime requirements
Local repo environment
Related principles
Apply progressive disclosure to system agency, Establish trust through inspectability, Make hand-offs, approvals, and blockers explicit, Represent delegated work as a system, not merely as a conversation

2-dependencies.py

python
from datetime import date
import json
from typing import Literal

import nest_asyncio
from pydantic import BaseModel
from pydantic_ai import Agent, RunContext

nest_asyncio.apply()

# --------------------------------------------------------------
# Without dependencies - limited context
# --------------------------------------------------------------

agent = Agent(
    "openai:gpt-4o-mini",
    instructions="You are a helpful customer service agent.",
)

result = agent.run_sync(user_prompt="What's my account status?")
print(result.output)

# --------------------------------------------------------------
# With dependencies - inject runtime data
# --------------------------------------------------------------


class Customer(BaseModel):
    name: str
    account_id: str
    status: Literal["Active", "Inactive"]


agent = Agent(
    "openai:gpt-4o-mini",
    deps_type=Customer,
    instructions="You are a helpful customer service agent.",
)


@agent.instructions
def add_customer_context(ctx: RunContext[Customer]) -> str:
    customer = ctx.deps
    return f"Customer: {customer.name} (ID: {customer.account_id}, Status: {customer.status})"


@agent.instructions
def add_current_date() -> str:
    return f"Today's date: {date.today()}"


customer = Customer(
    name="Alice",
    account_id="C001",
    status="Active",
)
result = agent.run_sync(user_prompt="What's my account status?", deps=customer)
print(result.output)

# --------------------------------------------------------------
# Different customer, same agent
# --------------------------------------------------------------

another_customer = Customer(
    name="Bob",
    account_id="C002",
    status="Inactive",
)
result = agent.run_sync(user_prompt="What's my account status?", deps=another_customer)
print(result.output)


# --------------------------------------------------------------
# Let's explore the result
# --------------------------------------------------------------

messages = json.loads(result.all_messages_json())
print(json.dumps(messages, indent=2))


# --------------------------------------------------------------
# Why you want type-safe dependencies
# --------------------------------------------------------------

wacky_agent = Agent(
    "openai:gpt-4o-mini",
    instructions="You are a helpful customer service agent.",
)


@wacky_agent.instructions
def add_customer_context(customer: str) -> str:
    return f"Customer: {customer}"


yet_another_customer = "Bob"
result = wacky_agent.run_sync(user_prompt="What's my account status?")
print(result.output)  # Results in generic chatbot error that will go unnoticed
What should the learner inspect in the code?

Look for the exact place where system scope is bounded: schema definitions, prompt framing, runtime configuration, and the call site that turns user intent into a concrete model or workflow action.

Look for output contracts and validation
Look for the exact execution call
Look for what the product could expose to the user
How does the sandbox relate to the source?

The sandbox should make the UX legible: what the user sees, what the system is deciding, and how the result becomes reviewable. The source then shows how that behavior is actually implemented.

Read the implementation summary.
Step through the user and system states.
Inspect the source code with the highlighted doctrine decisions in mind.
SandboxInspectable flow with visible system boundaries
Interaction walkthrough

Use the sandbox to step through the user-visible experience, the system work behind it, and the doctrine choice the example is making.

UX explanation

The sandbox explains what the user should see, what the system is doing, and where control or inspectability must remain explicit.

AI design explanation

The page turns raw source into a product-facing pattern: what the model is allowed to decide, what the product should expose, and where deterministic code or review should take over.

Interaction walkthrough

  1. 1Read the implementation summary.
  2. 2Step through the user and system states.
  3. 3Inspect the source code with the highlighted doctrine decisions in mind.

Visible to the user

A script demonstrating pydantic.

System work

The product prepares a bounded model or workflow task.

Why it matters

The interface should make the delegated task legible before automation happens.

Used in courses and paths

This example currently stands on its own in the library, but it still connects to the principle system and the broader example family.

Related principles

Runtime architecture

Use this example in your agents

This example is also available through the blueprint’s agent-ready layer. Use the For agents page for the public MCP, deterministic exports, and Claude/Cursor setup.

Define triggers, context, and boundaries before increasing autonomy
Make control, observability, and recovery explicit in the runtime
Choose the right operational patterns before delegating to workflows