{"record":{"id":"fa92ce2af84b8b6a","repo":"PrefectHQ/fastmcp","slug":"messages-i-must-be-message-or-str-got-type-it","errorCode":null,"errorMessage":"messages[{i}] must be Message or str, got {type(item).__name__}. Use Message({item!r}) to wrap the value.","messagePattern":"messages\\[(.+?)\\] must be Message or str, got (.+?)\\. Use Message\\((.+?)\\) to wrap the value\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/prompts/base.py","lineNumber":340,"sourceCode":"\n        if isinstance(raw_value, mcp_types.InputRequiredResult):\n            # The prompt asked the client for input (SEP-2322). Wrap it so the\n            # ask travels the middleware chain as an ordinary result; the wire\n            # handler unwraps it.\n            return InputRequiredPromptResult(raw_value)\n\n        if isinstance(raw_value, str):\n            return PromptResult(raw_value, description=self.description, meta=self.meta)\n\n        if isinstance(raw_value, list | tuple):\n            messages: list[Message] = []\n            for i, item in enumerate(raw_value):\n                if isinstance(item, Message):\n                    messages.append(item)\n                elif isinstance(item, str):\n                    messages.append(Message(item))\n                else:\n                    raise TypeError(\n                        f\"messages[{i}] must be Message or str, got {type(item).__name__}. \"\n                        f\"Use Message({item!r}) to wrap the value.\"\n                    )\n            return PromptResult(messages, description=self.description, meta=self.meta)\n\n        raise TypeError(\n            f\"Prompt must return str, list[Message], or PromptResult, \"\n            f\"got {type(raw_value).__name__}\"\n        )\n\n    async def _render(\n        self,\n        arguments: dict[str, Any] | None = None,\n    ) -> PromptResult:\n        \"\"\"Server entry point for prompt renders.\n\n        The server calls this method instead of render() directly so that\n        subclasses can customize dispatch. For example, FastMCPProviderPrompt","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/prompts/base.py#L322-L358","documentation":"Prompt.convert_result accepts only PromptResult, str, or list of Message/str as a render return value. Inside a list, any item that is not a Message or str raises TypeError at that index, prompting you to wrap it with Message(...).","triggerScenarios":"A prompt's render() returns a list containing ints, dicts, Messages from a different library, tuples, or None — e.g. return [\"hi\", {\"role\": \"user\", \"content\": \"x\"}].","commonSituations":"Returning LLM SDK message dicts directly from render; returning template parts of mixed types; returning results of splitting a string into non-str objects.","solutions":["Wrap non-str items in Message(...) inside the returned list","Return a plain str or PromptResult instead of a heterogeneous list","Sanitize the list before returning: [it if isinstance(it,(Message,str)) else Message(str(it)) for it in items]"],"exampleFix":"// before\nreturn [\"hello\", {\"role\": \"user\", \"content\": \"bye\"}]\n// after\nreturn [Message(\"hello\"), Message(\"bye\")]","handlingStrategy":"type-guard","validationCode":"def safe_render_list(items) -> list:\n    out = []\n    for it in items:\n        if isinstance(it, (Message, str)):\n            out.append(it)\n        else:\n            out.append(Message(str(it)))\n    return out","typeGuard":"def is_message_or_str_list(v: object) -> bool:\n    return isinstance(v, list) and all(isinstance(i, (Message, str)) for i in v)","tryCatchPattern":"try:\n    result = prompt.convert_result(raw)\nexcept TypeError as e:\n    raw = [Message(str(x)) for x in raw]\n    result = prompt.convert_result(raw)","preventionTips":["Return only str/Message items from render()","Never return raw LLM SDK dicts from render","Annotate render() return types so type checkers catch bad items"],"tags":["prompts","type-error","validation","python"],"backgroundTag":"wrong-argument-type","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}