666ghj/MiroFish · error · LLMResponseError

LLM returned multiple JSON values

Error message

LLM returned multiple JSON values

What it means

LLMResponseError raised when the content contains one complete JSON object followed by trailing text that itself contains another JSON container (detected by _contains_additional_json_container). Ambiguous multi-value output is rejected because picking one arbitrarily would risk using the wrong data; harmless trailing prose is only warned about and ignored.

Source

Thrown at backend/app/utils/llm_client.py:278

        try:
            value = json.loads(content)
        except json.JSONDecodeError as strict_error:
            # Some compatible providers append a short explanation after an
            # otherwise complete JSON object. Accept only an object decoded
            # from the beginning; never repair or invent truncated JSON.
            try:
                value, end = json.JSONDecoder().raw_decode(content)
            except json.JSONDecodeError:
                raise LLMResponseError(
                    "LLM returned invalid JSON "
                    f"(line {strict_error.lineno}, column {strict_error.colno})",
                    finish_reason=finish_reason,
                ) from strict_error

            trailing = content[end:].strip()
            if trailing:
                if _contains_additional_json_container(trailing):
                    raise LLMResponseError(
                        "LLM returned multiple JSON values",
                        finish_reason=finish_reason,
                    )
                logger.warning("Ignoring text after a complete LLM JSON object")

        if not isinstance(value, dict):
            raise LLMResponseError(
                "LLM JSON response must be a top-level JSON object",
                finish_reason=finish_reason,
            )

        return value

View on GitHub (pinned to b5b53acc57)

Solutions

  1. Split the request so exactly one JSON object is expected per call
  2. Make the system prompt explicit: 'Respond with exactly ONE JSON object' and remove multi-object few-shot examples
  3. Use provider-enforced JSON mode (response_format json_object), which forbids a second value
  4. If two values are genuinely needed, request a single object with nested keys instead

Example fix

# before
prompt = "Give me the entities and the relations as JSON."

# after
prompt = "Return exactly one JSON object: {\"entities\": [...], \"relations\": [...]}"
Defensive patterns

Strategy: retry

Try / catch

try:
    value = LLMClient._parse_json_response(resp)
except LLMResponseError as e:
    if "multiple JSON values" in str(e):
        value = LLMClient._parse_json_response(retry_with_single_object_instruction())
    else:
        raise

Prevention

When it happens

Trigger: Model outputs two concatenated JSON objects ('{...}{...}' or '{...} [Tool calls] {...}') — typically when the prompt asks for multiple things and the model answers each with its own object, or a reasoning model dumps intermediate JSON plus the final answer.

Common situations: Overloaded prompts requesting several structures at once, models that echo the input JSON before answering, or few-shot examples showing multiple objects.

Related errors


AI-assisted analysis of 666ghj/MiroFish@b5b53acc57 (2026-08-14). Data as JSON: /api/errors/c067eebeb52e62ce. Report an issue: GitHub.