{"record":{"id":"1c301cc254e9c82d","repo":"microsoft/autogen","slug":"json-content-must-be-a-dict","errorCode":null,"errorMessage":"JSON content must be a dict","messagePattern":"JSON content must be a dict","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/experimental/task_centric_memory/utils/teachability.py","lineNumber":49,"sourceCode":"    def name(self) -> str:\n        \"\"\"Get the memory instance identifier.\"\"\"\n        return self._name\n\n    def _extract_text(self, content_item: str | MemoryContent) -> str:\n        \"\"\"Extract searchable text from content.\"\"\"\n        if isinstance(content_item, str):\n            return content_item\n\n        content = content_item.content\n        mime_type = content_item.mime_type\n\n        if mime_type in [MemoryMimeType.TEXT, MemoryMimeType.MARKDOWN]:\n            return str(content)\n        elif mime_type == MemoryMimeType.JSON:\n            if isinstance(content, dict):\n                # Store original JSON string representation\n                return str(content).lower()\n            raise ValueError(\"JSON content must be a dict\")\n        elif isinstance(content, Image):\n            raise ValueError(\"Image content cannot be converted to text\")\n        else:\n            raise ValueError(f\"Unsupported content type: {mime_type}\")\n\n    async def update_context(\n        self,\n        model_context: ChatCompletionContext,\n    ) -> UpdateContextResult:\n        \"\"\"\n        Extracts any advice from the last user turn to be stored in memory,\n        and adds any relevant memories to the model context.\n        \"\"\"\n        self._logger.enter_function()\n\n        # Extract text from the user's last message\n        messages = await model_context.get_messages()\n        if not messages:","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/experimental/task_centric_memory/utils/teachability.py#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Wrap non-dict JSON payloads in a dict, e.g. {\"items\": [...]}, before creating MemoryContent.","If the content is a plain string, use MemoryMimeType.TEXT or MARKDOWN instead of JSON.","Validate content type against mime_type at MemoryContent creation time."],"exampleFix":"# before\nMemoryContent(content=[\"adv1\", \"adv2\"], mime_type=MemoryMimeType.JSON)\n# after\nMemoryContent(content={\"items\": [\"adv1\", \"adv2\"]}, mime_type=MemoryMimeType.JSON)","handlingStrategy":"type-guard","validationCode":"from autogen_core.memory import MemoryContent, MemoryMimeType\nassert not (c.mime_type == MemoryMimeType.JSON and not isinstance(c.content, dict)), \"JSON memory content must wrap a dict\"","typeGuard":"def is_valid_json_memory_content(c) -> bool:\n    if c.mime_type != MemoryMimeType.JSON:\n        return True\n    return isinstance(c.content, dict)","tryCatchPattern":null,"preventionTips":["Wrap JSON arrays in {\"items\": [...]} at construction time.","Use TEXT/Markdown mime for pre-serialized JSON strings.","Centralize MemoryContent construction behind a helper that validates shape."],"tags":["autogen","memory","validation","json"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}