home-assistant/core · error · HomeAssistantError

user_message_not_found

Error message

user_message_not_found

What it means

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.

Source

Thrown at homeassistant/components/anthropic/entity.py:1085

                        type="web_fetch_20250910",
                        max_uses=options[CONF_WEB_FETCH_MAX_USES],
                    )
                )
            else:
                tools.append(
                    WebFetchTool20260209Param(
                        name="web_fetch",
                        type="web_fetch_20260209",
                        max_uses=options[CONF_WEB_FETCH_MAX_USES],
                    )
                )

        # Handle attachments by adding them to the last user message
        last_content = chat_log.content[-1]
        if last_content.role == "user" and last_content.attachments:
            last_message = messages[-1]
            if last_message["role"] != "user":
                raise HomeAssistantError(
                    translation_domain=DOMAIN, translation_key="user_message_not_found"
                )
            if isinstance(last_message["content"], str):
                last_message["content"] = [
                    TextBlockParam(type="text", text=last_message["content"])
                ]
            last_message["content"].extend(  # type: ignore[union-attr]
                await async_prepare_files_for_prompt(
                    self.hass,
                    self.model_info,
                    [(a.path, a.mime_type) for a in last_content.attachments],
                )
            )

        if structure and structure_name:
            if (
                self.model_info.capabilities
                and self.model_info.capabilities.structured_outputs.supported

View on GitHub (pinned to 58a3fdb3ea)

Solutions

  1. Start a new conversation to rebuild a consistent chat log
  2. Ensure the last user message (with attachments) is truly the final content item before invoking the agent
  3. Report reproducible cases to the anthropic integration maintainers with the chat log shape
Defensive patterns

Strategy: validation

Validate before calling

last_content = chat_log.content[-1]
if last_content.role == "user" and last_content.attachments:
    assert messages[-1]["role"] == "user", "conversion/user-content ordering mismatch"

Try / catch

if last_message["role"] != "user":
    raise HomeAssistantError(translation_domain=DOMAIN, translation_key="user_message_not_found")

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of home-assistant/core@58a3fdb3ea (2026-08-14). Data as JSON: /api/errors/1d06f8dca14a43da. Report an issue: GitHub.