Repository navigation
fix(anthropic): tolerate a missing message id in responses - #3394
Merged
Merged
Conversation
AnthropicResponseParser called Message.id() directly, and the SDK's typed accessor throws AnthropicInvalidDataException when the field is absent. Any gateway or proxy that strips the top-level id therefore broke the response on both paths: - parseMessage failed outright; - the streaming message_start branch threw before it had seeded the shared usage state, so the prompt and cache token counts that message_delta reads back were lost as well. Resolve the id through the raw _id() field first, falling back to the typed accessor and finally to null. ChatResponse.Builder generates an id when none is set, so a response without one stays usable.
jujn
approved these changes
Oct 3, 2026
This was referenced Oct 5, 2026
chickenlj
pushed a commit
that referenced
this pull request
Nov 13, 2026
Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs. (cherry picked from commit a3798a8) Constraint: No AgentScope Service or SDK migration Confidence: high Not-tested: Whole-repository verification pending after lane integration
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.
AgentScope-Java Version
2.0.4-SNAPSHOT (the
revisionproperty in the root pom; based on 860b928)Description
Background
AnthropicResponseParsercallsMessage.id()directly on both parse paths, and theAnthropic SDK's typed
id()accessor is backed byJsonField.getRequired. Ittherefore throws
AnthropicInvalidDataException: id is not setwhenever the field isabsent -- which is what happens behind a gateway or proxy that strips the top-level
id, as reported in #592.On the streaming path the failure is worse than a rejected response: the
message_startbranch throws before it has seeded the shared usage state, so theprompt and cache-token counts that
message_deltareads back are silently lost aswell.
Changes
AnthropicResponseParsernow resolves the id through oneresolveMessageIdhelpershared by both call sites:
_id()field, and use it when present;id()accessor;null.Returning
nullis safe:ChatResponse.Builder.build()generates an id when none isset, so a response without one stays usable instead of failing the whole call.
The order is deliberate rather than arbitrary. The raw field goes first because it
never throws. The typed accessor has to stay as the second step, because the existing
tests build their messages as Mockito mocks that stub only
id(); a mock's_id()returns
null, so dropping the typed fallback would break those 14 call sites.How to test
Reproduce with the payload from #592 (no top-level
id):Before this change the call throws
AnthropicInvalidDataException: id is not set;after it returns a response with a generated id and the parsed text content.
Three regression tests cover both parse paths:
testParseMessageWithoutIdFallsBackToGeneratedId-- non-streaming, noidtestParseStreamEventMessageStartWithoutIdDoesNotThrow--message_start, noidtestParseMessageReusesRawId-- anidthat is present is reused unchangedI confirmed the first two fail with
AnthropicInvalidDataException: id is not setagainst the current
mainand pass with the fix. The module's suite is green:mvn -pl :agentscope-extensions-model-anthropic testreports 215 tests, 0 failures,0 errors, and
mvn spotless:checkreports 0 files needing changes.Fixes #592
Checklist
mvn spotless:applymvn test)