Skip to content

fix(anthropic): tolerate a missing message id in responses - #3394

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/anthropic-proxy-missing-id
Oct 3, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/anthropic-proxy-missing-id

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.4-SNAPSHOT (the revision property in the root pom; based on 860b928)

Description

Background

AnthropicResponseParser calls Message.id() directly on both parse paths, and the
Anthropic SDK's typed id() accessor is backed by JsonField.getRequired. It
therefore throws AnthropicInvalidDataException: id is not set whenever the field is
absent -- 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_start branch throws before it has seeded the shared usage state, so the
prompt and cache-token counts that message_delta reads back are silently lost as
well.

Changes

AnthropicResponseParser now resolves the id through one resolveMessageId helper
shared by both call sites:

  1. read the SDK's raw _id() field, and use it when present;
  2. otherwise fall back to the typed id() accessor;
  3. otherwise return null.

Returning null is safe: ChatResponse.Builder.build() generates an id when none is
set, 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):

String json =
        "{\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-sonnet-4-5\","
                + "\"content\":[{\"type\":\"text\",\"text\":\"2 + 3 = 5\"}],"
                + "\"usage\":{\"input_tokens\":10,\"output_tokens\":5}}";
Message message = ObjectMappers.jsonMapper().readValue(json, Message.class);
ChatResponse response = AnthropicResponseParser.parseMessage(message, Instant.now());

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, no id
  • testParseStreamEventMessageStartWithoutIdDoesNotThrow -- message_start, no id
  • testParseMessageReusesRawId -- an id that is present is reused unchanged

I confirmed the first two fail with AnthropicInvalidDataException: id is not set
against the current main and pass with the fix. The module's suite is green:
mvn -pl :agentscope-extensions-model-anthropic test reports 215 tests, 0 failures,
0 errors, and mvn spotless:check reports 0 files needing changes.

Fixes #592

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.) -- not applicable, no user-facing docs affected
  • Code is ready for review

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
jujn merged commit a3798a8 into agentscope-ai:main Oct 3, 2026
3 of 5 checks passed
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
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.

[Bug]:Anthropic SDK throws AnthropicInvalidDataException when using proxy services that strip the id field

2 participants