Repository navigation
Conversation
`Tool(...)` and `Agent(tool_timeout=...)` reject non-positive timeouts and negative max-retry counts at construction, but `FunctionToolset(...)` accepted them silently: `timeout=0` made every tool call fail via `anyio.fail_after(0)` until the run died with `UnexpectedModelBehavior`, and `max_retries=-1` was assigned onto each tool by `add_tool` after `Tool.__init__` had already validated. Reuse the `Tool` validators so the toolset fails fast with the same `UserError`.
|
Thanks for the contribution! To make sure changes are discussed before code is written, we ask that every PR references an existing issue with a closing keyword (e.g. If there's no issue for this yet, please open one first (e.g. a bug report with a reproducible example), wait for a maintainer to confirm it, then update this PR's description to reference it and reopen the PR. Documentation-only fixes are exempt and don't need an issue. Feel free to ping our team if you think this was closed in error. |
ApprovabilityVerdict: Approved at Macroscope's review found this PR approvable — This is a small, self-contained validation fix using existing helpers, with valid configurations unchanged and direct tests for rejected and accepted values. Its runtime impact is limited to earlier, clearer errors for invalid toolset configuration. You can add or adjust custom eligibility rules. Learn more. |
Merging this PR will not alter performance
Comparing Footnotes
|
FunctionToolset(timeout=...)andFunctionToolset(max_retries=...)accepted invalid values without validating them, while the sibling constructors reject them at construction time:timeoutbecame the enforced per-call timeout incall_tool(anyio.fail_after(timeout),pydantic_ai_slim/pydantic_ai/toolsets/function.py:707-711), sotimeout=0failed every tool call with a timeoutModelRetryuntil the run died — with no error pointing at the bad value.max_retrieswas stamped onto every tool without its own count inadd_tool(pydantic_ai_slim/pydantic_ai/toolsets/function.py:605-606), afterTool.__init__had already validated the tool's own value, andget_toolsfalls back to it for the run-context retry budget (pydantic_ai_slim/pydantic_ai/toolsets/function.py:633).The
Agent(tool_timeout=...)path got this validation in #6383 (for #6382); aFunctionToolsetconstructed directly, or passed viaAgent(toolsets=[...]), bypassed it.Why
FunctionToolset.__init__storedmax_retriesandtimeoutunchecked, unlikeTool.__init__(pydantic_ai_slim/pydantic_ai/tools.py:453-454) andAgent.__init__(pydantic_ai_slim/pydantic_ai/agent/__init__.py:743-744).Changes
FunctionToolset.__init__now calls the existing_validate_max_retriesand_validate_timeouthelpers frompydantic_ai.tools, so an invalid value fails fast with the sameUserErrorthe sibling constructors raise.Noneand valid values are unaffected.Testing
uv run pytest tests/test_toolsets.py -k function_toolset— 24 passed, including 5 new tests rejectingtimeoutof0/-1andmax_retriesof-1withUserError, and acceptingNone/valid values.uv run pytest tests/test_toolsets.py tests/test_tools.py— 295 passed.uv run ruff format --checkanduv run ruff check— clean.uv run pyright— 0 errors, 0 warnings, 0 informations.Checklist
UserErroronly fires on values that could never work —timeout=0fails every tool call immediately, and a negative retry count permits no retry — the "code relying on undocumented assumptions" case the policy excludes from breaking changes; fix: validate Agent tool_timeout #6383 shipped the same validation forAgentas a plain fix.Fixes #8815