Skip to content

Commit be47b36

Browse files
committed
Update docs for tests
1 parent eb478a9 commit be47b36

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

docs/toolsets.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,19 +439,27 @@ A Dynamic Toolset is a toolset that is built dynamically during an Agent run usi
439439

440440
To use a Dynamic Toolset, you can pass a function that matches ['`ToolsetFunc`][pydantic_ai.toolsets._dynamic.ToolsetFunc] to the `toolsets` argument of the `Agent` constructor or you can wrap a compliant function in the ['`@agent.toolset`][pydantic_ai.Agent.toolset] decorator.
441441

442-
The function will be called with the agent's [run context][pydantic_ai.tools.RunContext] and should return a [`Toolset`][pydantic_ai.toolsets.AbstractToolset] or [`None`][typing.None]. The function will be called once for each agent run step. If you are using the decorator, you can optionally provide a `per_run_step` argument to indicate whether the function should be called once for each agent run step or only once for the entire run.
442+
The function will be called with the agent's [run context][pydantic_ai.tools.RunContext] and should return a [`Toolset`][pydantic_ai.toolsets.AbstractToolset] or [`None`][typing.None]. The function will be called once for each agent run step. If you are using the decorator, you can optionally provide a `per_run_step` argument to indicate whether the function should be called once for each agent run step or only once for the entire run.
443443

444-
```python {title="dynamic_toolset.py"}
444+
```python {title="agent_dynamic_toolset.py", requires="function_toolset.py"}
445+
from function_toolset import weather_toolset
446+
447+
from pydantic_ai import Agent, RunContext
448+
from pydantic_ai.models.test import TestModel
449+
from pydantic_ai.toolsets.function import FunctionToolset
445450

446451
def build_toolset(ctx: RunContext) -> FunctionToolset:
447-
return FunctionToolset()
452+
return weather_toolset
448453

454+
test_model = TestModel() # (1)!
449455
agent = Agent(
450-
'openai:gpt-4o',
456+
test_model,
451457
toolsets=[build_toolset],
452458
)
453459

454460
result = agent.run_sync('Use the toolset')
461+
print([t.name for t in test_model.last_model_request_parameters.function_tools])
462+
#> ['temperature_celsius', 'temperature_fahrenheit', 'conditions']
455463
```
456464

457465
## Building a Custom Toolset

tests/test_toolsets.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,24 @@ def get_inner_toolset(toolset: DynamicToolset[None] | None) -> EnterableToolset
677677
assert (inner_toolset := get_inner_toolset(toolset))
678678
assert inner_toolset.depth_count == 1
679679

680+
def visitor(toolset: AbstractToolset[None]) -> None:
681+
assert toolset is inner_toolset
682+
683+
toolset.apply(visitor)
684+
680685
assert get_inner_toolset(toolset) is None
681686

682687
assert tools == {}
688+
689+
async def test_dynamic_toolset_empty():
690+
691+
def no_toolset_func(ctx: RunContext[None]) -> None:
692+
return None
693+
694+
toolset = DynamicToolset[None](toolset_func=no_toolset_func)
695+
696+
run_context = build_run_context(None)
697+
698+
tools = await toolset.get_tools(run_context)
699+
700+
assert tools == {}

0 commit comments

Comments
 (0)