Skip to content

fix(core): restore MemoryMimeType enum on mime_type after component roundtrip - #8295

Open
JingHao-Leon wants to merge 1 commit into
microsoft:mainfrom
JingHao-Leon:jinghao/memory-mime-type-enum-roundtrip
Open

JingHao-Leon wants to merge 1 commit into
microsoft:mainfrom
JingHao-Leon:jinghao/memory-mime-type-enum-roundtrip

Conversation

@JingHao-Leon

Copy link
Copy Markdown

What happened?

MemoryContent.mime_type accepts MemoryMimeType | str and serializes enum values to their string form via field_serializer (added in #5688 to fix "Object of type MemoryMimeType is not JSON serializable"). Nothing restored the enum on the way back in, so after a dump_component() / load_component() roundtrip (e.g. saving and restoring a ListMemory), mime_type degenerated to a plain str:

mem = ListMemory(name="t")
await mem.add(MemoryContent(content="hello", mime_type=MemoryMimeType.MARKDOWN))
config = mem.dump_component()          # 'text/markdown'
loaded = ListMemory.load_component(config)
loaded._contents[0].mime_type == MemoryMimeType.MARKDOWN   # False (was str)

Any code that dispatches on content.mime_type == MemoryMimeType.TEXT (user code, or autogen_ext memory implementations) works before a save/load and silently changes behavior after it.

Fixes #8293 (which has the full repro and version details).

Are you a new contributor?

How did you test it?

  • New regression test test_list_memory_mime_type_roundtrip_restores_enum in python/packages/autogen-core/tests/test_memory.py: dumps a ListMemory holding a MemoryMimeType.MARKDOWN item, loads it back, asserts the enum member is restored and that a custom MIME string stays str.
  • pytest tests/test_memory.py → 11 passed (10 existing + 1 new) with the fix; the new test fails on main without it.
  • Repro from [Bug]: MemoryContent.mime_type enum degenerates to str after dump/load roundtrip (ListMemory) #8293 now prints MemoryMimeType with equality True.

…oundtrip

MemoryContent serializes mime_type enum values to their string form (added in microsoft#5688 to fix JSON serialization), but nothing restored the enum on the way back in. After a dump_component()/load_component() roundtrip (e.g. saving and restoring a ListMemory), mime_type degenerated from MemoryMimeType.MARKDOWN to the plain string 'text/markdown', so equality comparisons against the enum flipped to False and any code dispatching on mime_type silently changed behavior after a save/load.

Add a before-validator that coerces strings matching a MemoryMimeType value back to the enum member, leaving custom MIME-type strings as str. Regression test included.

Fixes microsoft#8293

This branch has not been deployed

No deployments
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]: MemoryContent.mime_type enum degenerates to str after dump/load roundtrip (ListMemory)

1 participant