Repository navigation
Description
Describe the bug
When using AnthropicChatModel with a proxy service (anthropic proxy api) that forwards Anthropic API responses, the SDK throws AnthropicInvalidDataException: id is not set.
This issue occurs because some proxy services strip the id field from the response body for privacy reasons.
To Reproduce
Steps
- Configure
AnthropicChatModelwith a proxy URL:
AnthropicChatModel model = AnthropicChatModel.builder()
.apiKey("sk-ant-...") // Your API key
.modelName("claude-sonnet-4-5-20250929")
.baseUrl("Anthropic Proxy URL") // Proxy URL
.stream(false)
.build();
ReActAgent agent = ReActAgent.builder()
.name("TestAgent")
.sysPrompt("You are a math assistant.")
.model(model)
.build();
Msg response = agent.call(Msg.builder()
.textContent("2+3等于多少?")
.build()).block();-
Execute the code
-
See error:
AnthropicInvalidDataException: id is not set
Curl Command (Alternative Reproduction)
curl -X POST "https://anthropicProxyApi/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: sk-ant-YOUR_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-dangerous-direct-browser-access: true" \
-d '{
"model": "claude-sonnet-4-5-20250929",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "2+3等于多少?"
}
]
}'Expected behavior
The SDK should handle responses that are missing the id field gracefully. The id field is important for tracking but not critical for response parsing.
Error messages
com.anthropic.errors.AnthropicInvalidDataException: `id` is not set
at com.anthropic.core.JsonField.getRequired$anthropic_java_core(Values.kt:174)
at com.anthropic.models.messages.Message.id(Message.kt:68)
at io.agentscope.core.formatter.anthropic.AnthropicResponseParser.parseMessage(AnthropicResponseParser.java:97)
at io.agentscope.core.formatter.anthropic.AnthropicChatFormatter.parseResponse(AnthropicChatFormatter.java:47)
at io.agentscope.core.model.AnthropicChatModel.lambda$doStream$1(AnthropicChatModel.java:194)
Response from Proxy (missing id field)
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "2 + 3 = 5"
}
],
"model": "claude-sonnet-4-5-20250929",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 57,
"output_tokens": 8
}
}Environment
- AgentScope-Java Version: 1.0.7
- Java Version: 17+
- OS: macOS
Additional context
Root Cause
The Anthropic SDK 2.11.0 requires the id field in the message response and validates it with @Required annotation during deserialization.
Affected Components
AnthropicChatModel(io.agentscope.core.model.AnthropicChatModel)AnthropicResponseParser(io.agentscope.core.formatter.anthropic.AnthropicResponseParser)
Behavior Comparison
| Scenario | Current Behavior | Expected Behavior |
|---|---|---|
Proxy without id field |
❌ Throws exception | ✅ Should handle gracefully |
| Official Anthropic API | ❓ Not tested yet | ✅ Should work |
Suggested Fixes
-
Fix in Anthropic SDK (Recommended): Make the
idfield optional in the SDK'sMessageclass instead of required. -
Fix in AgentScope Response Parser: Modify
AnthropicResponseParser.parseMessage()to use a custom ObjectMapper that can deserialize responses without theidfield. -
Proxy-Friendly Configuration: Add a configuration option to
AnthropicChatModel.Builderto generate fallback IDs for responses missing theidfield.
Note
This issue is specific to proxy services that modify Anthropic API responses. The official Anthropic API always includes the id field.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
Alexxigang commentedon Apr 7, 2026
I traced this to AnthropicResponseParser: both the non-streaming path and the message-start streaming path call message.id() directly, so proxy responses that omit id fail before we can build a ChatResponse. I'm working on a fix that reads the SDK's raw _id() field safely, falls back when it is missing, and adds regression tests for both parse paths.
Alexxigang commentedon Apr 7, 2026
Opened a fix here: #1156
It makes the Anthropic parser tolerate proxy responses that omit the top-level id field, reuses the SDK raw _id() value when present, and adds parser/formatter regression coverage for non-streaming and message-start streaming paths.