fix(anthropic): preserve tool_use when ToolCallPart.Input is empty or malformed#219
Merged
andreynering merged 1 commit intocharmbracelet:mainfrom Apr 29, 2026
Conversation
… malformed
json.Unmarshal("", &m) errors and previously caused the tool_use to be
silently dropped in toPrompt, leaving the matching tool_result orphaned
in the next user message. The Anthropic API then rejects the request
with "tool_result must have a corresponding tool_use in the previous
message". Default empty/whitespace input to {} (and nil for the
server_tool_use branch); on malformed-but-non-empty input, emit {} +
a CallWarning rather than dropping the block.
Hit in the wild via DeepSeek's anthropic-compat endpoint, which emits
empty input for parameterless tool calls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
ToolCallPart.Inputis empty (""), null, or otherwise malformed JSON,toPromptpreviously dropped the correspondingtool_useblock silently. The agent loop has already executed the tool by that point and queued aToolResultPartfor the next user message, so the result survives without its parent. Anthropic's API enforces that everytool_resultblock have a matchingtool_usein the previous message and rejects the next request with HTTP 400:Reproduced via DeepSeek's anthropic-compat endpoint (
https://api.deepseek.com/anthropic/v1/messages), where the model emits parameterlesstool_useblocks with emptyinput. The bug is provider-agnostic — any anthropic-compat endpoint that emits empty/malformed input triggers it.Fix
Two new helpers (
decodeToolCallInputMap,decodeToolCallInputAny) handle the empty/malformed cases:ServerToolUseBlockParam.Input any). No warning — empty input is a valid round-trip.CallWarningnaming the tool call ID and the parse error. The block is still emitted sotool_use ↔ tool_resultpairing is preserved.Even malformed input is better surfaced as
{}plus a warning than as a silently dropped block, because a dropped block guarantees a 400 on the next round-trip.Tests
should drop assistant messages with invalid tool inputpinned the drop as intended. Renamed and rewritten asshould preserve tool_use block when input JSON is malformed.should preserve tool_use block when input is empty.Test plan
go test ./providers/anthropic/...passesgo vet ./providers/anthropic/...passesgofumpt -lclean on modified files