agentscope-ai / agentscope-ai/agentscope-java
[Bug]:ClassCastException in DataPartParser when deserializing ToolResultBlock in Java-to-Java A2A communication
- Lingua principale
- Java
- Stelle
- 5.6k
- Fork
- 1.3k
- Merge medio
- 4g 12h
- PR unite (30g)
- 77
Descrizione
### [Bug]: ClassCastException in DataPartParser when deserializing ToolResultBlock in Java-to-Java A2A communication
**Describe the bug**
In a Java-to-Java A2A (Agent-to-Agent) communication scenario, the `DataPartParser` fails with a `ClassCastException` when processing a message that contains a `ToolResultBlock`.
The root cause is that when `DataPartParser` deserializes the `output` field of a `ToolResultBlock` from JSON, it initially becomes a `List`. The code then attempts to directly cast this list to `List`, which is invalid in Java and triggers the exception during stream processing or access.
**To Reproduce**
Steps to reproduce the behavior:
1. Create a Supervisor agent and a Sub-agent, both using the Java SDK.
2. Set up A2A communication between them (e.g., via Nacos or direct gRPC).
3. In the Sub-agent, use a `ReActAgent` and configure `StreamOptions` to include `EventType.TOOL_RESULT`.
4. Trigger a tool call in the Sub-agent that returns content.
5. The Supervisor agent will crash with a `ClassCastException` upon receiving and parsing the sub-agent's response.
**Expected behavior**
The `DataPartParser` should correctly deserialize the list of maps into a list of `ContentBlock` objects by leveraging the polymorphic deserialization capabilities of the `JsonCodec` (Jackson), respecting the `@JsonTypeInfo` defined in `ContentBlock`.
**Error messages/Stack trace**
```text
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class io.agentscope.core.message.ContentBlock
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178)
at java.base/java.util.AbstractList$RandomAccessSpliterator.forEachRemaining(AbstractList.java:722)
at io.agentscope.core.a2a.agent.message.DataPartParser.parseToToolResultBlock(DataPartParser.java:91)
```
**Proposed Fix**
Modify `DataPartParser.java` to perform explicit conversion of each list item using the project's global `JsonCodec` instead of a direct raw cast.
```java
// Current problematic code in DataPartParser.java (line 91 in v1.0.12):
} else if (output instanceof List) {
@SuppressWarnings("unchecked")
List outputList = (List) output;
builder.output(outputList);
}
// Proposed fix:
} else if (output instanceof List rawList) {
List contentBlocks = new ArrayList<>(rawList.size());
for (Object item : rawList) {
if (item instanceof ContentBlock cb) {
contentBlocks.add(cb);
} else if (item != null) {
// Leverage Jackson's polymorphic deserialization based on the 'type' field
ContentBlock cb = JsonUtils.getJsonCodec().convertValue(item, ContentBlock.class);
if (cb != null) {
contentBlocks.add(cb);
}
}
}
builder.output(contentBlocks);
}
```
**System Information:**
- **AgentScope-Java Version**: v1.0.12
- **Environment**: Java-to-Java A2A communication
- **Component**: agentscope-extensions-a2a-client
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.