Skip to main contentSkip to footer
ExamplescriptintermediateRunnabletool-agent

Tools

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
Tools -> User request -> System execution -> Reviewable output -> Apply progressive disclosure to… -> Establish trust through inspectability

Start

Tools

Checkpoint

User request

Outcome

System execution

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-3-tools
Source path
content/example-library/sources/frameworks/pydantic-ai/3-core-concepts/3-tools.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

3-tools.py

python
import json

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

from utils.youtube import extract_video_id

nest_asyncio.apply()


class Transcript(BaseModel):
    video_id: str
    language: str
    text: str
    word_count: int


# --------------------------------------------------------------
# Basic tool - get YouTube transcript with structured output
# --------------------------------------------------------------

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


@agent.tool_plain
def get_transcript(video_url_or_id: str) -> Transcript:
    """
    Get the transcript for a YouTube video using either the video URL or video ID.
    Returns a structured Transcript object with video info and text.

    https://github.com/jdepoix/youtube-transcript-api
    """
    try:
        video_id = extract_video_id(video_url_or_id)
        ytt_api = YouTubeTranscriptApi()
        transcript = ytt_api.fetch(video_id)
        text = " ".join([snippet.text for snippet in transcript])

        return Transcript(
            video_id=video_id,
            language=transcript.language,
            text=text,
            word_count=len(text.split()),
        )
    except Exception as e:
        raise ValueError(f"Could not fetch transcript: {str(e)}")


result = agent.run_sync(
    "What is this video about? https://www.youtube.com/watch?v=dQw4w9WgXcQ"
)
print(result.output)

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

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

# --------------------------------------------------------------
# Tool with dependencies - personalized transcript language
# --------------------------------------------------------------


class UserPreferences(BaseModel):
    name: str
    preferred_language: str


user_agent = Agent(
    "openai:gpt-4o-mini",
    deps_type=UserPreferences,
    instructions="You are a helpful YouTube assistant.",
)


@user_agent.tool
def get_user_transcript(ctx: RunContext[UserPreferences], video_url: str) -> Transcript:
    """
    Get the transcript for a YouTube video in the user's preferred language.
    Returns a structured Transcript object.
    """
    try:
        video_id = extract_video_id(video_url)
        ytt_api = YouTubeTranscriptApi()
        transcript = ytt_api.fetch(video_id, languages=[ctx.deps.preferred_language])
        text = " ".join([snippet.text for snippet in transcript])

        return Transcript(
            video_id=video_id,
            language=transcript.language,
            text=text,
            word_count=len(text.split()),
        )
    except Exception as e:
        raise ValueError(f"Could not fetch transcript: {str(e)}")


@user_agent.instructions
def add_user_context(ctx: RunContext[UserPreferences]) -> str:
    return f"User: {ctx.deps.name}, Preferred language: {ctx.deps.preferred_language}"


user_prefs = UserPreferences(name="Alice", preferred_language="es")
result = user_agent.run_sync(
    "What is this video about? https://www.youtube.com/watch?v=4JDu69Jy41Y",
    deps=user_prefs,
)
print(result.output)


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

# --------------------------------------------------------------
# Multi-turn tool calls - agent orchestrates multiple tools
# --------------------------------------------------------------

multi_agent = Agent(
    "openai:gpt-4o",  # Use a more powerful model for multi-turn tool calls
    instructions="You are a video content analyzer. Use tools to analyze videos.",
)


@multi_agent.tool_plain
def fetch_transcript(video_url: str) -> Transcript:
    """Fetch the transcript of a YouTube video."""
    video_id = extract_video_id(video_url)
    ytt_api = YouTubeTranscriptApi()
    transcript = ytt_api.fetch(video_id)
    text = " ".join([snippet.text for snippet in transcript])

    return Transcript(
        video_id=video_id,
        language=transcript.language,
        text=text,
        word_count=len(text.split()),
    )


@multi_agent.tool_plain
def count_keyword(text: str, keyword: str) -> int:
    """Count how many times a keyword appears in text (case-insensitive)."""
    return text.lower().count(keyword.lower())


result = multi_agent.run_sync(
    "Get the transcript for https://www.youtube.com/watch?v=dQw4w9WgXcQ and tell me how many times the word 'never' appears"
)
print("\n--- Multi-turn Result ---")
print(result.output)

print("\n--- Tool Calls Made ---")
messages = result.all_messages()
for msg in messages:
    if msg.kind == "response":
        for part in msg.parts:
            if part.part_kind == "tool-call":
                print(f"  • {part.tool_name}()")


# --------------------------------------------------------------
# Validation of tool calls
# --------------------------------------------------------------

transcript = fetch_transcript("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
print(transcript)

count = count_keyword(transcript.text, "never")
print(count)
assert count == 40
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.

User request

I was charged twice on Feb 1st for my subscription. Please fix this.

Allowed tools onlyAgent chooses orderStructured resolution

Tool trace

The trace appears as the agent decides which tool to call next.

Resolution

The final output should summarize what the agent did, not leave the action implicit.

Autonomy boundary

  • Apply progressive disclosure to system agency
  • Establish trust through inspectability
  • Make hand-offs, approvals, and blockers explicit
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