{"record":{"id":"1d06f8dca14a43da","repo":"home-assistant/core","slug":"user-message-not-found","errorCode":null,"errorMessage":"user_message_not_found","messagePattern":"user_message_not_found","errorType":"exception","errorClass":"HomeAssistantError","httpStatus":null,"severity":"error","filePath":"homeassistant/components/anthropic/entity.py","lineNumber":1085,"sourceCode":"                        type=\"web_fetch_20250910\",\n                        max_uses=options[CONF_WEB_FETCH_MAX_USES],\n                    )\n                )\n            else:\n                tools.append(\n                    WebFetchTool20260209Param(\n                        name=\"web_fetch\",\n                        type=\"web_fetch_20260209\",\n                        max_uses=options[CONF_WEB_FETCH_MAX_USES],\n                    )\n                )\n\n        # Handle attachments by adding them to the last user message\n        last_content = chat_log.content[-1]\n        if last_content.role == \"user\" and last_content.attachments:\n            last_message = messages[-1]\n            if last_message[\"role\"] != \"user\":\n                raise HomeAssistantError(\n                    translation_domain=DOMAIN, translation_key=\"user_message_not_found\"\n                )\n            if isinstance(last_message[\"content\"], str):\n                last_message[\"content\"] = [\n                    TextBlockParam(type=\"text\", text=last_message[\"content\"])\n                ]\n            last_message[\"content\"].extend(  # type: ignore[union-attr]\n                await async_prepare_files_for_prompt(\n                    self.hass,\n                    self.model_info,\n                    [(a.path, a.mime_type) for a in last_content.attachments],\n                )\n            )\n\n        if structure and structure_name:\n            if (\n                self.model_info.capabilities\n                and self.model_info.capabilities.structured_outputs.supported","sourceCodeStart":1067,"sourceCodeEnd":1103,"githubUrl":"https://github.com/home-assistant/core/blob/58a3fdb3ea0538617f0a07efcfba6294de64fd59/homeassistant/components/anthropic/entity.py#L1067-L1103","documentation":"A HomeAssistantError with translation key 'user_message_not_found' raised while attaching files: the last chat log item is a user turn with attachments, but the last converted API message in messages[-1] has role != 'user'. The code assumes the final converted message corresponds to the final user content so attachment blocks can be appended to it.","triggerScenarios":"last_content.role == 'user' and last_content.attachments is non-empty while _convert_content produced a trailing assistant/tool message — a mismatch between chat log ordering and the conversion output, typically from an inconsistent chat log (e.g. user content after an assistant reply without a new user turn).","commonSituations":"Conversation state corruption after an interrupted tool-call loop, or external mutation of chat_log.content between conversion and attachment handling; version skew between conversation APIs and the integration.","solutions":["Start a new conversation to rebuild a consistent chat log","Ensure the last user message (with attachments) is truly the final content item before invoking the agent","Report reproducible cases to the anthropic integration maintainers with the chat log shape"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"last_content = chat_log.content[-1]\nif last_content.role == \"user\" and last_content.attachments:\n    assert messages[-1][\"role\"] == \"user\", \"conversion/user-content ordering mismatch\"","typeGuard":null,"tryCatchPattern":"if last_message[\"role\"] != \"user\":\n    raise HomeAssistantError(translation_domain=DOMAIN, translation_key=\"user_message_not_found\")","preventionTips":["Keep chat log ordering strict: final user turn must convert to the final API message","Start a new conversation after aborted tool loops instead of reusing mutated history"],"tags":["home-assistant","anthropic","attachments","chat-log"],"backgroundTag":null,"analyzedSha":"58a3fdb3ea0538617f0a07efcfba6294de64fd59","analyzedAt":"2026-08-14T20:54:38.818Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}