microsoft/autogen · error · ValueError

JSON content must be a dict

Error message

JSON content must be a dict

What it means

Teachability's text-extraction helper only converts MemoryContent items with JSON mime type when the inner content is a dict. If content is a list, str, or scalar while mime_type is MemoryMimeType.JSON, it raises this ValueError instead of silently coercing.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/experimental/task_centric_memory/utils/teachability.py:49

    def name(self) -> str:
        """Get the memory instance identifier."""
        return self._name

    def _extract_text(self, content_item: str | MemoryContent) -> str:
        """Extract searchable text from content."""
        if isinstance(content_item, str):
            return content_item

        content = content_item.content
        mime_type = content_item.mime_type

        if mime_type in [MemoryMimeType.TEXT, MemoryMimeType.MARKDOWN]:
            return str(content)
        elif mime_type == MemoryMimeType.JSON:
            if isinstance(content, dict):
                # Store original JSON string representation
                return str(content).lower()
            raise ValueError("JSON content must be a dict")
        elif isinstance(content, Image):
            raise ValueError("Image content cannot be converted to text")
        else:
            raise ValueError(f"Unsupported content type: {mime_type}")

    async def update_context(
        self,
        model_context: ChatCompletionContext,
    ) -> UpdateContextResult:
        """
        Extracts any advice from the last user turn to be stored in memory,
        and adds any relevant memories to the model context.
        """
        self._logger.enter_function()

        # Extract text from the user's last message
        messages = await model_context.get_messages()
        if not messages:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Wrap non-dict JSON payloads in a dict, e.g. {"items": [...]}, before creating MemoryContent.
  2. If the content is a plain string, use MemoryMimeType.TEXT or MARKDOWN instead of JSON.
  3. Validate content type against mime_type at MemoryContent creation time.

Example fix

# before
MemoryContent(content=["adv1", "adv2"], mime_type=MemoryMimeType.JSON)
# after
MemoryContent(content={"items": ["adv1", "adv2"]}, mime_type=MemoryMimeType.JSON)
Defensive patterns

Strategy: type-guard

Validate before calling

from autogen_core.memory import MemoryContent, MemoryMimeType
assert not (c.mime_type == MemoryMimeType.JSON and not isinstance(c.content, dict)), "JSON memory content must wrap a dict"

Type guard

def is_valid_json_memory_content(c) -> bool:
    if c.mime_type != MemoryMimeType.JSON:
        return True
    return isinstance(c.content, dict)

Prevention

When it happens

Trigger: Building MemoryContent(content=[1,2,3], mime_type=MemoryMimeType.JSON) or MemoryContent(content="{'a':1}", mime_type=MemoryMimeType.JSON) and passing it to the teachability memory, which calls the text converter during update_context.

Common situations: Storing JSON arrays or pre-serialized JSON strings in memory; upstream code producing list-typed JSON payloads; mistakenly setting mime_type=JSON for plain strings.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/1c301cc254e9c82d. Report an issue: GitHub.