How to Build AI Agents with PydanticAI (Beginner Tutorial)

Dave EbbelaarAbout 4 min readMay 27, 2025Watch original
THE SUMMARYAI-generated

Pydantic AI Agent Framework: Detailed Summary

Key Concepts:

  • Pydantic AI: A framework for building robust LLM applications with structured input/output validation and dependency injection.
  • Agents: Containers for system prompts, tools, result types, dependencies, and LLM models.
  • Dependencies: Pydantic models used to validate and inject data into system prompts.
  • Results: Structured output from LLMs, validated using Pydantic models.
  • Tools: Functions that agents can use to interact with external systems or data.
  • Model Retry: A mechanism for handling errors and prompting the LLM to self-correct.
  • Tool vs. Solution: Frameworks can be used as a complete solution or as individual tools to integrate into existing systems.

1. Introduction to Pydantic AI

  • Bentch (Pydantic team) released Pydantic AI, an agent framework for building LLM applications.
  • The framework aims to provide low-level abstractions for structured output and input validation.
  • Pydantic AI is model-agnostic, type-safe, and offers control flow and agent composition in pure Python.
  • It features structured responses, a type-safe dependency injection system, and integration with Lockfire (similar to LangSmith/Langfuse).

2. Core Concepts and Philosophy

  • Pydantic AI focuses on agents, dependencies, results, and messages/chat history.
  • The goal is to chain these components together to send data to an LLM (e.g., OpenAI) and receive structured responses.
  • The framework emphasizes validating both input and output data using Pydantic models.
  • The philosophy is to structure inputs and outputs for LLMs, rather than solving all agent system problems.

3. Hello World Example

  • Model Selection: The OpenAI model class is used to specify the LLM (e.g., GPT-4-0125-preview).
  • Agent Creation: An agent is created with a system prompt (e.g., "helpful customer support agent") and the selected model.
  • Agent Execution: The agent can be run synchronously (agent.run_sync), asynchronously (agent.run), or as a stream (agent.run_stream).
  • Result Object: The Result object contains information about the agent run, including data, failures, stream results, costs, and message history.
  • Message History: The message history includes the system prompt, user prompt, and the model's reply.
  • Conversation Continuation: The agent can be run again with the message history to continue the conversation.

4. Structured Output with Pydantic Models

  • A Pydantic model (e.g., ResponseModel) is defined to specify the desired schema for the LLM's output.
  • The agent is configured with the result_type set to the Pydantic model.
  • The LLM's output is validated against the Pydantic model, ensuring structured data.
  • The response.data now contains an instance of the Pydantic model, allowing for easy access to the structured data.
  • Example: The ResponseModel includes fields like response, needs_escalation, followup_required, and sentiment.

5. Dependency Injection

  • Pydantic models are used to define dependencies (e.g., Customer, Order).
  • The agent is configured with the dependency_type set to the Pydantic model.
  • Dependencies are injected into the system prompt using the @agent.add_system_prompt decorator and the RunContext.
  • Dynamic system prompts can be created based on the dependencies.
  • Example: Customer details (ID, name, email, orders) are injected into the system prompt.
  • Validation: Pydantic's validation ensures that the incoming data matches the defined schema, preventing errors.
  • Benefits: Ensures the agent has the correct information and context for each run.

6. Tools

  • Tools are functions that agents can use to interact with external systems or data.
  • Two types of tools: Tool (requires agent context) and ToolPlane (does not require agent context).
  • Tools can be registered using decorators or by providing a list of tools to the agent.
  • Example: A get_shipping_info tool retrieves shipping information from a database based on the order ID.
  • The tool accesses the order ID from the RunContext and the customer details dependency.
  • The agent uses the tool to retrieve the shipping status and provide it to the user.

7. Reflection and Self-Correction with Model Retry

  • The ModelRetry mechanism allows the LLM to self-correct errors.
  • Specific instructions can be provided to the LLM on how to handle common errors.
  • Example: If the shipping status is not found, the ModelRetry is raised with a prompt to include a hashtag in the order ID.
  • The agent retries the request with the updated prompt, allowing the LLM to self-correct.
  • The retries parameter controls the number of retry attempts.
  • ModelRetry can be set at the agent level, tool level, or result validator level.

8. Review and Recommendations

  • Pydantic AI is a promising framework with a focus on data validation and structured output.
  • The dependency injection system is a key feature.
  • The framework is still in early beta, so changes are likely.
  • It's recommended to experiment with Pydantic AI and integrate specific elements into existing systems.
  • Avoid becoming overly dependent on any single framework.
  • Pydantic AI leans more towards being a tool than a complete ecosystem.

9. Conclusion

  • Pydantic AI offers a structured approach to building LLM applications with a strong emphasis on data validation and dependency injection.
  • The framework's low-level abstractions and focus on structured input/output make it a valuable tool for developers building production-ready AI systems.
  • While still in early development, Pydantic AI has the potential to become a major player in the agent framework space.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.