Skip to content

fix: preserve empty error_code on ToolSearchToolResult.ToParam (#317)#338

Open
spor3006 wants to merge 1 commit into
anthropics:mainfrom
spor3006:fix/317-toolsearch-error-code-omitzero
Open

fix: preserve empty error_code on ToolSearchToolResult.ToParam (#317)#338
spor3006 wants to merge 1 commit into
anthropics:mainfrom
spor3006:fix/317-toolsearch-error-code-omitzero

Conversation

@spor3006
Copy link
Copy Markdown

Summary

ToolSearchToolResultBlock.ToParam() and its beta twin BetaToolSearchToolResultBlock.ToParam() build a ToolSearchToolResultErrorParam whose ErrorCode is tagged json:"error_code,omitzero". When the response value resolves to the zero value of ToolSearchToolResultErrorCode (empty string — e.g. the API returned an enum value the SDK doesn't list yet, or returned error_code: ""), omitzero drops the field. The next request then fails server-side with:

messages.N.content.M.tool_search_tool_result.content.RequestToolSearchToolResultError.error_code: Field required

This is the same class of ToParam round-trip bug as #242, but isolated to the tool-search error variant. Reported in #317 with a working raw-JSON passthrough workaround.

Fix

In the handwritten helpers in messageutil.go and betamessageutil.go, when the typed ErrorCode is the zero value, use the param package's SetExtraFields escape hatch (documented at packages/param/param.go:146-159) to force the field to marshal with the original raw value from the response. Known/recognized codes continue to flow through the typed field exactly as before.

This deliberately:

  • Does not modify any Stainless-generated types — those would be regenerated away.
  • Lives in the handwritten *messageutil.go helpers that CONTRIBUTING.md already designates as the place for this kind of manual fix.
  • Mirrors the change into betamessageutil.go because the bug is identical there.

Test

Adds two subtests under TestContentBlockUnionToParam:

Both tests round-trip the marshaled JSON to assert content.error_code is actually present in the wire payload (not just on the typed struct), which is what the API enforces.

Test plan

  • ./scripts/lint passes
  • go test -run TestContentBlockUnionToParam ./... passes
  • New regression test fails on main without the fix and passes with it
  • No changes to generated types — safe across regenerations

Fixes #317
Related to #242

…oParam

ToolSearchToolResultBlock.ToParam() (and its beta twin) constructs a
ToolSearchToolResultErrorParam whose ErrorCode field is tagged
`json:"error_code,omitzero"`. When the response value resolves to the
zero value of ToolSearchToolResultErrorCode (an empty string — typically
because the API returned a code the current SDK version does not list
in its enum, or returned an empty string explicitly), `omitzero` drops
the field entirely. The next request then fails server-side with:

  messages.N.content.M.tool_search_tool_result.content
    .RequestToolSearchToolResultError.error_code: Field required

Use the param package's SetExtraFields escape hatch to force the field
to be emitted with the original raw string value when the typed value
is zero. This is the smallest correct fix and avoids touching the
generated types, which would be regenerated away.

Adds two regression tests covering both a recognized error_code and the
empty-string case that previously regressed.

Fixes anthropics#317

Signed-off-by: Sparshal Kothari <41056517+spor3006@users.noreply.github.com>
@spor3006 spor3006 requested a review from a team as a code owner May 17, 2026 05:05
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.

ToolSearchToolResultBlock.ToParam() drops required error_code field via omitzero

1 participant