{"record":{"id":"2bee7aefa70464e4","repo":"home-assistant/core","slug":"json-parse-error","errorCode":null,"errorMessage":"json_parse_error","messagePattern":"json_parse_error","errorType":"exception","errorClass":"HomeAssistantError","httpStatus":null,"severity":"error","filePath":"homeassistant/components/anthropic/ai_task.py","lineNumber":83,"sourceCode":"                translation_domain=DOMAIN, translation_key=\"response_not_found\"\n            )\n\n        text = chat_log.content[-1].content or \"\"\n\n        if not task.structure:\n            return ai_task.GenDataTaskResult(\n                conversation_id=chat_log.conversation_id,\n                data=text,\n            )\n        try:\n            data = json_loads(text)\n        except JSONDecodeError as err:\n            _LOGGER.error(\n                \"Failed to parse JSON response: %s. Response: %s\",\n                err,\n                text,\n            )\n            raise HomeAssistantError(\n                translation_domain=DOMAIN, translation_key=\"json_parse_error\"\n            ) from err\n\n        return ai_task.GenDataTaskResult(\n            conversation_id=chat_log.conversation_id,\n            data=data,\n        )\n","sourceCodeStart":65,"sourceCodeEnd":91,"githubUrl":"https://github.com/home-assistant/core/blob/58a3fdb3ea0538617f0a07efcfba6294de64fd59/homeassistant/components/anthropic/ai_task.py#L65-L91","documentation":"A HomeAssistantError with translation key 'json_parse_error' raised when the Anthropic ai_task response must match a structure (task.structure set) but json_loads(text) raises JSONDecodeError. The raw model text is logged before the error is raised, so the malformed payload is inspectable.","triggerScenarios":"task.structure is truthy, chat_log.content[-1] is AssistantContent, and its text is not valid JSON — model wrapped JSON in markdown fences, emitted prose around the JSON, or truncated the payload (max token limit reached).","commonSituations":"Generating structured data with a schema but a chat model that poorly follows JSON instructions; low max_tokens option truncating output; prompt injection or unusual user data causing the model to answer conversationally.","solutions":["Check the preceding _LOGGER.error line which prints the raw response text to see exactly why parsing failed","Increase the max tokens option so the JSON is not truncated mid-object","Use a model that reliably emits raw JSON for structured tasks, and keep the schema/structure prompt intact"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"from homeassistant.util.json import json_loads\n\ndef is_parseable_json(text: str) -> bool:\n    try:\n        json_loads(text)\n        return True\n    except JSONDecodeError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    data = json_loads(text)\nexcept JSONDecodeError as err:\n    _LOGGER.error(\"Failed to parse JSON response: %s. Response: %s\", err, text)\n    raise HomeAssistantError(translation_domain=DOMAIN, translation_key=\"json_parse_error\") from err","preventionTips":["Use models with reliable JSON output for structured tasks","Set max_tokens high enough that JSON is not truncated","Strip markdown fences from model output before parsing when the model adds them"],"tags":["home-assistant","anthropic","json","ai-task","parse-error"],"backgroundTag":null,"analyzedSha":"58a3fdb3ea0538617f0a07efcfba6294de64fd59","analyzedAt":"2026-08-14T20:54:38.818Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}