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
Copy file name to clipboardExpand all lines: docs/concepts/dependencies.md
+14-11Lines changed: 14 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Dependencies
2
2
3
-
PydanticAI uses a dependency injection system to provide data and services to your agent's [system prompts](system-prompt.md), [retrievers](retrievers.md) and [result validators](result-validation.md#TODO).
3
+
PydanticAI uses a dependency injection system to provide data and services to your agent's [system prompts](system-prompt.md), [retrievers](retrievers.md) and [result validators](results.md#TODO).
4
4
5
5
Matching PydanticAI's design philosophy, our dependency system tries to use existing best practice in Python development rather than inventing esoteric "magic", this should make dependencies type-safe, understandable easier to test and ultimately easier to deploy in production.
6
6
@@ -13,7 +13,7 @@ Here's an example of defining an agent that requires dependencies.
13
13
14
14
(**Note:** dependencies aren't actually used in this example, see [Accessing Dependencies](#accessing-dependencies) below)
15
15
16
-
```python title="unused_dependencies.py"
16
+
```py title="unused_dependencies.py"
17
17
from dataclasses import dataclass
18
18
19
19
import httpx
@@ -41,6 +41,7 @@ async def main():
41
41
deps=deps, # (3)!
42
42
)
43
43
print(result.data)
44
+
#> Did you hear about the toothpaste scandal? They called it Colgate.
result =await agent.run('Tell me a joke.', deps=deps)
91
92
print(result.data)
93
+
#> Did you hear about the toothpaste scandal? They called it Colgate.
92
94
```
93
95
94
96
1.[`CallContext`][pydantic_ai.dependencies.CallContext] may optionally be passed to a [`system_prompt`][pydantic_ai.Agent.system_prompt] function as the only argument.
4. Call the agent from within the application code, in a real application this call might be deep within a call stack. Note `app_deps` here will NOT be used when deps are overridden.
274
276
275
277
```py title="test_joke_app.py" hl_lines="10-12"
276
-
from joke_app importapplication_code, joke_agent, MyDeps
278
+
from joke_app importMyDeps, application_code, joke_agent
277
279
278
280
279
281
classTestMyDeps(MyDeps): # (1)!
@@ -284,8 +286,8 @@ class TestMyDeps(MyDeps): # (1)!
284
286
asyncdeftest_application_code():
285
287
test_deps = TestMyDeps('test_key', None) # (2)!
286
288
with joke_agent.override_deps(test_deps): # (3)!
287
-
joke = application_code('Tell me a joke.') # (4)!
288
-
assert joke=='funny'
289
+
joke =awaitapplication_code('Tell me a joke.') # (4)!
290
+
assert joke.startswith('Did you hear about the toothpaste scandal?')
289
291
```
290
292
291
293
1. Define a subclass of `MyDeps` in tests to customise the system prompt factory.
@@ -314,7 +316,7 @@ joke_agent = Agent(
314
316
system_prompt=(
315
317
'Use the "joke_factory" to generate some jokes, then choose the best. '
0 commit comments