{"record":{"id":"eaac6ebeb65b5183","repo":"PrefectHQ/fastmcp","slug":"messages-i-must-be-message-got-type-item-n","errorCode":null,"errorMessage":"messages[{i}] must be Message, got {type(item).__name__}. Use Message({item!r}) to wrap the value.","messagePattern":"messages\\[(.+?)\\] must be Message, got (.+?)\\. Use Message\\((.+?)\\) to wrap the value\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/prompts/base.py","lineNumber":173,"sourceCode":"            messages: String or list of Message objects.\n            description: Optional description of the prompt result.\n            meta: Optional metadata about the prompt result.\n        \"\"\"\n        normalized = self._normalize_messages(messages)\n        super().__init__(messages=normalized, description=description, meta=meta)\n\n    @staticmethod\n    def _normalize_messages(\n        messages: str | list[Message],\n    ) -> list[Message]:\n        \"\"\"Normalize input to list[Message].\"\"\"\n        if isinstance(messages, str):\n            return [Message(messages)]\n        if isinstance(messages, list):\n            # Validate all items are Message\n            for i, item in enumerate(messages):\n                if not isinstance(item, Message):\n                    raise TypeError(\n                        f\"messages[{i}] must be Message, got {type(item).__name__}. \"\n                        f\"Use Message({item!r}) to wrap the value.\"\n                    )\n            return messages\n        raise TypeError(\n            f\"messages must be str or list[Message], got {type(messages).__name__}\"\n        )\n\n    def to_mcp_prompt_result(self) -> GetPromptResult:\n        \"\"\"Convert to MCP GetPromptResult.\"\"\"\n        mcp_messages = [m.to_mcp_prompt_message() for m in self.messages]\n        return GetPromptResult(\n            description=self.description,\n            messages=mcp_messages,\n            _meta=self.meta,  # type: ignore[call-arg]  # _meta is Pydantic alias for meta field\n        )\n\n","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/prompts/base.py#L155-L191","documentation":"Prompt._normalize_messages enforces that the messages argument is either a plain string or a list whose every element is a Message instance. A list containing any other type (str, dict, etc.) raises TypeError, telling you exactly which index failed and to wrap the value with Message(...).","triggerScenarios":"Prompt(..., messages=[\"hello\"]) — a list of raw strings; mixing Message and str items; passing dicts or tuples in the list; constructing Prompt programmatically from serialized data.","commonSituations":"Loading prompts from JSON/YAML where items deserialize to dicts/strings; refactors after upgrading where str items were previously auto-wrapped; authors passing openai-style message dicts.","solutions":["Wrap each item: Message(\"hello\") instead of \"hello\"","Pass a single string if the prompt is one user message: Prompt(..., messages=\"hello\")","Convert loaded data: messages=[Message(m) if isinstance(m, str) else m for m in items]"],"exampleFix":"// before\nPrompt(name=\"greet\", messages=[\"hello\", Message(\"bye\")])\n// after\nPrompt(name=\"greet\", messages=[Message(\"hello\"), Message(\"bye\")])","handlingStrategy":"type-guard","validationCode":"def ensure_messages(msgs) -> list[Message]:\n    if isinstance(msgs, str):\n        return [Message(msgs)]\n    if isinstance(msgs, list) and all(isinstance(m, Message) for m in msgs):\n        return msgs\n    raise TypeError(\"messages must be str or list[Message]\")","typeGuard":"def is_valid_messages(v: object) -> bool:\n    return isinstance(v, str) or (isinstance(v, list) and all(isinstance(i, Message) for i in v))","tryCatchPattern":"try:\n    prompt = Prompt(name=name, messages=msgs)\nexcept TypeError as e:\n    msgs = [Message(m) if isinstance(m, str) else m for m in msgs]\n    prompt = Prompt(name=name, messages=msgs)","preventionTips":["Always wrap plain strings in Message() when building lists","Validate deserialized prompt data types before constructing Prompt","Type-annotate messages as list[Message] so checkers catch raw strings"],"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"}