home-assistant/core · error · HomeAssistantError

json_parse_error

Error message

json_parse_error

What it means

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.

Source

Thrown at homeassistant/components/anthropic/ai_task.py:83

                translation_domain=DOMAIN, translation_key="response_not_found"
            )

        text = chat_log.content[-1].content or ""

        if not task.structure:
            return ai_task.GenDataTaskResult(
                conversation_id=chat_log.conversation_id,
                data=text,
            )
        try:
            data = json_loads(text)
        except JSONDecodeError as err:
            _LOGGER.error(
                "Failed to parse JSON response: %s. Response: %s",
                err,
                text,
            )
            raise HomeAssistantError(
                translation_domain=DOMAIN, translation_key="json_parse_error"
            ) from err

        return ai_task.GenDataTaskResult(
            conversation_id=chat_log.conversation_id,
            data=data,
        )

View on GitHub (pinned to 58a3fdb3ea)

Solutions

  1. Check the preceding _LOGGER.error line which prints the raw response text to see exactly why parsing failed
  2. Increase the max tokens option so the JSON is not truncated mid-object
  3. Use a model that reliably emits raw JSON for structured tasks, and keep the schema/structure prompt intact
Defensive patterns

Strategy: try-catch

Validate before calling

from homeassistant.util.json import json_loads

def is_parseable_json(text: str) -> bool:
    try:
        json_loads(text)
        return True
    except JSONDecodeError:
        return False

Try / catch

try:
    data = json_loads(text)
except JSONDecodeError as err:
    _LOGGER.error("Failed to parse JSON response: %s. Response: %s", err, text)
    raise HomeAssistantError(translation_domain=DOMAIN, translation_key="json_parse_error") from err

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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