Repository navigation
Python: fix(serialization): serialize tuple values to JSON-compatible lists in _serialize_value - #10374
Python: fix(serialization): serialize tuple values to JSON-compatible lists in _serialize_value#10374Hardik K (hardikkaurani) wants to merge 6 commits into
Conversation
…n _serialize_value
|
Hi Eduard van Valkenburg (@eavanvalkenburg), Could you please take a look when you have a moment? This PR addresses an issue where tuple attributes and tuple elements within containers were silently omitted by _serialize_value during SerializationMixin.to_dict() because only list was handled in sequence serialization. The fix extends sequence checks to (list, tuple) so tuples are consistently serialized to JSON-compatible lists, matching the behavior in make_json_safe and _sessions.py. All unit tests and lint checks are passing locally. Thank you for your time and feedback! |
Nick (surlymochan)
left a comment
There was a problem hiding this comment.
Verified the tuple handling end to end and the tests cover the shapes that matter (flat, nested, empty/single-element, mixed list-of-tuples and tuple-of-lists). Two questions, both about the round trip rather than the serialization itself:
to_dict()now emits tuples as JSON lists, butfrom_dict()restores them as lists, so a tuple attribute does not survive a round trip (coordinatescomes back alist, not atuple). Consumers doingisinstance(x, tuple)or unpacking with tuple-only expectations would see a type change after a save/load cycle. Is list-only round-tripping the accepted contract here, or shouldfrom_dictrestore tuples where the model declares tuple fields? If it is accepted, a one-line note in the docstring would save the next reader the same question.- The circular-reference guard keys on
id()of the container. That is safe while the container is alive on the recursion stack, and the new tests show the guard still trips correctly — but a tuple can (transitively) contain itself via a mutable element, e.g.t = ([],); t[0].append(t). Is that case covered anywhere, or worth a regression test to pin theValueErrorrather than aRecursionError?
Non-blocking: the change is minimal and the added tests are well targeted.
|
Thanks for the great feedback and for digging into these details!
Really appreciate the thoughtful review and sanity check! |
Motivation & Context
In Python,
SerializationMixinobjects serialize their attributes to JSON-compatible dictionaries viato_dict(). However,_serialize_valuepreviously checked onlyif isinstance(value, list):when handling sequences. Becausetupleis not included in_DIRECT_JSON_TYPESor_JSON_SCALAR_TYPES, any attribute or container item typed as atuple(e.g.,coordinates: tuple[float, float]ortags: tuple[str, ...]) was treated as non-serializable and dropped (_SKIP_SERIALIZATION), silently omitting those fields during serialization.This PR extends sequence serialization in
_serialize_valueto supporttuplealongsidelist, converting tuple values into JSON-compatible lists consistently withmake_json_safeand_sessions.py.Description & Review Guide
_serialize_valueinpackages/core/agent_framework/_serialization.pyto accept(list, tuple)and cast tolist[Any] | tuple[Any, ...].test_serializable_mixin.pycovering tuple model attributes, nested tuples within lists and dictionaries, empty and single-element tuples, and mixed sequence structures.SerializationMixinare now correctly serialized into JSON-compatible lists instead of being silently dropped.Related Issue
N/A - trivial change: Resolves silent omission of tuple attributes during
SerializationMixin.to_dict()serialization.AI Assistance
AI assistance details: Used AI assistance for drafting test cases and verification.
Contribution Checklist