{"record":{"id":"fe40c0d917f45f39","repo":"microsoft/autogen","slug":"query-must-be-either-a-string-or-memorycontent","errorCode":null,"errorMessage":"'query' must be either a string or MemoryContent","messagePattern":"'query' must be either a string or MemoryContent","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/memory/redis/_redis_memory.py","lineNumber":320,"sourceCode":"            results = self.message_history.get_recent(\n                top_k=top_k,\n                raw=False,\n            )\n        else:\n            # get the query string, or raise an error for unsupported MemoryContent types\n            if isinstance(query, str):\n                prompt = query\n            elif isinstance(query, MemoryContent):\n                if query.mime_type in (MemoryMimeType.TEXT, MemoryMimeType.MARKDOWN):\n                    prompt = str(query.content)\n                elif query.mime_type == MemoryMimeType.JSON:\n                    prompt = serialize(query.content)\n                else:\n                    raise NotImplementedError(\n                        f\"Error: {query.mime_type} is not supported. Only MemoryMimeType.TEXT, MemoryMimeType.JSON, MemoryMimeType.MARKDOWN are currently supported.\"\n                    )\n            else:\n                raise TypeError(\"'query' must be either a string or MemoryContent\")\n\n            results = self.message_history.get_relevant(  # type: ignore\n                prompt=prompt,  # type: ignore[reportArgumentType]\n                top_k=top_k,\n                distance_threshold=distance_threshold,\n                raw=False,\n            )\n\n        memories: List[MemoryContent] = []\n        for result in results:  # type: ignore[reportUnkownVariableType]\n            metadata = deserialize(result[\"metadata\"])  # type: ignore[reportArgumentType]\n            mime_type = MemoryMimeType(metadata.pop(\"mime_type\"))\n            if mime_type in (MemoryMimeType.TEXT, MemoryMimeType.MARKDOWN):\n                memory_content = result[\"content\"]  # type: ignore[reportArgumentType]\n            elif mime_type == MemoryMimeType.JSON:\n                memory_content = deserialize(result[\"content\"])  # type: ignore[reportArgumentType]\n            else:\n                raise NotImplementedError(","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/memory/redis/_redis_memory.py#L302-L338","documentation":"RedisMemory.query accepts only str or MemoryContent as the query object; the type signature is query: str | MemoryContent. Anything else (dict, list, bytes, None) fails an isinstance chain and raises TypeError with the exact expected types — an explicit contract check before hitting RedisVL.","triggerScenarios":"await memory.query({'text': 'find this'}) (dict); passing bytes or None; forwarding an untyped variable from user input without coercion; passing a MemoryQueryResult or other autogen object by mistake.","commonSituations":"Loosely typed plumbing where the query comes from JSON request payloads; refactoring from string queries to structured queries; None slipping through when an optional field is unset.","solutions":["Coerce to str before querying: memory.query(str(query_obj)) when the value is not already MemoryContent.","Add a type check/narrowing helper at your API boundary.","If you intended structured content, wrap it: MemoryContent(content=payload, mime_type=MemoryMimeType.JSON)."],"exampleFix":"# before\nresult = await memory.query(request.payload)  # payload is a dict -> TypeError\n\n# after\nq = request.payload if isinstance(request.payload, (str, MemoryContent)) else str(request.payload)\nresult = await memory.query(q)","handlingStrategy":"type-guard","validationCode":"from autogen_core.memory import MemoryContent\n\ndef normalize_query(q):\n    if isinstance(q, (str, MemoryContent)):\n        return q\n    raise TypeError(f'query must be str or MemoryContent, got {type(q).__name__}')","typeGuard":"def is_valid_query(q) -> bool:\n    return isinstance(q, (str, MemoryContent))","tryCatchPattern":"try:\n    res = await memory.query(q)\nexcept TypeError:\n    res = await memory.query(str(q))","preventionTips":["Validate external input types before forwarding to query()","Wrap structured payloads in MemoryContent(JSON) explicitly","Annotate your plumbing with str | MemoryContent to catch drift statically"],"tags":["redis","memory","type-error","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}