Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import Any, Dict, List, Union

from pydantic import BaseModel, ConfigDict, field_serializer
from pydantic import BaseModel, ConfigDict, field_serializer, field_validator

from .._cancellation_token import CancellationToken
from .._component_config import ComponentBase
Expand Down Expand Up @@ -44,6 +44,24 @@ def serialize_mime_type(self, mime_type: MemoryMimeType | str) -> str:
return mime_type.value
return mime_type

@field_validator("mime_type", mode="before")
@classmethod
def _coerce_mime_type(cls, v: object) -> object:
"""Restore a serialized string back to its :class:`MemoryMimeType`.

``dump_component()`` serializes an enum member to its string value, so a
``load_component()`` roundtrip used to leave ``mime_type`` as a plain
``str``, changing behaviour for code that dispatches on the enum. A
string that matches an enum value is coerced back; other custom MIME
type strings stay strings.
"""
if isinstance(v, str):
try:
return MemoryMimeType(v)
except ValueError:
return v
return v


class MemoryQueryResult(BaseModel):
"""Result of a memory :meth:`~autogen_core.memory.Memory.query` operation."""
Expand Down
23 changes: 23 additions & 0 deletions python/packages/autogen-core/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ def test_memory_component_dump_config_to_base_model() -> None:
assert len(config.config["memory_contents"]) == 1


def test_memory_mime_type_roundtrip() -> None:
"""A dump/load roundtrip restores the enum, not a plain str (#8293)."""
memory = ListMemory(
name="test_memory",
memory_contents=[MemoryContent(content="hello", mime_type=MemoryMimeType.MARKDOWN)],
)
config = memory.dump_component()

loaded = ListMemory.load_component(config)
assert loaded.content[0].mime_type is MemoryMimeType.MARKDOWN


def test_memory_mime_type_custom_string_stays_str() -> None:
"""A custom MIME type that is not an enum value stays a string."""
memory = ListMemory(
name="test_memory",
memory_contents=[MemoryContent(content="hello", mime_type="application/custom")],
)
loaded = ListMemory.load_component(memory.dump_component())
assert loaded.content[0].mime_type == "application/custom"
assert not isinstance(loaded.content[0].mime_type, MemoryMimeType)


def test_memory_abc_implementation() -> None:
"""Test that Memory ABC is properly implemented."""

Expand Down