You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Create an agent, which expects an integer dependency and returns a boolean result. This agent will have type `#!python Agent[int, bool]`.
52
-
2. Define a tool that checks if the square is a winner. Here [`CallContext`][pydantic_ai.dependencies.CallContext] is parameterized with the dependency type `int`; if you got the dependency type wrong you'd get a typing error.
52
+
2. Define a tool that checks if the square is a winner. Here [`RunContext`][pydantic_ai.dependencies.RunContext] is parameterized with the dependency type `int`; if you got the dependency type wrong you'd get a typing error.
53
53
3. In reality, you might want to use a random number here e.g. `random.randint(0, 36)`.
54
54
4.`result.data` will be a boolean indicating if the square is a winner. Pydantic performs the result validation, it'll be typed as a `bool` since its type is derived from the `result_type` generic parameter of the agent.
55
55
@@ -135,7 +135,7 @@ Here's an example using both types of system prompts:
2. Static system prompt defined at agent creation time.
164
-
3. Dynamic system prompt defined via a decorator with [`CallContext`][pydantic_ai.dependencies.CallContext], this is called just after `run_sync`, not when the agent is created, so can benefit from runtime information like the dependencies used on that run.
165
-
4. Another dynamic system prompt, system prompts don't have to have the `CallContext` parameter.
164
+
3. Dynamic system prompt defined via a decorator with [`RunContext`][pydantic_ai.dependencies.RunContext], this is called just after `run_sync`, not when the agent is created, so can benefit from runtime information like the dependencies used on that run.
165
+
4. Another dynamic system prompt, system prompts don't have to have the `RunContext` parameter.
166
166
167
167
_(This example is complete, it can be run "as is")_
168
168
@@ -179,8 +179,8 @@ They're useful when it is impractical or impossible to put all the context an ag
179
179
180
180
There are two different decorator functions to register tools:
181
181
182
-
1.[`@agent.tool`][pydantic_ai.Agent.tool] — for tools that need access to the agent [context][pydantic_ai.dependencies.CallContext]
183
-
2.[`@agent.tool_plain`][pydantic_ai.Agent.tool_plain] — for tools that do not need access to the agent [context][pydantic_ai.dependencies.CallContext]
182
+
1.[`@agent.tool`][pydantic_ai.Agent.tool] — for tools that need access to the agent [context][pydantic_ai.dependencies.RunContext]
183
+
2.[`@agent.tool_plain`][pydantic_ai.Agent.tool_plain] — for tools that do not need access to the agent [context][pydantic_ai.dependencies.RunContext]
184
184
185
185
`@agent.tool` is the default since in the majority of cases tools will need access to the agent context.
186
186
@@ -189,7 +189,7 @@ Here's an example using both:
189
189
```py title="dice_game.py"
190
190
import random
191
191
192
-
from pydantic_ai import Agent, CallContext
192
+
from pydantic_ai import Agent, RunContext
193
193
194
194
agent = Agent(
195
195
'gemini-1.5-flash', # (1)!
@@ -209,7 +209,7 @@ def roll_die() -> str:
209
209
210
210
211
211
@agent.tool# (4)!
212
-
defget_player_name(ctx: CallContext[str]) -> str:
212
+
defget_player_name(ctx: RunContext[str]) -> str:
213
213
"""Get the player's name."""
214
214
return ctx.deps
215
215
@@ -222,7 +222,7 @@ print(dice_result.data)
222
222
1. This is a pretty simple task, so we can use the fast and cheap Gemini flash model.
223
223
2. We pass the user's name as the dependency, to keep things simple we use just the name as a string as the dependency.
224
224
3. This tool doesn't need any context, it just returns a random number. You could probably use a dynamic system prompt in this case.
225
-
4. This tool needs the player's name, so it uses `CallContext` to access dependencies which are just the player's name in this case.
225
+
4. This tool needs the player's name, so it uses `RunContext` to access dependencies which are just the player's name in this case.
226
226
5. Run the agent, passing the player's name as the dependency.
227
227
228
228
_(This example is complete, it can be run "as is")_
@@ -325,7 +325,7 @@ As the name suggests, function tools use the model's "tools" or "functions" API
325
325
326
326
### Function tools and schema
327
327
328
-
Function parameters are extracted from the function signature, and all parameters except `CallContext` are used to build the schema for that tool call.
328
+
Function parameters are extracted from the function signature, and all parameters except `RunContext` are used to build the schema for that tool call.
329
329
330
330
Even better, PydanticAI extracts the docstring from functions and (thanks to [griffe](https://mkdocstrings.github.io/griffe/)) extracts parameter descriptions from the docstring and adds them to the schema.
331
331
@@ -395,15 +395,15 @@ Validation errors from both function tool parameter validation and [structured r
395
395
You can also raise [`ModelRetry`][pydantic_ai.exceptions.ModelRetry] from within a [tool](#function-tools) or [result validator function](results.md#result-validators-functions) to tell the model it should retry generating a response.
396
396
397
397
- The default retry count is **1** but can be altered for the [entire agent][pydantic_ai.Agent.__init__], a [specific tool][pydantic_ai.Agent.tool], or a [result validator][pydantic_ai.Agent.__init__].
398
-
- You can access the current retry count from within a tool or result validator via [`ctx.retry`][pydantic_ai.dependencies.CallContext].
398
+
- You can access the current retry count from within a tool or result validator via [`ctx.retry`][pydantic_ai.dependencies.RunContext].
399
399
400
400
Here's an example:
401
401
402
402
```py title="tool_retry.py"
403
403
from fake_database import DatabaseConn
404
404
from pydantic import BaseModel
405
405
406
-
from pydantic_ai import Agent, CallContext, ModelRetry
406
+
from pydantic_ai import Agent, RunContext, ModelRetry
Copy file name to clipboardExpand all lines: docs/dependencies.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,15 +51,15 @@ _(This example is complete, it can be run "as is")_
51
51
52
52
## Accessing Dependencies
53
53
54
-
Dependencies are accessed through the [`CallContext`][pydantic_ai.dependencies.CallContext] type, this should be the first parameter of system prompt functions etc.
54
+
Dependencies are accessed through the [`RunContext`][pydantic_ai.dependencies.RunContext] type, this should be the first parameter of system prompt functions etc.
#> Did you hear about the toothpaste scandal? They called it Colgate.
93
93
```
94
94
95
-
1.[`CallContext`][pydantic_ai.dependencies.CallContext] may optionally be passed to a [`system_prompt`][pydantic_ai.Agent.system_prompt] function as the only argument.
96
-
2.[`CallContext`][pydantic_ai.dependencies.CallContext] is parameterized with the type of the dependencies, if this type is incorrect, static type checkers will raise an error.
97
-
3. Access dependencies through the [`.deps`][pydantic_ai.dependencies.CallContext.deps] attribute.
98
-
4. Access dependencies through the [`.deps`][pydantic_ai.dependencies.CallContext.deps] attribute.
95
+
1.[`RunContext`][pydantic_ai.dependencies.RunContext] may optionally be passed to a [`system_prompt`][pydantic_ai.Agent.system_prompt] function as the only argument.
96
+
2.[`RunContext`][pydantic_ai.dependencies.RunContext] is parameterized with the type of the dependencies, if this type is incorrect, static type checkers will raise an error.
97
+
3. Access dependencies through the [`.deps`][pydantic_ai.dependencies.RunContext.deps] attribute.
98
+
4. Access dependencies through the [`.deps`][pydantic_ai.dependencies.RunContext.deps] attribute.
99
99
100
100
_(This example is complete, it can be run "as is")_
101
101
@@ -117,7 +117,7 @@ from dataclasses import dataclass
"""Returns the customer's current account balance."""# (7)!
100
100
balance =await ctx.deps.db.customer_balance(
@@ -126,8 +126,8 @@ async def main():
126
126
2. Here we configure the agent to use [OpenAI's GPT-4o model](api/models/openai.md), you can also set the model when running the agent.
127
127
3. The `SupportDependencies` dataclass is used to pass data, connections, and logic into the model that will be needed when running [system prompt](agents.md#system-prompts) and [tool](agents.md#function-tools) functions. PydanticAI's system of dependency injection provides a type-safe way to customise the behavior of your agents, and can be especially useful when running unit tests and evals.
128
128
4. Static [system prompts](agents.md#system-prompts) can be registered with the [`system_prompt` keyword argument][pydantic_ai.Agent.__init__] to the agent.
129
-
5. Dynamic [system prompts](agents.md#system-prompts) can be registered with the [`@agent.system_prompt`][pydantic_ai.Agent.system_prompt] decorator, and can make use of dependency injection. Dependencies are carried via the [`CallContext`][pydantic_ai.dependencies.CallContext] argument, which is parameterized with the `deps_type` from above. If the type annotation here is wrong, static type checkers will catch it.
130
-
6.[Tools](agents.md#function-tools) let you register "tools" which the LLM may call while responding to a user. Again, dependencies are carried via [`CallContext`][pydantic_ai.dependencies.CallContext], and 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.
129
+
5. Dynamic [system prompts](agents.md#system-prompts) can be registered with the [`@agent.system_prompt`][pydantic_ai.Agent.system_prompt] decorator, and can make use of dependency injection. Dependencies are carried via the [`RunContext`][pydantic_ai.dependencies.RunContext] argument, which is parameterized with the `deps_type` from above. If the type annotation here is wrong, static type checkers will catch it.
130
+
6.[Tools](agents.md#function-tools) let you register "tools" which the LLM may call while responding to a user. Again, dependencies are carried via [`RunContext`][pydantic_ai.dependencies.RunContext], and 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.
131
131
7. The docstring of a tool is also passed to the LLM as the description of the tool. Parameter descriptions are [extracted](agents.md#function-tools-and-schema) from the docstring and added to the tool schema sent to the LLM.
132
132
8.[Run the agent](agents.md#running-agents) 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 a result.
133
133
9. The response from the agent will, be guaranteed to be a `SupportResult`, if validation fails [reflection](agents.md#reflection-and-self-correction) will mean the agent is prompted to try again.
0 commit comments