Skip to content

Fix MemoryContent.mime_type degenerating to str after dump/load roundtrip - #8379

Closed
zhouh (OUAO-FRANK) wants to merge 1 commit into
microsoft:mainfrom
OUAO-FRANK:fix/8293-mime-type-roundtrip
Closed

zhouh (OUAO-FRANK) wants to merge 1 commit into
microsoft:mainfrom
OUAO-FRANK:fix/8293-mime-type-roundtrip

Conversation

@OUAO-FRANK

Copy link
Copy Markdown

Why are these changes needed?

MemoryContent.mime_type accepts MemoryMimeType | str, and a field_serializer writes an enum member as its string value. There was no matching validator on the way back in, so after a dump_component() / load_component() roundtrip (e.g. saving and restoring a ListMemory) the enum degenerated into a plain str. Any code that dispatches on content.mime_type is MemoryMimeType.X silently changed behaviour after a save/load.

This adds a field_validator(mode="before") that coerces a string matching an enum value back to MemoryMimeType, while leaving arbitrary custom MIME-type strings as str.

Related issue number

Closes #8293

Checks

Testing

Added test_memory_mime_type_roundtrip and test_memory_mime_type_custom_string_stays_str to python/packages/autogen-core/tests/test_memory.py. The roundtrip test fails on the previous code ('text/markdown' vs the enum) and passes now; the custom-string test guards against over-coercing unknown MIME types.

$ pytest tests/test_memory.py -q
12 passed

ruff check and ruff format --check (v0.4.8) are clean on the two changed files.

…trip

MemoryContent serializes an enum mime_type to its string value, but had no
inverse validator, so a ListMemory dump_component()/load_component() roundtrip
left mime_type as a plain str. Code that dispatches on
`content.mime_type is MemoryMimeType.X` silently changed behaviour after a
save/load. Coerce a string matching an enum value back to MemoryMimeType on
validation; other custom MIME-type strings stay strings.
@OUAO-FRANK

Copy link
Copy Markdown
Author

Closing this myself: I opened it before noticing the project is archived and no longer maintained (README points to microsoft/agent-framework as the successor). No point adding to an unmaintained repo. Sorry for the noise.

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