-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow overriding deps, e.g. in testing (#41)
- Loading branch information
1 parent
265a7a7
commit a8edf02
Showing
4 changed files
with
93 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
- run_sync | ||
- run_stream | ||
- model | ||
- override_deps | ||
- last_run_messages | ||
- system_prompt | ||
- retriever_plain | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from dataclasses import dataclass | ||
|
||
from pydantic_ai import Agent, CallContext | ||
from pydantic_ai.models.test import TestModel | ||
|
||
|
||
@dataclass | ||
class MyDeps: | ||
foo: int | ||
bar: int | ||
|
||
|
||
agent = Agent(TestModel(), deps_type=MyDeps) | ||
|
||
|
||
@agent.retriever_context | ||
async def test_retriever(ctx: CallContext[MyDeps]) -> str: | ||
return f'{ctx.deps}' | ||
|
||
|
||
def test_deps_used(): | ||
result = agent.run_sync('foobar', deps=MyDeps(foo=1, bar=2)) | ||
assert result.data == '{"test_retriever":"MyDeps(foo=1, bar=2)"}' | ||
|
||
|
||
def test_deps_override(): | ||
with agent.override_deps(MyDeps(foo=3, bar=4)): | ||
result = agent.run_sync('foobar', deps=MyDeps(foo=1, bar=2)) | ||
assert result.data == '{"test_retriever":"MyDeps(foo=3, bar=4)"}' | ||
|
||
with agent.override_deps(MyDeps(foo=5, bar=6)): | ||
result = agent.run_sync('foobar', deps=MyDeps(foo=1, bar=2)) | ||
assert result.data == '{"test_retriever":"MyDeps(foo=5, bar=6)"}' | ||
|
||
result = agent.run_sync('foobar', deps=MyDeps(foo=1, bar=2)) | ||
assert result.data == '{"test_retriever":"MyDeps(foo=3, bar=4)"}' | ||
|
||
result = agent.run_sync('foobar', deps=MyDeps(foo=1, bar=2)) | ||
assert result.data == '{"test_retriever":"MyDeps(foo=1, bar=2)"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters