{"record":{"id":"e19dc91ad3cc3a87","repo":"microsoft/autogen","slug":"json-content-must-be-a-dict-e19dc9","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/memory/chromadb/_chromadb.py","lineNumber":298,"sourceCode":"            except Exception as e:\n                logger.error(f\"Failed to get/create collection: {e}\")\n                raise\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    def _calculate_score(self, distance: float) -> float:\n        \"\"\"Convert ChromaDB distance to a similarity score.\"\"\"\n        if self._config.distance_metric == \"cosine\":\n            return 1.0 - (distance / 2.0)\n        return 1.0 / (1.0 + distance)\n\n    async def update_context(\n        self,\n        model_context: ChatCompletionContext,\n    ) -> UpdateContextResult:\n        messages = await model_context.get_messages()\n        if not messages:\n            return UpdateContextResult(memories=MemoryQueryResult(results=[]))","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/memory/chromadb/_chromadb.py#L280-L316","documentation":"ChromaDBVectorMemory's internal text converter (used to turn MemoryContent into text for embedding/storage) accepts JSON mime type only when the inner content is a dict. A JSON content that is a list, string, or scalar raises this ValueError before the item is embedded.","triggerScenarios":"Calling memory.add() / update_context() with MemoryContent(content=<list or str>, mime_type=MemoryMimeType.JSON); the converter runs during ingestion and rejects non-dict JSON.","commonSituations":"Storing JSON arrays or pre-stringified JSON in ChromaDB memory; upstream producers emitting list payloads; reusing the same content objects across memory backends with mismatched expectations.","solutions":["Wrap list/scalar payloads in a dict before creating MemoryContent.","Use MemoryMimeType.TEXT for already-serialized JSON strings.","Validate content shape at the point where MemoryContent objects are constructed."],"exampleFix":"# before\nawait memory.add(MemoryContent(content=\"[1, 2, 3]\", mime_type=MemoryMimeType.JSON))\n# after\nawait memory.add(MemoryContent(content={\"items\": [1, 2, 3]}, mime_type=MemoryMimeType.JSON))","handlingStrategy":"type-guard","validationCode":"from autogen_core.memory import MemoryContent, MemoryMimeType\nif c.mime_type == MemoryMimeType.JSON:\n    assert isinstance(c.content, dict), \"ChromaDB memory JSON content must be a dict\"\nawait memory.add(c)","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":["Validate MemoryContent shape at construction, not at memory.add time.","Wrap JSON arrays in a dict and use TEXT mime for serialized strings."],"tags":["autogen","chromadb","memory","validation","json"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}