{"record":{"id":"04c8f7f52afb2563","repo":"home-assistant/core","slug":"unexpected-stream-object","errorCode":null,"errorMessage":"unexpected_stream_object","messagePattern":"unexpected_stream_object","errorType":"exception","errorClass":"HomeAssistantError","httpStatus":null,"severity":"error","filePath":"homeassistant/components/anthropic/entity.py","lineNumber":559,"sourceCode":"        self._stream_iterator: AsyncIterator[MessageStreamEvent] | None = None\n\n        self._current_tool_block: ToolUseBlockParam | ServerToolUseBlockParam | None = (\n            None\n        )\n        self._current_tool_args: str = \"\"\n        self._content_details = ContentDetails()\n        self._content_details.add_citation_detail()\n        self._input_usage: Usage | None = None\n        self._first_block: bool = True\n\n    def __aiter__(\n        self,\n    ) -> AsyncIterator[\n        conversation.AssistantContentDeltaDict | conversation.ToolResultContentDeltaDict\n    ]:\n        \"\"\"Initialize the stream and return the async iterator.\"\"\"\n        if self._stream is None or not hasattr(self._stream, \"__aiter__\"):\n            raise HomeAssistantError(\n                translation_domain=DOMAIN, translation_key=\"unexpected_stream_object\"\n            )\n        if self._stream_iterator is None:\n            self._stream_iterator = self._stream.__aiter__()\n        return self\n\n    async def __anext__(\n        self,\n    ) -> (\n        conversation.AssistantContentDeltaDict | conversation.ToolResultContentDeltaDict\n    ):\n        \"\"\"Get the next item from the stream.\"\"\"\n        while True:\n            if self._buffer:\n                return self._buffer.popleft()\n\n            response = await self._stream_iterator.__anext__()  # type: ignore[union-attr]\n","sourceCodeStart":541,"sourceCodeEnd":577,"githubUrl":"https://github.com/home-assistant/core/blob/58a3fdb3ea0538617f0a07efcfba6294de64fd59/homeassistant/components/anthropic/entity.py#L541-L577","documentation":"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.","triggerScenarios":"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).","commonSituations":"anthropic SDK version change where the streaming return type differs, or custom code reusing the class with a mocked/incorrect stream object in tests.","solutions":["Ensure messages are created with stream=True via the AsyncAnthropic client so the returned object is an async iterator","Verify self._stream was assigned before iteration begins","Pin/upgrade the anthropic package to the version required by the integration"],"exampleFix":"// before\nclient = anthropic.Anthropic()  # sync client\nstream = client.messages.create(..., stream=True)\ndelta = AnthropicDeltaStream(chat_log, stream)\n\n// after\nclient = anthropic.AsyncAnthropic()\nstream = await client.messages.create(..., stream=True)\ndelta = AnthropicDeltaStream(chat_log, stream)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from typing import AsyncIterable\n\ndef is_async_stream(obj: object) -> bool:\n    return obj is not None and hasattr(obj, \"__aiter__\")","tryCatchPattern":"try:\n    async for delta in AnthropicDeltaStream(chat_log, stream):\n        ...\nexcept HomeAssistantError as err:\n    if err.translation_key == \"unexpected_stream_object\":\n        # verify stream=True and the AsyncAnthropic client were used\n        ...","preventionTips":["Always create streams with stream=True on an AsyncAnthropic client","Assert the stream is non-None before constructing the delta wrapper"],"tags":["home-assistant","anthropic","streaming","async"],"backgroundTag":null,"analyzedSha":"58a3fdb3ea0538617f0a07efcfba6294de64fd59","analyzedAt":"2026-08-14T20:54:38.818Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}