pydantic-ai 1.0.0b1__py3-none-any.whl → 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pydantic-ai might be problematic. Click here for more details.
- README.md +47 -40
- {pydantic_ai-1.0.0b1.dist-info → pydantic_ai-1.0.2.dist-info}/METADATA +53 -44
- pydantic_ai-1.0.2.dist-info/RECORD +6 -0
- pydantic_ai-1.0.0b1.dist-info/RECORD +0 -6
- {pydantic_ai-1.0.0b1.dist-info → pydantic_ai-1.0.2.dist-info}/WHEEL +0 -0
- {pydantic_ai-1.0.0b1.dist-info → pydantic_ai-1.0.2.dist-info}/entry_points.txt +0 -0
- {pydantic_ai-1.0.0b1.dist-info → pydantic_ai-1.0.2.dist-info}/licenses/LICENSE +0 -0
README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
</a>
|
|
8
8
|
</div>
|
|
9
9
|
<div align="center">
|
|
10
|
-
<
|
|
10
|
+
<h3>GenAI Agent Framework, the Pydantic way</h3>
|
|
11
11
|
</div>
|
|
12
12
|
<div align="center">
|
|
13
13
|
<a href="https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push" alt="CI"></a>
|
|
@@ -24,43 +24,48 @@
|
|
|
24
24
|
|
|
25
25
|
---
|
|
26
26
|
|
|
27
|
-
Pydantic AI is a Python agent framework designed to
|
|
27
|
+
### <em>Pydantic AI is a Python agent framework designed to help you quickly, confidently, and painlessly build production grade applications and workflows with Generative AI.</em>
|
|
28
28
|
|
|
29
|
-
FastAPI revolutionized web development by offering an innovative and ergonomic design, built on the foundation of [Pydantic Validation](https://docs.pydantic.dev).
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
FastAPI revolutionized web development by offering an innovative and ergonomic design, built on the foundation of [Pydantic Validation](https://docs.pydantic.dev) and modern Python features like type hints.
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
Yet despite virtually every Python agent framework and LLM library using Pydantic Validation, when we began to use LLMs in [Pydantic Logfire](https://pydantic.dev/logfire), we couldn't find anything that gave us the same feeling.
|
|
33
|
+
|
|
34
|
+
We built Pydantic AI with one simple aim: to bring that FastAPI feeling to GenAI app and agent development.
|
|
34
35
|
|
|
35
36
|
## Why use Pydantic AI
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
1. **Built by the Pydantic Team**:
|
|
39
|
+
[Pydantic Validation](https://docs.pydantic.dev/latest/) 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?_ :smiley:
|
|
40
|
+
|
|
41
|
+
2. **Model-agnostic**:
|
|
42
|
+
Supports virtually every [model](https://ai.pydantic.dev/models/overview) and provider: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Cohere, Mistral, and Perplexity; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Ollama, LiteLLM, Groq, OpenRouter, Together AI, Fireworks AI, Cerebras, Hugging Face, GitHub, Heroku, Vercel. If your favorite model or provider is not listed, you can easily implement a [custom model](https://ai.pydantic.dev/models/overview#custom-models).
|
|
43
|
+
|
|
44
|
+
3. **Seamless Observability**:
|
|
45
|
+
Tightly [integrates](https://ai.pydantic.dev/logfire) with [Pydantic Logfire](https://pydantic.dev/logfire), our general-purpose OpenTelemetry observability platform, for real-time debugging, evals-based performance monitoring, and behavior, tracing, and cost tracking. If you already have an observability platform that supports OTel, you can [use that too](https://ai.pydantic.dev/logfire#alternative-observability-backends).
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
4. **Fully Type-safe**:
|
|
48
|
+
Designed to give your IDE or AI coding agent as much context as possible for auto-completion and [type checking](https://ai.pydantic.dev/agents#static-type-checking), moving entire classes of errors from runtime to write-time for a bit of that Rust "if it compiles, it works" feel.
|
|
42
49
|
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
5. **Powerful Evals**:
|
|
51
|
+
Enables you to systematically test and [evaluate](https://ai.pydantic.dev/evals) the performance and accuracy of the agentic systems you build, and monitor the performance over time in Pydantic Logfire.
|
|
45
52
|
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
6. **MCP, A2A, and AG-UI**:
|
|
54
|
+
Integrates the [Model Context Protocol](https://ai.pydantic.dev/mcp/client), [Agent2Agent](https://ai.pydantic.dev/a2a), and [AG-UI](https://ai.pydantic.dev/ag-ui) standards to give your agent access to external tools and data, let it interoperate with other agents, and build interactive applications with streaming event-based communication.
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
7. **Human-in-the-Loop Tool Approval**:
|
|
57
|
+
Easily lets you flag that certain tool calls [require approval](https://ai.pydantic.dev/deferred-tools#human-in-the-loop-tool-approval) before they can proceed, possibly depending on tool call arguments, conversation history, or user preferences.
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
8. **Durable Execution**:
|
|
60
|
+
Enables you to build [durable agents](https://ai.pydantic.dev/temporal) that can preserve their progress across transient API failures and application errors or restarts, and handle long-running, asynchronous, and human-in-the-loop workflows with production-grade reliability.
|
|
54
61
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
This is useful for testing and eval-driven iterative development.
|
|
62
|
+
9. **Streamed Outputs**:
|
|
63
|
+
Provides the ability to [stream](https://ai.pydantic.dev/output#streamed-results) structured output continuously, with immediate validation, ensuring real time access to generated data.
|
|
58
64
|
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
10. **Graph Support**:
|
|
66
|
+
Provides a powerful way to define [graphs](https://ai.pydantic.dev/graph) using type hints, for use in complex applications where standard control flow can degrade to spaghetti code.
|
|
61
67
|
|
|
62
|
-
-
|
|
63
|
-
[Pydantic Graph](https://ai.pydantic.dev/graph) provides a powerful way to define graphs using typing hints, this is useful in complex applications where standard control flow can degrade to spaghetti code.
|
|
68
|
+
Realistically though, no list is going to be as convincing as [giving it a try](#next-steps) and seeing how it makes you feel!
|
|
64
69
|
|
|
65
70
|
## Hello World Example
|
|
66
71
|
|
|
@@ -71,15 +76,13 @@ from pydantic_ai import Agent
|
|
|
71
76
|
|
|
72
77
|
# Define a very simple agent including the model to use, you can also set the model when running the agent.
|
|
73
78
|
agent = Agent(
|
|
74
|
-
'
|
|
75
|
-
# Register
|
|
76
|
-
# For more complex dynamically-generated
|
|
77
|
-
|
|
79
|
+
'anthropic:claude-sonnet-4-0',
|
|
80
|
+
# Register static instructions using a keyword argument to the agent.
|
|
81
|
+
# For more complex dynamically-generated instructions, see the example below.
|
|
82
|
+
instructions='Be concise, reply with one sentence.',
|
|
78
83
|
)
|
|
79
84
|
|
|
80
85
|
# Run the agent synchronously, conducting a conversation with the LLM.
|
|
81
|
-
# Here the exchange should be very short: Pydantic AI will send the system prompt and the user query to the LLM,
|
|
82
|
-
# the model will return a text response. See below for a more complex run.
|
|
83
86
|
result = agent.run_sync('Where does "hello world" come from?')
|
|
84
87
|
print(result.output)
|
|
85
88
|
"""
|
|
@@ -87,9 +90,11 @@ The first known use of "hello, world" was in a 1974 textbook about the C program
|
|
|
87
90
|
"""
|
|
88
91
|
```
|
|
89
92
|
|
|
90
|
-
_(This example is complete, it can be run "as is")_
|
|
93
|
+
_(This example is complete, it can be run "as is", assuming you've [installed the `pydantic_ai` package](https://ai.pydantic.dev/install))_
|
|
91
94
|
|
|
92
|
-
|
|
95
|
+
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.
|
|
96
|
+
|
|
97
|
+
Not very interesting yet, but we can easily add [tools](https://ai.pydantic.dev/tools), [dynamic instructions](https://ai.pydantic.dev/agents#instructions), and [structured outputs](https://ai.pydantic.dev/output) to build more powerful agents.
|
|
93
98
|
|
|
94
99
|
## Tools & Dependency Injection Example
|
|
95
100
|
|
|
@@ -107,14 +112,14 @@ from bank_database import DatabaseConn
|
|
|
107
112
|
|
|
108
113
|
|
|
109
114
|
# SupportDependencies is used to pass data, connections, and logic into the model that will be needed when running
|
|
110
|
-
#
|
|
115
|
+
# instructions and tool functions. Dependency injection provides a type-safe way to customise the behavior of your agents.
|
|
111
116
|
@dataclass
|
|
112
117
|
class SupportDependencies:
|
|
113
118
|
customer_id: int
|
|
114
119
|
db: DatabaseConn
|
|
115
120
|
|
|
116
121
|
|
|
117
|
-
# This
|
|
122
|
+
# This Pydantic model defines the structure of the output returned by the agent.
|
|
118
123
|
class SupportOutput(BaseModel):
|
|
119
124
|
support_advice: str = Field(description='Advice returned to the customer')
|
|
120
125
|
block_card: bool = Field(description="Whether to block the customer's card")
|
|
@@ -125,28 +130,28 @@ class SupportOutput(BaseModel):
|
|
|
125
130
|
# Agents are generic in the type of dependencies they accept and the type of output they return.
|
|
126
131
|
# In this case, the support agent has type `Agent[SupportDependencies, SupportOutput]`.
|
|
127
132
|
support_agent = Agent(
|
|
128
|
-
'openai:gpt-
|
|
133
|
+
'openai:gpt-5',
|
|
129
134
|
deps_type=SupportDependencies,
|
|
130
135
|
# The response from the agent will, be guaranteed to be a SupportOutput,
|
|
131
136
|
# if validation fails the agent is prompted to try again.
|
|
132
137
|
output_type=SupportOutput,
|
|
133
|
-
|
|
138
|
+
instructions=(
|
|
134
139
|
'You are a support agent in our bank, give the '
|
|
135
140
|
'customer support and judge the risk level of their query.'
|
|
136
141
|
),
|
|
137
142
|
)
|
|
138
143
|
|
|
139
144
|
|
|
140
|
-
# Dynamic
|
|
145
|
+
# Dynamic instructions can make use of dependency injection.
|
|
141
146
|
# Dependencies are carried via the `RunContext` argument, which is parameterized with the `deps_type` from above.
|
|
142
147
|
# If the type annotation here is wrong, static type checkers will catch it.
|
|
143
|
-
@support_agent.
|
|
148
|
+
@support_agent.instructions
|
|
144
149
|
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
|
|
145
150
|
customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id)
|
|
146
151
|
return f"The customer's name is {customer_name!r}"
|
|
147
152
|
|
|
148
153
|
|
|
149
|
-
# `tool` let you register functions which the LLM may call while responding to a user.
|
|
154
|
+
# The `tool` decorator let you register functions which the LLM may call while responding to a user.
|
|
150
155
|
# Again, dependencies are carried via `RunContext`, any other arguments become the tool schema passed to the LLM.
|
|
151
156
|
# Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
|
|
152
157
|
@support_agent.tool
|
|
@@ -187,8 +192,10 @@ async def main():
|
|
|
187
192
|
|
|
188
193
|
## Next Steps
|
|
189
194
|
|
|
190
|
-
To try Pydantic AI yourself, follow the instructions [in the examples](https://ai.pydantic.dev/examples/).
|
|
195
|
+
To try Pydantic AI for yourself, [install it](https://ai.pydantic.dev/install) and follow the instructions [in the examples](https://ai.pydantic.dev/examples/setup).
|
|
191
196
|
|
|
192
197
|
Read the [docs](https://ai.pydantic.dev/agents/) to learn more about building applications with Pydantic AI.
|
|
193
198
|
|
|
194
199
|
Read the [API Reference](https://ai.pydantic.dev/api/agent/) to understand Pydantic AI's interface.
|
|
200
|
+
|
|
201
|
+
Join [Slack](https://logfire.pydantic.dev/docs/join-slack/) or file an issue on [GitHub](https://github.com/pydantic/pydantic-ai/issues) if you have any questions.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pydantic-ai
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: Agent Framework / shim to use Pydantic with LLMs
|
|
5
5
|
Project-URL: Homepage, https://ai.pydantic.dev
|
|
6
6
|
Project-URL: Source, https://github.com/pydantic/pydantic-ai
|
|
@@ -9,7 +9,7 @@ Project-URL: Changelog, https://github.com/pydantic/pydantic-ai/releases
|
|
|
9
9
|
Author-email: Samuel Colvin <samuel@pydantic.dev>, Marcelo Trylesinski <marcelotryle@gmail.com>, David Montague <david@pydantic.dev>, Alex Hall <alex@pydantic.dev>, Douwe Maan <douwe@pydantic.dev>
|
|
10
10
|
License-Expression: MIT
|
|
11
11
|
License-File: LICENSE
|
|
12
|
-
Classifier: Development Status ::
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
13
|
Classifier: Framework :: Pydantic
|
|
14
14
|
Classifier: Framework :: Pydantic :: 2
|
|
15
15
|
Classifier: Intended Audience :: Developers
|
|
@@ -27,11 +27,13 @@ Classifier: Topic :: Internet
|
|
|
27
27
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
28
28
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
29
|
Requires-Python: >=3.10
|
|
30
|
-
Requires-Dist: pydantic-ai-slim[ag-ui,anthropic,bedrock,cli,cohere,evals,google,groq,huggingface,logfire,mcp,mistral,openai,retries,temporal,vertexai]==1.0.
|
|
30
|
+
Requires-Dist: pydantic-ai-slim[ag-ui,anthropic,bedrock,cli,cohere,evals,google,groq,huggingface,logfire,mcp,mistral,openai,retries,temporal,vertexai]==1.0.2
|
|
31
31
|
Provides-Extra: a2a
|
|
32
32
|
Requires-Dist: fasta2a>=0.4.1; extra == 'a2a'
|
|
33
|
+
Provides-Extra: dbos
|
|
34
|
+
Requires-Dist: pydantic-ai-slim[dbos]==1.0.2; extra == 'dbos'
|
|
33
35
|
Provides-Extra: examples
|
|
34
|
-
Requires-Dist: pydantic-ai-examples==1.0.
|
|
36
|
+
Requires-Dist: pydantic-ai-examples==1.0.2; extra == 'examples'
|
|
35
37
|
Description-Content-Type: text/markdown
|
|
36
38
|
|
|
37
39
|
<div align="center">
|
|
@@ -43,7 +45,7 @@ Description-Content-Type: text/markdown
|
|
|
43
45
|
</a>
|
|
44
46
|
</div>
|
|
45
47
|
<div align="center">
|
|
46
|
-
<
|
|
48
|
+
<h3>GenAI Agent Framework, the Pydantic way</h3>
|
|
47
49
|
</div>
|
|
48
50
|
<div align="center">
|
|
49
51
|
<a href="https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push" alt="CI"></a>
|
|
@@ -60,43 +62,48 @@ Description-Content-Type: text/markdown
|
|
|
60
62
|
|
|
61
63
|
---
|
|
62
64
|
|
|
63
|
-
Pydantic AI is a Python agent framework designed to
|
|
65
|
+
### <em>Pydantic AI is a Python agent framework designed to help you quickly, confidently, and painlessly build production grade applications and workflows with Generative AI.</em>
|
|
64
66
|
|
|
65
|
-
FastAPI revolutionized web development by offering an innovative and ergonomic design, built on the foundation of [Pydantic Validation](https://docs.pydantic.dev).
|
|
66
67
|
|
|
67
|
-
|
|
68
|
+
FastAPI revolutionized web development by offering an innovative and ergonomic design, built on the foundation of [Pydantic Validation](https://docs.pydantic.dev) and modern Python features like type hints.
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
Yet despite virtually every Python agent framework and LLM library using Pydantic Validation, when we began to use LLMs in [Pydantic Logfire](https://pydantic.dev/logfire), we couldn't find anything that gave us the same feeling.
|
|
71
|
+
|
|
72
|
+
We built Pydantic AI with one simple aim: to bring that FastAPI feeling to GenAI app and agent development.
|
|
70
73
|
|
|
71
74
|
## Why use Pydantic AI
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
1. **Built by the Pydantic Team**:
|
|
77
|
+
[Pydantic Validation](https://docs.pydantic.dev/latest/) 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?_ :smiley:
|
|
78
|
+
|
|
79
|
+
2. **Model-agnostic**:
|
|
80
|
+
Supports virtually every [model](https://ai.pydantic.dev/models/overview) and provider: OpenAI, Anthropic, Gemini, DeepSeek, Grok, Cohere, Mistral, and Perplexity; Azure AI Foundry, Amazon Bedrock, Google Vertex AI, Ollama, LiteLLM, Groq, OpenRouter, Together AI, Fireworks AI, Cerebras, Hugging Face, GitHub, Heroku, Vercel. If your favorite model or provider is not listed, you can easily implement a [custom model](https://ai.pydantic.dev/models/overview#custom-models).
|
|
81
|
+
|
|
82
|
+
3. **Seamless Observability**:
|
|
83
|
+
Tightly [integrates](https://ai.pydantic.dev/logfire) with [Pydantic Logfire](https://pydantic.dev/logfire), our general-purpose OpenTelemetry observability platform, for real-time debugging, evals-based performance monitoring, and behavior, tracing, and cost tracking. If you already have an observability platform that supports OTel, you can [use that too](https://ai.pydantic.dev/logfire#alternative-observability-backends).
|
|
75
84
|
|
|
76
|
-
|
|
77
|
-
|
|
85
|
+
4. **Fully Type-safe**:
|
|
86
|
+
Designed to give your IDE or AI coding agent as much context as possible for auto-completion and [type checking](https://ai.pydantic.dev/agents#static-type-checking), moving entire classes of errors from runtime to write-time for a bit of that Rust "if it compiles, it works" feel.
|
|
78
87
|
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
5. **Powerful Evals**:
|
|
89
|
+
Enables you to systematically test and [evaluate](https://ai.pydantic.dev/evals) the performance and accuracy of the agentic systems you build, and monitor the performance over time in Pydantic Logfire.
|
|
81
90
|
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
6. **MCP, A2A, and AG-UI**:
|
|
92
|
+
Integrates the [Model Context Protocol](https://ai.pydantic.dev/mcp/client), [Agent2Agent](https://ai.pydantic.dev/a2a), and [AG-UI](https://ai.pydantic.dev/ag-ui) standards to give your agent access to external tools and data, let it interoperate with other agents, and build interactive applications with streaming event-based communication.
|
|
84
93
|
|
|
85
|
-
|
|
86
|
-
|
|
94
|
+
7. **Human-in-the-Loop Tool Approval**:
|
|
95
|
+
Easily lets you flag that certain tool calls [require approval](https://ai.pydantic.dev/deferred-tools#human-in-the-loop-tool-approval) before they can proceed, possibly depending on tool call arguments, conversation history, or user preferences.
|
|
87
96
|
|
|
88
|
-
|
|
89
|
-
|
|
97
|
+
8. **Durable Execution**:
|
|
98
|
+
Enables you to build [durable agents](https://ai.pydantic.dev/temporal) that can preserve their progress across transient API failures and application errors or restarts, and handle long-running, asynchronous, and human-in-the-loop workflows with production-grade reliability.
|
|
90
99
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
This is useful for testing and eval-driven iterative development.
|
|
100
|
+
9. **Streamed Outputs**:
|
|
101
|
+
Provides the ability to [stream](https://ai.pydantic.dev/output#streamed-results) structured output continuously, with immediate validation, ensuring real time access to generated data.
|
|
94
102
|
|
|
95
|
-
|
|
96
|
-
|
|
103
|
+
10. **Graph Support**:
|
|
104
|
+
Provides a powerful way to define [graphs](https://ai.pydantic.dev/graph) using type hints, for use in complex applications where standard control flow can degrade to spaghetti code.
|
|
97
105
|
|
|
98
|
-
-
|
|
99
|
-
[Pydantic Graph](https://ai.pydantic.dev/graph) provides a powerful way to define graphs using typing hints, this is useful in complex applications where standard control flow can degrade to spaghetti code.
|
|
106
|
+
Realistically though, no list is going to be as convincing as [giving it a try](#next-steps) and seeing how it makes you feel!
|
|
100
107
|
|
|
101
108
|
## Hello World Example
|
|
102
109
|
|
|
@@ -107,15 +114,13 @@ from pydantic_ai import Agent
|
|
|
107
114
|
|
|
108
115
|
# Define a very simple agent including the model to use, you can also set the model when running the agent.
|
|
109
116
|
agent = Agent(
|
|
110
|
-
'
|
|
111
|
-
# Register
|
|
112
|
-
# For more complex dynamically-generated
|
|
113
|
-
|
|
117
|
+
'anthropic:claude-sonnet-4-0',
|
|
118
|
+
# Register static instructions using a keyword argument to the agent.
|
|
119
|
+
# For more complex dynamically-generated instructions, see the example below.
|
|
120
|
+
instructions='Be concise, reply with one sentence.',
|
|
114
121
|
)
|
|
115
122
|
|
|
116
123
|
# Run the agent synchronously, conducting a conversation with the LLM.
|
|
117
|
-
# Here the exchange should be very short: Pydantic AI will send the system prompt and the user query to the LLM,
|
|
118
|
-
# the model will return a text response. See below for a more complex run.
|
|
119
124
|
result = agent.run_sync('Where does "hello world" come from?')
|
|
120
125
|
print(result.output)
|
|
121
126
|
"""
|
|
@@ -123,9 +128,11 @@ The first known use of "hello, world" was in a 1974 textbook about the C program
|
|
|
123
128
|
"""
|
|
124
129
|
```
|
|
125
130
|
|
|
126
|
-
_(This example is complete, it can be run "as is")_
|
|
131
|
+
_(This example is complete, it can be run "as is", assuming you've [installed the `pydantic_ai` package](https://ai.pydantic.dev/install))_
|
|
127
132
|
|
|
128
|
-
|
|
133
|
+
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.
|
|
134
|
+
|
|
135
|
+
Not very interesting yet, but we can easily add [tools](https://ai.pydantic.dev/tools), [dynamic instructions](https://ai.pydantic.dev/agents#instructions), and [structured outputs](https://ai.pydantic.dev/output) to build more powerful agents.
|
|
129
136
|
|
|
130
137
|
## Tools & Dependency Injection Example
|
|
131
138
|
|
|
@@ -143,14 +150,14 @@ from bank_database import DatabaseConn
|
|
|
143
150
|
|
|
144
151
|
|
|
145
152
|
# SupportDependencies is used to pass data, connections, and logic into the model that will be needed when running
|
|
146
|
-
#
|
|
153
|
+
# instructions and tool functions. Dependency injection provides a type-safe way to customise the behavior of your agents.
|
|
147
154
|
@dataclass
|
|
148
155
|
class SupportDependencies:
|
|
149
156
|
customer_id: int
|
|
150
157
|
db: DatabaseConn
|
|
151
158
|
|
|
152
159
|
|
|
153
|
-
# This
|
|
160
|
+
# This Pydantic model defines the structure of the output returned by the agent.
|
|
154
161
|
class SupportOutput(BaseModel):
|
|
155
162
|
support_advice: str = Field(description='Advice returned to the customer')
|
|
156
163
|
block_card: bool = Field(description="Whether to block the customer's card")
|
|
@@ -161,28 +168,28 @@ class SupportOutput(BaseModel):
|
|
|
161
168
|
# Agents are generic in the type of dependencies they accept and the type of output they return.
|
|
162
169
|
# In this case, the support agent has type `Agent[SupportDependencies, SupportOutput]`.
|
|
163
170
|
support_agent = Agent(
|
|
164
|
-
'openai:gpt-
|
|
171
|
+
'openai:gpt-5',
|
|
165
172
|
deps_type=SupportDependencies,
|
|
166
173
|
# The response from the agent will, be guaranteed to be a SupportOutput,
|
|
167
174
|
# if validation fails the agent is prompted to try again.
|
|
168
175
|
output_type=SupportOutput,
|
|
169
|
-
|
|
176
|
+
instructions=(
|
|
170
177
|
'You are a support agent in our bank, give the '
|
|
171
178
|
'customer support and judge the risk level of their query.'
|
|
172
179
|
),
|
|
173
180
|
)
|
|
174
181
|
|
|
175
182
|
|
|
176
|
-
# Dynamic
|
|
183
|
+
# Dynamic instructions can make use of dependency injection.
|
|
177
184
|
# Dependencies are carried via the `RunContext` argument, which is parameterized with the `deps_type` from above.
|
|
178
185
|
# If the type annotation here is wrong, static type checkers will catch it.
|
|
179
|
-
@support_agent.
|
|
186
|
+
@support_agent.instructions
|
|
180
187
|
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
|
|
181
188
|
customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id)
|
|
182
189
|
return f"The customer's name is {customer_name!r}"
|
|
183
190
|
|
|
184
191
|
|
|
185
|
-
# `tool` let you register functions which the LLM may call while responding to a user.
|
|
192
|
+
# The `tool` decorator let you register functions which the LLM may call while responding to a user.
|
|
186
193
|
# Again, dependencies are carried via `RunContext`, any other arguments become the tool schema passed to the LLM.
|
|
187
194
|
# Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
|
|
188
195
|
@support_agent.tool
|
|
@@ -223,8 +230,10 @@ async def main():
|
|
|
223
230
|
|
|
224
231
|
## Next Steps
|
|
225
232
|
|
|
226
|
-
To try Pydantic AI yourself, follow the instructions [in the examples](https://ai.pydantic.dev/examples/).
|
|
233
|
+
To try Pydantic AI for yourself, [install it](https://ai.pydantic.dev/install) and follow the instructions [in the examples](https://ai.pydantic.dev/examples/setup).
|
|
227
234
|
|
|
228
235
|
Read the [docs](https://ai.pydantic.dev/agents/) to learn more about building applications with Pydantic AI.
|
|
229
236
|
|
|
230
237
|
Read the [API Reference](https://ai.pydantic.dev/api/agent/) to understand Pydantic AI's interface.
|
|
238
|
+
|
|
239
|
+
Join [Slack](https://logfire.pydantic.dev/docs/join-slack/) or file an issue on [GitHub](https://github.com/pydantic/pydantic-ai/issues) if you have any questions.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
README.md,sha256=t2tWkJDwzT_98YeJIS9C4RWpl-Tt_Zu-geAgwkGslA8,11565
|
|
2
|
+
pydantic_ai-1.0.2.dist-info/METADATA,sha256=Y0GjV47OYmghgDTxP2Fr6b00DbPLpqSQoySGdh701D4,13467
|
|
3
|
+
pydantic_ai-1.0.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
4
|
+
pydantic_ai-1.0.2.dist-info/entry_points.txt,sha256=kbKxe2VtDCYS06hsI7P3uZGxcVC08-FPt1rxeiMpIps,50
|
|
5
|
+
pydantic_ai-1.0.2.dist-info/licenses/LICENSE,sha256=vA6Jc482lEyBBuGUfD1pYx-cM7jxvLYOxPidZ30t_PQ,1100
|
|
6
|
+
pydantic_ai-1.0.2.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
README.md,sha256=bRQiIpEWIZOUki4ChFf6CYLwyz1xpbVY-KELGDhaRKA,9980
|
|
2
|
-
pydantic_ai-1.0.0b1.dist-info/METADATA,sha256=wX5Q6poa2aR7dOASp4ZPeXDaXC9d9LxmD0JEjfGt_lg,11792
|
|
3
|
-
pydantic_ai-1.0.0b1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
4
|
-
pydantic_ai-1.0.0b1.dist-info/entry_points.txt,sha256=kbKxe2VtDCYS06hsI7P3uZGxcVC08-FPt1rxeiMpIps,50
|
|
5
|
-
pydantic_ai-1.0.0b1.dist-info/licenses/LICENSE,sha256=vA6Jc482lEyBBuGUfD1pYx-cM7jxvLYOxPidZ30t_PQ,1100
|
|
6
|
-
pydantic_ai-1.0.0b1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|