microsoft/autogen · error · TypeError

'query' must be either a string or MemoryContent

Error message

'query' must be either a string or MemoryContent

What it means

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.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/memory/redis/_redis_memory.py:320

            results = self.message_history.get_recent(
                top_k=top_k,
                raw=False,
            )
        else:
            # get the query string, or raise an error for unsupported MemoryContent types
            if isinstance(query, str):
                prompt = query
            elif isinstance(query, MemoryContent):
                if query.mime_type in (MemoryMimeType.TEXT, MemoryMimeType.MARKDOWN):
                    prompt = str(query.content)
                elif query.mime_type == MemoryMimeType.JSON:
                    prompt = serialize(query.content)
                else:
                    raise NotImplementedError(
                        f"Error: {query.mime_type} is not supported. Only MemoryMimeType.TEXT, MemoryMimeType.JSON, MemoryMimeType.MARKDOWN are currently supported."
                    )
            else:
                raise TypeError("'query' must be either a string or MemoryContent")

            results = self.message_history.get_relevant(  # type: ignore
                prompt=prompt,  # type: ignore[reportArgumentType]
                top_k=top_k,
                distance_threshold=distance_threshold,
                raw=False,
            )

        memories: List[MemoryContent] = []
        for result in results:  # type: ignore[reportUnkownVariableType]
            metadata = deserialize(result["metadata"])  # type: ignore[reportArgumentType]
            mime_type = MemoryMimeType(metadata.pop("mime_type"))
            if mime_type in (MemoryMimeType.TEXT, MemoryMimeType.MARKDOWN):
                memory_content = result["content"]  # type: ignore[reportArgumentType]
            elif mime_type == MemoryMimeType.JSON:
                memory_content = deserialize(result["content"])  # type: ignore[reportArgumentType]
            else:
                raise NotImplementedError(

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Coerce to str before querying: memory.query(str(query_obj)) when the value is not already MemoryContent.
  2. Add a type check/narrowing helper at your API boundary.
  3. If you intended structured content, wrap it: MemoryContent(content=payload, mime_type=MemoryMimeType.JSON).

Example fix

# before
result = await memory.query(request.payload)  # payload is a dict -> TypeError

# after
q = request.payload if isinstance(request.payload, (str, MemoryContent)) else str(request.payload)
result = await memory.query(q)
Defensive patterns

Strategy: type-guard

Validate before calling

from autogen_core.memory import MemoryContent

def normalize_query(q):
    if isinstance(q, (str, MemoryContent)):
        return q
    raise TypeError(f'query must be str or MemoryContent, got {type(q).__name__}')

Type guard

def is_valid_query(q) -> bool:
    return isinstance(q, (str, MemoryContent))

Try / catch

try:
    res = await memory.query(q)
except TypeError:
    res = await memory.query(str(q))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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