Skip to content

Validate timeout and max_retries in FunctionToolset - #8788

Closed
BlueX888 wants to merge 1 commit into
pydantic:mainfrom
BlueX888:fix/prep-function-toolset-timeout-max-retries-unvalidated
Closed

BlueX888 wants to merge 1 commit into
pydantic:mainfrom
BlueX888:fix/prep-function-toolset-timeout-max-retries-unvalidated

Conversation

@BlueX888

@BlueX888 BlueX888 commented Sep 25, 2026 •

Copy link
Copy Markdown

FunctionToolset(timeout=...) and FunctionToolset(max_retries=...) accepted invalid values without validating them, while the sibling constructors reject them at construction time:

  • A non-positive timeout became the enforced per-call timeout in call_tool (anyio.fail_after(timeout), pydantic_ai_slim/pydantic_ai/toolsets/function.py:707-711), so timeout=0 failed every tool call with a timeout ModelRetry until the run died — with no error pointing at the bad value.
  • A negative max_retries was stamped onto every tool without its own count in add_tool (pydantic_ai_slim/pydantic_ai/toolsets/function.py:605-606), after Tool.__init__ had already validated the tool's own value, and get_tools falls 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); a FunctionToolset constructed directly, or passed via Agent(toolsets=[...]), bypassed it.

Why

FunctionToolset.__init__ stored max_retries and timeout unchecked, unlike Tool.__init__ (pydantic_ai_slim/pydantic_ai/tools.py:453-454) and Agent.__init__ (pydantic_ai_slim/pydantic_ai/agent/__init__.py:743-744).

Changes

FunctionToolset.__init__ now calls the existing _validate_max_retries and _validate_timeout helpers from pydantic_ai.tools, so an invalid value fails fast with the same UserError the sibling constructors raise. None and valid values are unaffected.

Testing

  • uv run pytest tests/test_toolsets.py -k function_toolset — 24 passed, including 5 new tests rejecting timeout of 0/-1 and max_retries of -1 with UserError, and accepting None/valid values.
  • uv run pytest tests/test_toolsets.py tests/test_tools.py — 295 passed.
  • uv run ruff format --check and uv run ruff check — clean.
  • uv run pyright — 0 errors, 0 warnings, 0 informations.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy: the new UserError only fires on values that could never work — timeout=0 fails 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 for Agent as a plain fix.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (No compatibility impact.)
  • PR title is fit for the release changelog.

Review in cubic

Fixes #8815

`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`.
@github-actions github-actions Bot added the size: S Small PR (≤100 weighted lines) label Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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. Fixes #1234) in its description, so this PR has been closed automatically.

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.

@github-actions github-actions Bot closed this Sep 25, 2026
@github-actions github-actions Bot added the bug Report that something isn't working, or PR implementing a fix label Sep 25, 2026
@macroscopeapp

macroscopeapp Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at a072737

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.

@codspeed

codspeed Bot commented Sep 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing BlueX888:fix/prep-function-toolset-timeout-max-retries-unvalidated (a072737) with main (84ebeb0)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: S Small PR (≤100 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FunctionToolset accepts non-positive timeout and negative max_retries without validation

1 participant