Skip to content

Preserve iterable ToolError content - #1837

Open
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/tool-error-iterable-content
Open

Preserve iterable ToolError content#1837
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/tool-error-iterable-content

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Preserve structured ToolError content when callers provide a one-shot iterable such as a generator.

BetaFunctionToolResultType explicitly accepts Iterable[BetaContent]. ToolError.__init__() currently iterates non-string content to build the exception message and then stores the same iterable on self.content. For generators, that consumes the iterable before the tool runner later reads exc.content, so the emitted tool_result loses the structured error content.

Fix

Materialize non-string ToolError content once before rendering the exception message. The same reusable collection is then retained on self.content for the runner.

String content remains unchanged.

Regression coverage

Adds focused tests verifying:

  • generator-backed structured error content survives through tool_error_content(), the same path used by the runners;
  • the stored content remains reusable after exception message construction;
  • string ToolError behavior is unchanged.

The production change is three lines in Anthropic's hand-maintained tool helper.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 16, 2026 23:04
Jeffchiucp pushed a commit to Jeffchiucp/anthropic-sdk-python that referenced this pull request Aug 18, 2026
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.

1 participant