Repository navigation
fix(a2a): convert ToolResultBlock output maps to ContentBlock in DataParser - #3289
breno-concrete wants to merge 4 commits into
Conversation
…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
There was a problem hiding this comment.
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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
oss-maintainer
left a comment
There was a problem hiding this comment.
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)); | ||
| } |
There was a problem hiding this comment.
[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) { |
There was a problem hiding this comment.
[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
left a comment
There was a problem hiding this comment.
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
|
Fixed in: #3590 |
AgentScope-Java Version
2.0.4-SNAPSHOT (main)
Description
Fixes #1336.
Background
In Java-to-Java A2A communication,
ToolResultBlockoutput items arrive inDataPartParseras JSON maps (LinkedHashMap) after the round trip, not asContentBlockinstances. The parser cast the list with an unchecked cast, whichsucceeds due to type erasure, so the
ClassCastExceptiononly surfaced laterwhen a consumer read an element
Changes
DataPartParser: convert each output item individually viaJsonUtils.getJsonCodec().convertValue(item, ContentBlock.class), relying onthe polymorphic
typefield declared onContentBlock.ContentBlockare kept as-is.nullitems are skipped.@SuppressWarnings("unchecked")cast.Design note
An item with an unknown
typecurrently throwsJsonException(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:TextBlock(regression for [Bug]:ClassCastException in DataPartParser when deserializing ToolResultBlock in Java-to-Java A2A communication #1336)nullitem → skippedtype→JsonExceptionChecklist
mvn spotless:applymvn test)