Skip to content

fix: preserve JSON-native types in A2A metadata serialization#5181

Open
YizukiAme wants to merge 1 commit intogoogle:mainfrom
YizukiAme:clean/a2a-metadata-serialization
Open

fix: preserve JSON-native types in A2A metadata serialization#5181
YizukiAme wants to merge 1 commit intogoogle:mainfrom
YizukiAme:clean/a2a-metadata-serialization

Conversation

@YizukiAme
Copy link
Copy Markdown

@YizukiAme YizukiAme commented Apr 7, 2026

Fixes #5183

Problem

_serialize_value() in from_adk_event.py falls through to str() for JSON-native Python types (dict, list, bool, int, float), corrupting structured metadata values into their string representations during A2A event conversion.

For example, a custom_metadata dict like {"retry": True, "max_attempts": 3} gets serialized as "{'retry': True, 'max_attempts': 3}" — a Python repr string instead of a proper JSON object.

Fix

  • Preserve JSON-native types directly: dict, list, str, bool, int, float are now returned as-is since they are already valid JSON values.
  • JSON normalization fallback: For other JSON-serializable objects (e.g., custom classes with __dict__), normalize via json.loads(json.dumps(value)) before falling back to str().
  • Added regression test for structured custom_metadata round-trip to ensure values are not corrupted to strings.

Logs After Fix

$ uv run pytest tests/unittests/a2a/converters/test_event_round_trip.py -v

tests/unittests/a2a/converters/test_event_round_trip.py::test_custom_metadata_structured_roundtrip PASSED

========== 143 passed, 124 warnings in 9.12s ==========

Testing Plan

uv run pytest tests/unittests/a2a/converters/ -v

All 143 A2A converter tests pass. The new test_custom_metadata_structured_roundtrip test verifies that dict, list, bool, int, and float values survive the _serialize_value() → metadata round-trip without being corrupted to strings.

Signed-off-by: Yizuki_Ame <yinzimike@gmail.com>
@YizukiAme YizukiAme marked this pull request as draft April 7, 2026 07:54
@YizukiAme YizukiAme marked this pull request as ready for review April 7, 2026 08:04
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.

A2A _serialize_value() corrupts JSON-native metadata types to strings

1 participant