Skip to content

fix(a2a): convert ToolResultBlock output maps to ContentBlock in DataParser - #3289

Closed
breno-concrete wants to merge 4 commits into
agentscope-ai:mainfrom
breno-concrete:fix/a2a-datapart-toolresult-output
Closed

breno-concrete wants to merge 4 commits into
agentscope-ai:mainfrom
breno-concrete:fix/a2a-datapart-toolresult-output

Conversation

@breno-concrete

Copy link
Copy Markdown

AgentScope-Java Version

2.0.4-SNAPSHOT (main)

Description

Fixes #1336.

Background

In Java-to-Java A2A communication, ToolResultBlock output items arrive in
DataPartParser as JSON maps (LinkedHashMap) after the round trip, not as
ContentBlock instances. The parser cast the list with an unchecked cast, which
succeeds due to type erasure, so the ClassCastException only surfaced later
when a consumer read an element

Changes

  • DataPartParser: convert each output item individually via
    JsonUtils.getJsonCodec().convertValue(item, ContentBlock.class), relying on
    the polymorphic type field declared on ContentBlock.
    • Items that are already ContentBlock are kept as-is.
    • null items are skipped.
  • Removed the @SuppressWarnings("unchecked") cast.

Design note

An item with an unknown type currently throws JsonException (fail fast).
I pinned this behavior with a test; happy to switch to skipping/logging such
items instead if maintainers prefer.

How to test

mvn -pl agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-a2a/agentscope-extensions-a2a-client -am test

New tests in DataPartParserTest:

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 (N/A — no public API change)
  • Code is ready for review

…PartParser

After an A2A round trip, ToolResultBlock output items arrive as JSON maps instead of ContentBlock instances. DataPartParser cast the list with an unchecked cast, so a ClassCastException was thrown later, when a consumer read an element.

Convert each item through the JsonCodec, relying on ContentBlock's polymorphic "type" field; items that are already ContentBlock are kept and null items are skipped.

Fixes agentscope-ai#1336
Copilot AI lite review requested due to automatic review settings September 25, 2026 02:57
@CLAassistant

CLAassistant commented Sep 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved issues were identified that would block approval.

Review effort: Lite
Findings: None

What changed in this PR

Fixes A2A deserialization of ToolResultBlock outputs by converting raw JSON maps into polymorphic ContentBlock instances.

Changes:

  • Converts mixed output lists item-by-item while preserving existing blocks.
  • Skips null items and fails fast on unknown block types.
  • Adds regression tests covering these cases.
File Description
agentscope-extensions/​agentscope-extensions-protocol/​agentscope-extensions-a2a/​agentscope-extensions-a2a-client/​src/​test/​java/​io/​agentscope/​core/​a2a/​agent/​message/​DataPartParserTest.java Updated as part of this pull request.
agentscope-extensions/​agentscope-extensions-protocol/​agentscope-extensions-a2a/​agentscope-extensions-a2a-client/​src/​main/​java/​io/​agentscope/​core/​a2a/​agent/​message/​DataPartParser.java Updated as part of this pull request.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ntscope/core/a2a/agent/message/DataPartParser.java 95.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Nice catch — the unconditional (List<ContentBlock>) output cast was a latent ClassCastException waiting to happen, because JSON round-tripped parts arrive as Maps, not typed blocks. The polymorphic conversion through JsonUtils.getJsonCodec() is the right fix (the shared codec already sets FAIL_ON_UNKNOWN_PROPERTIES=false, so blocks carrying new fields from a newer producer still parse). Tests cover the typed / mixed / null / unknown-type cases, which is the right matrix.

Two resilience notes are pinned inline: one conversion failure currently discards the whole tool result, and a plain List<String> output (valid per the Python contract quoted in this very method) throws instead of degrading to text. Neither is a blocker on its own — flagging them so the behaviour is a deliberate choice rather than an accident.

Head reviewed: 9749a5828f. CI: passing.


Automated review by github-manager-bot

contentBlocks.add(contentBlock);
} else if (item != null) {
contentBlocks.add(JsonUtils.getJsonCodec().convertValue(item, ContentBlock.class));
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Warning] A single unconvertible item aborts the whole ToolResultBlock parse (convertValue throws JsonException for an unknown/missing type, which the new test asserts on). Against a remote A2A agent that is the worst case: one unexpected block in an otherwise valid tool result discards the entire result. Since ContentBlock's subtypes are fixed here (@JsonSubTypes on the type field), consider per-item resilience — catch the conversion failure, log at warn with the offending type, and either skip the item or degrade it to TextBlock (raw JSON). This class currently has no logger at all, so adding one would also make these productions observable. If the hard-fail is deliberate, it deserves a sentence in the javadoc so the next parser change does not "fix" it into a silent skip.

for (Object item : rawList) {
if (item instanceof ContentBlock contentBlock) {
contentBlocks.add(contentBlock);
} else if (item != null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Info] instanceof ContentBlock keeps typed blocks and non-string scalars fall into convertValue. A List<String> output (legal under the Python str | List[TextBlock|ImageBlock|...] contract referenced above, e.g. a producer that emits ["line1", "line2"] after JSON round-trip) will throw JsonException: Cannot deserialize value of type String into ContentBlock. Wrapping CharSequence items as TextBlock.builder().text(...) here is a two-line addition and removes a whole class of cross-language parse failures.

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

The follow-up commit fully addresses the previous round: each ToolResultBlock output item is now converted independently, so one unconvertible value degrades to a raw-JSON TextBlock instead of aborting the whole parse, and the new unknown-type test confirms a later valid block still survives. Re-checked the full diff across correctness, compatibility and test coverage — nothing blocking, nice work.


Automated review by github-manager-bot

@jujn

jujn commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Fixed in: #3590

@jujn jujn closed this Oct 19, 2026
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]:ClassCastException in DataPartParser when deserializing ToolResultBlock in Java-to-Java A2A communication

5 participants