Pydantic AI tutorial: typed agents, tools and MCP setup
Learn Pydantic AI setup, structured output and tool use, with a SQLite dependency example and an MCP server at localhost port 8000.
Player not loading? Watch on YouTube
This Python crash course introduces Pydantic AI through working agent examples. The speaker describes its focus as validation, type safety and structured output, comparing its style with FastAPI. He contrasts that focus with LangChain's broader framework approach and LlamaIndex's emphasis on retrieval.
Setup uses uv, with pip as an alternative, and JupyterLab for interactive examples. Ordinary Python files are also an option. The examples use OpenAI models and require a provider API key loaded from an environment file; this is not a walkthrough to run models locally.
The tutorial starts with an asynchronous agent call, then defines a Pydantic model for a person's name, age and job. It covers streaming text chunks, passing message history, and the different behavior of instructions and system prompts when sharing history between agents.
Tool examples progress from a favorite-color lookup to dependency injection with a username and SQLite connection. Later sections cover toolsets, timeouts and retries. The web-search example returns a video the speaker identifies as incorrect, while the code-execution example checks 24 factorial against Python's math module. The course ends with embeddings and an AI agent that accesses a local MCP server through streamable HTTP at localhost:8000/mcp.