Skip to content

Preserve one-shot iterable content in ToolError - #1953

Open
jjt998 wants to merge 2 commits into
anthropics:mainfrom
jjt998:fix/preserve-tool-error-iterables-clean
Open

jjt998 wants to merge 2 commits into
anthropics:mainfrom
jjt998:fix/preserve-tool-error-iterables-clean

Conversation

@jjt998

@jjt998 jjt998 commented Sep 27, 2026

Copy link
Copy Markdown

Summary

ToolError accepts structured content as an Iterable. When callers passed a one-shot iterable such as a generator, the constructor consumed it while building the exception message, leaving the runner with empty tool-result content.

Materialize non-string content once before deriving the message so the same structured blocks remain available to the tool runner.

Tests

  • Added a regression test covering generator-backed ToolError content.
  • ruff check and py_compile pass locally; the full test suite could not run because the environment is missing the repository's httpx2 development dependency.

@jjt998
jjt998 requested a review from a team as a code owner September 27, 2026 08:03

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjt998 the fix is right, and the branch still wants ./scripts/format. Nothing here blocks a merge. ./scripts/lint is what CI runs, and it passes.

non-blocking: ruff format wants two hunks in tests/lib/tools/test_session_runner.py. The blank line at :909 carries four trailing spaces, unrelated to the fix. The nested boom helper in the new test wants a blank line either side. [tool.ruff.lint] select omits W, so ruff check stays quiet.

non-blocking: #1837 adds the identical content = list(content) line.

Verification

git archive of 950052d into a clean directory, then uv sync --all-extras. ruff 0.15.6, pyright 1.1.399.

$ sed -n '909p' tests/lib/tools/test_session_runner.py | cat -A
    $

$ uv run ruff format --diff tests/lib/tools/test_session_runner.py
--- tests/lib/tools/test_session_runner.py
+++ tests/lib/tools/test_session_runner.py
@@ -906,7 +906,7 @@
# the blank line above the first assert loses its four spaces
@@ -1730,9 +1730,11 @@
# a blank line arrives before `async def boom` and before `tool = _FakeTool`
1 file would be reformatted

$ uv run ruff check .
All checks passed!

$ uv run pyright --project .
186 errors, 16 warnings, 0 informations
# all 186 are in tests/test_middleware.py, which this branch does not touch; the two changed files are clean

$ uv run pytest tests/lib/tools/test_session_runner.py -q -p no:randomly
57 passed in 7.56s

# src/anthropic/lib/tools/_beta_functions.py restored to 4421d56, the test kept
$ uv run pytest tests/lib/tools/test_session_runner.py -q -p no:randomly
1 failed, 56 passed in 7.29s
FAILED tests/lib/tools/test_session_runner.py::test_tool_error_preserves_generator_content
# not run: the rest of tests/, and any run on Windows

CODEOWNERS carries only * @anthropics/sdk. @dtmeadows-ant merged the last two outside changes under src/anthropic/lib (#1795, #1592). @jjt998, will you run ./scripts/format before a maintainer reads this?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants