agentscope-ai / agentscope-ai/agentscope-java

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

Abierto
#592 2 comentarios 0 reacciones 0 asignados Ver en GitHub
area/core/model bug
Lenguaje dominante
Java
Estrellas
5.6k
Forks
1.3k
Merge medio
4 d 12 h
PR fusionados (30 d)
77

Descripción

## 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

1. Configure `AnthropicChatModel` with a proxy URL:
```java
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();
```

2. Execute the code

3. See error: `AnthropicInvalidDataException: id is not set`

### Curl Command (Alternative Reproduction)

```bash
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)

```json
{
"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

1. **Fix in Anthropic SDK (Recommended)**: Make the `id` field optional in the SDK's `Message` class instead of required.

2. **Fix in AgentScope Response Parser**: Modify `AnthropicResponseParser.parseMessage()` to use a custom ObjectMapper that can deserialize responses without the `id` field.

3. **Proxy-Friendly Configuration**: Add a configuration option to `AnthropicChatModel.Builder` to generate fallback IDs for responses missing the `id` field.

### Note

This issue is specific to proxy services that modify Anthropic API responses. The official Anthropic API always includes the `id` field.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.