Skip to content

Commit 6354c7f

Browse files
authored
Fix toggleable toolset example so toolset state is not shared across agent runs (#2396)
1 parent 749dc98 commit 6354c7f

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

docs/toolsets.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,29 +403,30 @@ from function_toolset import weather_toolset, datetime_toolset
403403

404404
from pydantic_ai import Agent, RunContext
405405
from pydantic_ai.models.test import TestModel
406-
from pydantic_ai.toolsets import WrapperToolset, FunctionToolset
406+
from pydantic_ai.toolsets import WrapperToolset
407407

408408
togglable_toolset = WrapperToolset(weather_toolset)
409409

410+
test_model = TestModel() # (1)!
411+
agent = Agent(
412+
test_model,
413+
deps_type=WrapperToolset # (2)!
414+
)
415+
416+
@agent.tool
410417
def toggle(ctx: RunContext[WrapperToolset]):
411418
if ctx.deps.wrapped == weather_toolset:
412419
ctx.deps.wrapped = datetime_toolset
413420
else:
414421
ctx.deps.wrapped = weather_toolset
415422

416-
test_model = TestModel() # (1)!
417-
agent = Agent(
418-
test_model,
419-
deps_type=WrapperToolset, # (2)!
420-
toolsets=[togglable_toolset, FunctionToolset([toggle])]
421-
)
422-
result = agent.run_sync('Toggle the toolset', deps=togglable_toolset)
423+
result = agent.run_sync('Toggle the toolset', deps=togglable_toolset, toolsets=[togglable_toolset])
423424
print([t.name for t in test_model.last_model_request_parameters.function_tools]) # (3)!
424-
#> ['now', 'toggle']
425+
#> ['toggle', 'now']
425426

426-
result = agent.run_sync('Toggle the toolset', deps=togglable_toolset)
427+
result = agent.run_sync('Toggle the toolset', deps=togglable_toolset, toolsets=[togglable_toolset])
427428
print([t.name for t in test_model.last_model_request_parameters.function_tools])
428-
#> ['temperature_celsius', 'temperature_fahrenheit', 'conditions', 'toggle']
429+
#> ['toggle', 'temperature_celsius', 'temperature_fahrenheit', 'conditions']
429430
```
430431

431432
1. We're using [`TestModel`][pydantic_ai.models.test.TestModel] here because it makes it easy to see which tools were available on each run.

0 commit comments

Comments
 (0)