Skip to content

[Bug]:ClassCastException in DataPartParser when deserializing ToolResultBlock in Java-to-Java A2A communication #1336

Closed

Description

@philo-x

[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<LinkedHashMap>. The code then attempts to directly cast this list to List<ContentBlock>, 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

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.

// Current problematic code in DataPartParser.java (line 91 in v1.0.12):
} else if (output instanceof List) {
    @SuppressWarnings("unchecked")
    List<ContentBlock> outputList = (List<ContentBlock>) output;
    builder.output(outputList);
}

// Proposed fix:
} else if (output instanceof List<?> rawList) {
    List<ContentBlock> 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

Activity

  1. added
    bug
    on May 4, 2026
  2. moved this from Backlog to In progress in AgentScope Javaon May 6, 2026
  3. added theissue type on May 11, 2026
  4. added
    area/ext/integration
    on May 28, 2026
  5. breno-concrete commented on Sep 24, 2026

    @breno-concrete

    Hi, I'd like to work on this. I will apply the fix suggested in the issue (convert each item via JsonUtils.getJsonCodec().convertValue() instead of the raw cast) and add a regression test. Opening a PR shortly.

  6. oss-maintainer commented on Sep 24, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed (reviewed against main@3c1c29c01, static code review, no runtime reproduction)

    Root cause. DataPartParser.parseToToolResultBlock still does an unchecked cast of the decoded output list:

    agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-a2a/agentscope-extensions-a2a-client/src/main/java/io/agentscope/core/a2a/agent/message/DataPartParser.java:80-96

    } else if (output instanceof List) {
        @SuppressWarnings("unchecked")
        List<ContentBlock> outputList = (List<ContentBlock>) output;
        builder.output(outputList);
    }

    Because of erasure the cast succeeds for a List<LinkedHashMap> and the ClassCastException only fires when a consumer reads an element as ContentBlock, which matches the reported stack trace.

    The polymorphic path the reporter suggests is available: ToolResultBlock.output is typed List<ContentBlock> (agentscope-core/src/main/java/io/agentscope/core/message/ToolResultBlock.java:40-55), ContentBlock carries @JsonTypeInfo(use=NAME, property="type") + @JsonSubTypes (agentscope-core/src/main/java/io/agentscope/core/message/ContentBlock.java:46-58), and JsonCodec.convertValue handles map → typed conversion (agentscope-core/src/main/java/io/agentscope/core/util/JsonCodec.java:91-102).

    Not already fixed on main. An equivalent per-element conversion exists only in a non-main history (c8797a799 refactor(a2a)!: migrate A2A modules to SDK 1.0.0.Final, contained by remotes/pr/2311), which normalizes through MessageConvertUtil.contentBlockFromProtobufSafeValue. The only main commit touching this file is 3c1c29c01. We could not tie the closed PR #1346 to any commit on main; its behaviour is not present here.

    Impact. Any Java↔Java A2A call that returns a ToolResultBlock with list-typed output through DataPart breaks at stream-consumption time; agentscope-extensions-a2a-client is a published reactor module (agentscope-distribution/agentscope-bom/pom.xml:102-114), so this is not an examples-only path.

    Severity: medium (crash on an integration path, narrow and deterministic fix).

    Suggested fix shape. Iterate List<?>: keep items already instanceof ContentBlock, convert the rest via JsonUtils.getJsonCodec().convertValue(item, ContentBlock.class), and pass the resulting list to the builder — the reporter's snippet matches what the code supports. Add the missing regression case in agentscope-extensions-a2a-client/src/test/java/io/agentscope/core/a2a/agent/message/DataPartParserTest.java (existing coverage at :120-142 only feeds an already-typed List<ContentBlock>, never a list of maps).

    No automated fix PR from us this time. A contributor (@breno-concrete) has already claimed this fix in the thread, so a bot PR would duplicate their work. If no PR lands, reply /approve and we will generate one against the fix shape above.

    Automated evaluation by github-manager-bot.

  7. oss-maintainer commented on Sep 27, 2026

    @oss-maintainer
    Collaborator

    Status: 72 h feedback window expired — no automated PR will be generated, the proposal window here is closed

    The evaluation above (5823097511, posted 2026-09-24T22:13:13Z) reached its 72-hour feedback limit at 2026-09-27T22:13:13Z. Nobody replied in the thread, and no line in it starts with an approve, revise or reject command, so per our workflow we skip PR generation and no competing PR will be filed from this proposal window. No action is needed from anyone.

    The fix path stays community-owned, which was already the case when the evaluation was written. PR #3289 by @breno-concrete (fix(a2a): convert ToolResultBlock output maps to ContentBlock in DataParser) is still OPEN and unmerged at head ebf05d7252 (mergeable_state=clean, last updated 2026-09-25T13:54:24Z), and the earlier attempt PR #1346 by @JGoP-L was closed unmerged on 2026-08-21 at head f256958aaf. Filing a bot PR into the same parser seam now would collide with #3289 rather than add to it.

    Standing this window down is a timing call, not a verdict on the report. The evaluation confirmed the defect: DataPartParser.parseToToolResultBlock still casts the decoded output list to List<ContentBlock> unchecked, so erasure lets a List<LinkedHashMap> through and the ClassCastException surfaces at the first element read — which is what the reporter's stack trace shows. That conclusion is unchanged by the window closing. If a first-party patch is wanted anyway, replying here with a direction (a revise command carrying the chosen converter shape, or an explicit go-ahead) is enough to get a spec and a PR generated against current main; the bot simply stops waiting on this thread.

    Automated by github-manager-bot

  8. moved this from In progress to Done in AgentScope Javaon Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/ext/integrationbug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions