home-assistant/core · error · HomeAssistantError

unexpected_stream_object

Error message

unexpected_stream_object

What it means

A HomeAssistantError with translation key 'unexpected_stream_object' raised in AnthropicDeltaStream.__aiter__ when the stored stream is None or does not implement __aiter__. The code expects an Anthropic SDK async stream object; anything else (a sync stream, a response dict, or None) cannot be iterated asynchronously.

Source

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

        self._stream_iterator: AsyncIterator[MessageStreamEvent] | None = None

        self._current_tool_block: ToolUseBlockParam | ServerToolUseBlockParam | None = (
            None
        )
        self._current_tool_args: str = ""
        self._content_details = ContentDetails()
        self._content_details.add_citation_detail()
        self._input_usage: Usage | None = None
        self._first_block: bool = True

    def __aiter__(
        self,
    ) -> AsyncIterator[
        conversation.AssistantContentDeltaDict | conversation.ToolResultContentDeltaDict
    ]:
        """Initialize the stream and return the async iterator."""
        if self._stream is None or not hasattr(self._stream, "__aiter__"):
            raise HomeAssistantError(
                translation_domain=DOMAIN, translation_key="unexpected_stream_object"
            )
        if self._stream_iterator is None:
            self._stream_iterator = self._stream.__aiter__()
        return self

    async def __anext__(
        self,
    ) -> (
        conversation.AssistantContentDeltaDict | conversation.ToolResultContentDeltaDict
    ):
        """Get the next item from the stream."""
        while True:
            if self._buffer:
                return self._buffer.popleft()

            response = await self._stream_iterator.__anext__()  # type: ignore[union-attr]

View on GitHub (pinned to 58a3fdb3ea)

Solutions

  1. Ensure messages are created with stream=True via the AsyncAnthropic client so the returned object is an async iterator
  2. Verify self._stream was assigned before iteration begins
  3. Pin/upgrade the anthropic package to the version required by the integration

Example fix

// before
client = anthropic.Anthropic()  # sync client
stream = client.messages.create(..., stream=True)
delta = AnthropicDeltaStream(chat_log, stream)

// after
client = anthropic.AsyncAnthropic()
stream = await client.messages.create(..., stream=True)
delta = AnthropicDeltaStream(chat_log, stream)
Defensive patterns

Strategy: type-guard

Type guard

from typing import AsyncIterable

def is_async_stream(obj: object) -> bool:
    return obj is not None and hasattr(obj, "__aiter__")

Try / catch

try:
    async for delta in AnthropicDeltaStream(chat_log, stream):
        ...
except HomeAssistantError as err:
    if err.translation_key == "unexpected_stream_object":
        # verify stream=True and the AsyncAnthropic client were used
        ...

Prevention

When it happens

Trigger: Constructing AnthropicDeltaStream with a non-async-iterable (e.g. passing the raw response of a non-streaming messages.create call, or the sync client's stream), or iterating before a stream was assigned (self._stream is None).

Common situations: anthropic SDK version change where the streaming return type differs, or custom code reusing the class with a mocked/incorrect stream object in tests.

Related errors


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