Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Agent Framework / shim to use Pydantic with LLMs

License

NotificationsYou must be signed in to change notification settings

pydantic/pydantic-ai

 
 

Repository files navigation

Pydantic AI

LLM Agent Framework, the Pydantic way

CICoveragePyPIversionslicenseJoin Slack

Documentation:ai.pydantic.dev


Pydantic AI is a Python agent framework designed to help you quickly, confidently, and painlessly build production grade applications and workflows with Generative AI.

FastAPI revolutionized web development by offering an innovative and ergonomic design, built on the foundation ofPydantic and modern Python features like type hints.

Yet despite the fact that virtually every agent framework and LLM library in Python uses Pydantic, when we began to use LLMs inPydantic Logfire, we couldn't find anything that gave us the same feeling.

We built Pydantic AI with one simple aim: to bring that FastAPI feeling to GenAI app and agent development.

Why use Pydantic AI

  1. Built by the Pydantic Team:Pydantic is the validation layer of the OpenAI SDK, the Google ADK, the Anthropic SDK, LangChain, LlamaIndex, AutoGPT, Transformers, CrewAI, Instructor and many more.Why use the derivative when you can go straight to the source? 😃

  2. Model-agnostic:Supports virtually everymodel and provider under the sun: OpenAI, Anthropic, Gemini, DeepSeek, Ollama, Grok, Cohere, and Mistral; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Groq, Together AI, Fireworks AI, OpenRouter, and Heroku. If your favorite model or provider is not listed, you can easily implement acustom model.

  3. Seamless Observability:Tightlyintegrates withPydantic Logfire, our general-purpose OpenTelemetry observability platform, for real-time debugging, evals-based performance monitoring, and behavior and cost tracking. If you already have an observability platform that supports OTel, you can use that too.

  4. Fully Type-safe:Designed to give your IDE or AI coding agent as much context as possible for auto-completion andtype checking, moving entire classes of errors from runtime to write-time for a bit of that Rust "if it compiles, it works" feel.

  5. Powerful Evals:Enables you to systematically test andevaluate the performance and accuracy of the agentic systems you build, and monitor the performance over time in Pydantic Logfire.

  6. MCP and A2A:Integrates theModel Context Protocol andAgent2Agent standards to give your agent access to external tools and data and let it interoperate with other agents.

  7. Multi-Modal Input:Lets you easily share images, documents, videos and audioinput with the LLM to go beyond the limitations of text.

  8. Streamed Outputs:Provides the ability tostream structured output continuously, with immediate validation, ensuring real time access to generated data.

  9. Dependency Injection:Offers an optionaldependency injection system to provide data and services to your agent'sinstructions,tools andoutput functions.

  10. Graph Support:Provides a powerful way to definegraphs using type hints, for use in complex applications where standard control flow can degrade to spaghetti code.

Realistically though, no list is going to be as convincing asgiving it a try and seeing how it makes you feel!

Hello World Example

Here's a minimal example of Pydantic AI:

frompydantic_aiimportAgent# Define a very simple agent including the model to use, you can also set the model when running the agent.agent=Agent('anthropic:claude-sonnet-4-0',# Register static instructions using a keyword argument to the agent.# For more complex dynamically-generated instructions, see the example below.instructions='Be concise, reply with one sentence.',)# Run the agent synchronously, conducting a conversation with the LLM.result=agent.run_sync('Where does "hello world" come from?')print(result.output)"""The first known use of "hello, world" was in a 1974 textbook about the C programming language."""

(This example is complete, it can be run "as is", assuming you'veinstalled thepydantic_ai package)

The exchange will be very short: Pydantic AI will send the instructions and the user prompt to the LLM, and the model will return a text response.

Not very interesting yet, but we can easily addtools,dynamic instructions, andstructured outputs to build more powerful agents.

Tools & Dependency Injection Example

Here is a concise example using Pydantic AI to build a support agent for a bank:

(Better documented examplein the docs)

fromdataclassesimportdataclassfrompydanticimportBaseModel,Fieldfrompydantic_aiimportAgent,RunContextfrombank_databaseimportDatabaseConn# SupportDependencies is used to pass data, connections, and logic into the model that will be needed when running# instructions and tool functions. Dependency injection provides a type-safe way to customise the behavior of your agents.@dataclassclassSupportDependencies:customer_id:intdb:DatabaseConn# This Pydantic model defines the structure of the output returned by the agent.classSupportOutput(BaseModel):support_advice:str"""Advice returned to the customer"""# (14)!block_card:bool"""Whether to block the customer's card"""risk:int=Field(ge=0,le=10)"""Risk level of query"""# The docstrings of fields on a Pydantic model are passed to the LLM,# so that it has all the context needed to generate a value.# This agent will act as first-tier support in a bank.# Agents are generic in the type of dependencies they accept and the type of output they return.# In this case, the support agent has type `Agent[SupportDependencies, SupportOutput]`.support_agent=Agent('openai:gpt-4o',deps_type=SupportDependencies,# The response from the agent will, be guaranteed to be a SupportOutput,# if validation fails the agent is prompted to try again.output_type=SupportOutput,instructions=('You are a support agent in our bank, give the ''customer support and judge the risk level of their query.'    ),)# Dynamic instructions can make use of dependency injection.# Dependencies are carried via the `RunContext` argument, which is parameterized with the `deps_type` from above.# If the type annotation here is wrong, static type checkers will catch it.@support_agent.instructionsasyncdefadd_customer_name(ctx:RunContext[SupportDependencies])->str:customer_name=awaitctx.deps.db.customer_name(id=ctx.deps.customer_id)returnf"The customer's name is{customer_name!r}"# The `tool` decorator let you register functions which the LLM may call while responding to a user.# Again, dependencies are carried via `RunContext`, any other arguments become the tool schema passed to the LLM.# Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.@support_agent.toolasyncdefcustomer_balance(ctx:RunContext[SupportDependencies],include_pending:bool)->float:"""Returns the customer's current account balance."""# The docstring of a tool is also passed to the LLM as the description of the tool.# Parameter descriptions are extracted from the docstring and added to the parameter schema sent to the LLM.balance=awaitctx.deps.db.customer_balance(id=ctx.deps.customer_id,include_pending=include_pending,    )returnbalance...# In a real use case, you'd add more tools and a longer system promptasyncdefmain():deps=SupportDependencies(customer_id=123,db=DatabaseConn())# Run the agent asynchronously, conducting a conversation with the LLM until a final response is reached.# Even in this fairly simple case, the agent will exchange multiple messages with the LLM as tools are called to retrieve an output.result=awaitsupport_agent.run('What is my balance?',deps=deps)# The `result.output` will be validated with Pydantic to guarantee it is a `SupportOutput`. Since the agent is generic,# it'll also be typed as a `SupportOutput` to aid with static type checking.print(result.output)"""    support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1    """result=awaitsupport_agent.run('I just lost my card!',deps=deps)print(result.output)"""    support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8    """

Next Steps

To try Pydantic AI for yourself,install it and follow the instructionsin the examples.

Read thedocs to learn more about building applications with Pydantic AI.

Read theAPI Reference to understand Pydantic AI's interface.

JoinSlack or file an issue onGitHub if you have any questions.


[8]ページ先頭

©2009-2025 Movatter.jp