Repository navigation
fix(core): preserve bytes in MemoryContent across JSON round-trip - #8482
Open
AILICute (lkuko) wants to merge 1 commit into
Open
AILICute (lkuko) wants to merge 1 commit into
AILICute (lkuko) wants to merge 1 commit into
Conversation
MemoryContent.content is typed as Union[str, bytes, Dict[str, Any], Image], but only mime_type had a custom serializer. In JSON mode pydantic UTF-8 decodes bytes into str (silently changing the type) or raises UnicodeDecodeError for non-UTF-8 data (e.g. PNG headers). Add a when_used="json" field serializer that encodes bytes as a base64 data URI, and a mode="before" field validator that restores them, so bytes content survives JSON serialization/deserialization losslessly. Other content types (str, dict, Image) are unchanged.
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
MemoryContent.contentis typed asUnion[str, bytes, Dict[str, Any], Image], andMemoryMimeType.BINARYexists for binary payloads. However, onlymime_typehad a custom serializer. In pydantic's JSON mode,bytesare UTF-8 decoded intostr, which means:b"\x89PNG\r\n\x1a\n") raiseUnicodeDecodeErroronmodel_dump(mode="json")/model_dump_json()/BaseMessage.dump()/team.save_state()— a hard crash.b"hello") are silently converted tostr, so the type is lost on load:model_validate(dumped).contentis"hello", notb"hello".The
Imagemember already has its own base64 (de)serialization, sobytesbeing left out is an asymmetry rather than an intentional design. This matters for the JSON persistence path thatMemoryContentis meant to support (see the earlier serialization work onmime_type).Reproduction on
main:What changed
when_used="json"field_serializerforcontentthat encodesbytesas a base64 data URI (data:application/octet-stream;base64,...). Other content types (str,dict,Image) are returned unchanged and serialized by pydantic as usual.mode="before"field_validatorthat restores those data URIs back tobyteson load, so JSON round-trips are lossless.model_dump()(non-JSON) still returns rawbytes, so existing in-memory behavior is unchanged.Checks
Tests: added
test_memory_content_bytes_json_roundtripinpython/packages/autogen-core/tests/test_memory.py, covering non-UTF-8 bytes (no crash, round-trips asbytes), UTF-8 bytes (no silent coercion tostr), unchangedstr/dictcontent, and unchanged python-mode dump.Local verification:
pytest python/packages/autogen-core/tests/test_memory.py -q→ 11 passed (10 existing + 1 new); without the fix the new test fails withUnicodeDecodeError/AssertionError.ruff checkon both changed files → clean.mypy(repositorypyproject.tomlstrict config) on_base_memory.py→ Success: no issues found.Related issue number
N/A — found while auditing JSON round-trip behavior of
MemoryContent.