Skip to content

Fix ToolResultBlockParam to support both string and array content#297

Open
joaquinhuigomez wants to merge 1 commit intoanthropics:mainfrom
joaquinhuigomez:fix/tool-result-string-content
Open

Fix ToolResultBlockParam to support both string and array content#297
joaquinhuigomez wants to merge 1 commit intoanthropics:mainfrom
joaquinhuigomez:fix/tool-result-string-content

Conversation

@joaquinhuigomez
Copy link
Copy Markdown

@joaquinhuigomez joaquinhuigomez commented Mar 17, 2026

The Anthropic API accepts tool results as either a plain string or an array of content blocks, but the Go SDK's ToolResultBlockParam only serializes the array form. This adds support for both formats with a ContentString field and custom JSON marshaling that picks the right wire format automatically.

…mats

The API docs state that tool_result content can be either a string
(e.g. "content": "15 degrees") or an array of content blocks. The SDK
previously only supported the array format, causing deserialization
errors when content was a plain string.

Changes:
- Add ContentString field to ToolResultBlockParam and BetaToolResultBlockParam
  for explicit string content serialization
- Update MarshalJSON to output content as a string when ContentString is set
- Update UnmarshalJSON to handle string content by normalizing it into a
  Content array with a single TextBlock
- Update NewToolResultBlock to use string format (matches API docs examples)
- Add NewToolResultBlockFromArray for explicit array content construction
- Add NewBetaToolResultStringBlockParam for explicit string content
- Add comprehensive tests for both formats

Fixes anthropics#182
@joaquinhuigomez joaquinhuigomez requested a review from a team as a code owner March 17, 2026 14:19
@joaquinhuigomez joaquinhuigomez force-pushed the fix/tool-result-string-content branch from edefef9 to c907622 Compare March 24, 2026 20:51
@joaquinhuigomez
Copy link
Copy Markdown
Author

Hi — just checking if this is still on the review queue. Happy to address any feedback.

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