{"record":{"id":"cc1c60e9ef0f2ae6","repo":"FoundationAgents/OpenManus","slug":"the-last-message-must-be-from-the-user-to-attach-i","errorCode":null,"errorMessage":"The last message must be from the user to attach images","messagePattern":"The last message must be from the user to attach images","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"app/llm.py","lineNumber":528,"sourceCode":"            TokenLimitExceeded: If token limits are exceeded\n            ValueError: If messages are invalid or response is empty\n            OpenAIError: If API call fails after retries\n            Exception: For unexpected errors\n        \"\"\"\n        try:\n            # For ask_with_images, we always set supports_images to True because\n            # this method should only be called with models that support images\n            if self.model not in MULTIMODAL_MODELS:\n                raise ValueError(\n                    f\"Model {self.model} does not support images. Use a model from {MULTIMODAL_MODELS}\"\n                )\n\n            # Format messages with image support\n            formatted_messages = self.format_messages(messages, supports_images=True)\n\n            # Ensure the last message is from the user to attach images\n            if not formatted_messages or formatted_messages[-1][\"role\"] != \"user\":\n                raise ValueError(\n                    \"The last message must be from the user to attach images\"\n                )\n\n            # Process the last user message to include images\n            last_message = formatted_messages[-1]\n\n            # Convert content to multimodal format if needed\n            content = last_message[\"content\"]\n            multimodal_content = (\n                [{\"type\": \"text\", \"text\": content}]\n                if isinstance(content, str)\n                else content\n                if isinstance(content, list)\n                else []\n            )\n\n            # Add images to content\n            for image in images:","sourceCodeStart":510,"sourceCodeEnd":546,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/llm.py#L510-L546","documentation":"Raised by LLM.ask_with_images() when, after formatting, the last message in the list is not role=\"user\". Images are attached by mutating the final user message's content into a multimodal array, so the code requires that anchor point. A trailing assistant or system message makes attachment ambiguous and triggers this guard.","triggerScenarios":"Calling ask_with_images(messages) where messages ends with an assistant message (e.g. pre-populated dialogue), an empty list (formatted_messages is empty), or messages ending with a system message.","commonSituations":"Reusing a conversation history that ends with the model's last reply; injecting a system-style suffix after the user turn; passing [] accidentally.","solutions":["Append a user message before calling: messages.append(Message.user_message(\"Describe this image\"))","Strip trailing non-user messages from the history you pass in","Ensure the list is non-empty — check len(messages) > 0 before the call"],"exampleFix":"# before\nmessages = [Message.system_message(\"you are helpful\"), Message.assistant_message(\"hi\")]\nawait llm.ask_with_images(messages, [img])  # ValueError\n\n# after\nmessages.append(Message.user_message(\"describe this image\"))\nawait llm.ask_with_images(messages, [img])","handlingStrategy":"validation","validationCode":"def last_message_is_user(messages: list) -> bool:\n    return bool(messages) and getattr(messages[-1], \"role\", None) == \"user\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always append a fresh user message before ask_with_images()","Trim trailing assistant/system messages from reused histories","Unit-test the message assembly step so role ordering cannot silently regress"],"tags":["llm","messages","multimodal","validation"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}