Repository: home-assistant/core
File: homeassistant/components/cloud/entity.py (Python)

Code before:
                            "input_tokens": usage.get("input_tokens"),
                            "output_tokens": usage.get("output_tokens"),
                        }
                    }
                )

        elif isinstance(event, LLMResponseIncompleteEvent):
            response = event.response
            if response and "usage" in response:
                usage = response["usage"]
                chat_log.async_trace(
                    {
                        "stats": {
                            "input_tokens": usage.get("input_tokens"),
                            "output_tokens": usage.get("output_tokens"),
                        }
                    }
                )

            incomplete_details = (
                response.get("incomplete_details")
                if isinstance(response, dict)
                else None
            )
            reason = "unknown reason"
            if isinstance(incomplete_details, dict) and incomplete_details.get(
                "reason"
            ):
                reason = incomplete_details["reason"]

            if reason == "max_output_tokens":
                reason = "max output tokens reached"
            elif reason == "content_filter":
                reason = "content filter triggered"

            raise HomeAssistantError(f"OpenAI response incomplete: {reason}")

        elif isinstance(event, LLMResponseFailedEvent):
            response = event.response
            if response and "usage" in response:
                usage = response["usage"]
                chat_log.async_trace(
                    {
                        "stats": {
                            "input_tokens": usage.get("input_tokens"),
                            "output_tokens": usage.get("output_tokens"),
                        }
                    }
                )
            reason = "unknown reason"
            if response and "error" in response:

Review comment (suggestion):
It's faster to check `is not None` than to use `isinstance`. If it's only a dict or None, check for None instead.

Code after:
                    }
                )

        elif isinstance(event, LLMResponseIncompleteEvent):
            response = event.response
            if response and "usage" in response:
                usage = response["usage"]
                chat_log.async_trace(
                    {
                        "stats": {
                            "input_tokens": usage.get("input_tokens"),
                            "output_tokens": usage.get("output_tokens"),
                        }
                    }
                )

            incomplete_details = response.get("incomplete_details")
            reason = "unknown reason"
            if incomplete_details is not None and incomplete_details.get("reason"):
                reason = incomplete_details["reason"]

            if reason == "max_output_tokens":
                reason = "max output tokens reached"
            elif reason == "content_filter":
                reason = "content filter triggered"

            raise HomeAssistantError(f"OpenAI response incomplete: {reason}")

        elif isinstance(event, LLMResponseFailedEvent):
            response = event.response
            if response and "usage" in response:
                usage = response["usage"]
                chat_log.async_trace(
                    {
                        "stats": {
                            "input_tokens": usage.get("input_tokens"),
                            "output_tokens": usage.get("output_tokens"),
                        }
                    }
                )
            reason = "unknown reason"
            if isinstance(error := response.get("error"), dict):
                reason = error.get("message") or reason
            raise HomeAssistantError(f"OpenAI response failed: {reason}")

        elif isinstance(event, LLMResponseErrorEvent):
            raise HomeAssistantError(f"OpenAI response error: {event.message}")


class BaseCloudLLMEntity(Entity):
    """Cloud LLM conversation agent."""