Skip to content

[Bug]: MemoryContent.mime_type enum degenerates to str after dump/load roundtrip (ListMemory) #8293

Description

@JingHao-Leon

What happened?

MemoryContent.mime_type accepts MemoryMimeType | str, and serializes any enum value to its string form via a field_serializer. There is no inverse validator on the way back in, so after a dump_component() / load_component() roundtrip (e.g. saving and restoring a ListMemory), the enum degenerates into a plain str. Any code that dispatches on content.mime_type == MemoryMimeType.TEXT (or is MemoryMimeType.X) silently changes behavior after a save/load.

This is a follow-on from #5688: the serializer added there fixed the "not JSON serializable" error but did not restore the enum on load.

Autogen-core code itself currently doesn't compare mime_type against the enum, but user code and autogen_ext memory implementations (e.g. semantic/chroma-based memories) that filter or dispatch by MIME type do — and they work before the roundtrip and break after it.

Repro

import asyncio
from autogen_core.memory import ListMemory, MemoryContent, MemoryMimeType

async def main():
    mem = ListMemory(name="t")
    await mem.add(MemoryContent(content="hello", mime_type=MemoryMimeType.MARKDOWN))

    cfg = mem.dump_component()
    print(repr(cfg.config["memory_contents"][0]["mime_type"]))   # 'text/markdown'

    loaded = ListMemory.load_component(cfg)
    print(type(loaded._contents[0].mime_type).__name__)          # str
    print(loaded._contents[0].mime_type == MemoryMimeType.MARKDOWN)  # False

asyncio.run(main())

Observed on autogen-core 0.7.5 and confirmed unfixed on main (python/packages/autogen-core/src/autogen_core/memory/_base_memory.py, the MemoryContent model has serialize_mime_type but no matching validator).

Expected behavior

mime_type should round-trip: if the serialized string matches a MemoryMimeType value, loading should restore the enum member, so loaded._contents[0].mime_type == MemoryMimeType.MARKDOWN holds after save/load.

Suggested fix

A small field_validator(mode="before") on mime_type that coerces a string matching an enum value back to MemoryMimeType (leaving arbitrary custom MIME-type strings as str):

@field_validator("mime_type", mode="before")
@classmethod
def _coerce_mime_type(cls, v: object) -> object:
    if isinstance(v, str):
        try:
            return MemoryMimeType(v)
        except ValueError:
            return v
    return v

Version

  • autogen-core 0.7.5 (also reproduced against main source as of 2026-09-29)
  • Python 3.13.9, macOS arm64

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions