{"record":{"id":"3bad59d6384456b7","repo":"home-assistant/core","slug":"api-refusal","errorCode":null,"errorMessage":"api_refusal","messagePattern":"api_refusal","errorType":"exception","errorClass":"HomeAssistantError","httpStatus":null,"severity":"warning","filePath":"homeassistant/components/anthropic/entity.py","lineNumber":870,"sourceCode":"                    \"tool_calls\": [\n                        llm.ToolInput(\n                            id=self._current_tool_block[\"id\"],\n                            tool_name=self._current_tool_block[\"name\"],\n                            tool_args=self._current_tool_block[\"input\"],\n                            external=self._current_tool_block[\"type\"]\n                            == \"server_tool_use\",\n                        )\n                    ]\n                }\n            )\n            self._current_tool_block = None\n\n    def on_message_delta_event(self, delta: Delta, usage: MessageDeltaUsage) -> None:\n        \"\"\"Handle RawMessageDeltaEvent.\"\"\"\n        self._chat_log.async_trace(self._create_token_stats(self._input_usage, usage))\n        self._content_details.container = delta.container\n        if delta.stop_reason == \"refusal\":\n            raise HomeAssistantError(\n                translation_domain=DOMAIN, translation_key=\"api_refusal\"\n            )\n\n    def on_message_stop_event(self) -> None:\n        \"\"\"Handle RawMessageStopEvent.\"\"\"\n        if self._content_details:\n            self._content_details.delete_empty()\n            self._buffer.append({\"native\": self._content_details})\n        self._content_details = ContentDetails()\n        self._content_details.add_citation_detail()\n\n    def _create_token_stats(\n        self, input_usage: Usage | None, response_usage: MessageDeltaUsage\n    ) -> dict[str, Any]:\n        \"\"\"Create token stats for conversation agent tracing.\"\"\"\n        input_tokens = 0\n        cached_input_tokens = 0\n        if input_usage:","sourceCodeStart":852,"sourceCodeEnd":888,"githubUrl":"https://github.com/home-assistant/core/blob/58a3fdb3ea0538617f0a07efcfba6294de64fd59/homeassistant/components/anthropic/entity.py#L852-L888","documentation":"A HomeAssistantError with translation key 'api_refusal' raised in on_message_delta_event when the Anthropic message stream ends with stop_reason == 'refusal'. Anthropic models can refuse to continue generating when safety systems flag the conversation; the integration surfaces this as a user-visible error instead of returning partial content.","triggerScenarios":"Streaming a completion where the final MessageDeltaEvent carries delta.stop_reason='refusal' — prompts containing content that trips Anthropic safety filters, or attempts to extract system-prompt/copyrighted material via the conversation agent.","commonSituations":"Users asking the voice/assistant entity for disallowed content, attachments or chat history that accumulate into a flagged context, or overly adversarial prompt engineering.","solutions":["Rephrase the request to avoid the content that triggered the safety refusal","Clear or trim the conversation history that may be accumulating flagged context","If false-positive, report via Anthropic feedback channels and retry with different wording"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    async for delta in stream:\n        ...\nexcept HomeAssistantError as err:\n    if err.translation_key == \"api_refusal\":\n        # inform the user and offer to rephrase; do not retry identically\n        ...","preventionTips":["Avoid prompts that trip safety filters; keep conversation history trimmed","Treat refusal as a terminal condition, not a retryable error"],"tags":["home-assistant","anthropic","safety","refusal","streaming"],"backgroundTag":null,"analyzedSha":"58a3fdb3ea0538617f0a07efcfba6294de64fd59","analyzedAt":"2026-08-14T20:54:38.818Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}