{"record":{"id":"c067eebeb52e62ce","repo":"666ghj/MiroFish","slug":"llm-returned-multiple-json-values","errorCode":null,"errorMessage":"LLM returned multiple JSON values","messagePattern":"LLM returned multiple JSON values","errorType":"exception","errorClass":"LLMResponseError","httpStatus":null,"severity":"error","filePath":"backend/app/utils/llm_client.py","lineNumber":278,"sourceCode":"        try:\n            value = json.loads(content)\n        except json.JSONDecodeError as strict_error:\n            # Some compatible providers append a short explanation after an\n            # otherwise complete JSON object. Accept only an object decoded\n            # from the beginning; never repair or invent truncated JSON.\n            try:\n                value, end = json.JSONDecoder().raw_decode(content)\n            except json.JSONDecodeError:\n                raise LLMResponseError(\n                    \"LLM returned invalid JSON \"\n                    f\"(line {strict_error.lineno}, column {strict_error.colno})\",\n                    finish_reason=finish_reason,\n                ) from strict_error\n\n            trailing = content[end:].strip()\n            if trailing:\n                if _contains_additional_json_container(trailing):\n                    raise LLMResponseError(\n                        \"LLM returned multiple JSON values\",\n                        finish_reason=finish_reason,\n                    )\n                logger.warning(\"Ignoring text after a complete LLM JSON object\")\n\n        if not isinstance(value, dict):\n            raise LLMResponseError(\n                \"LLM JSON response must be a top-level JSON object\",\n                finish_reason=finish_reason,\n            )\n\n        return value\n","sourceCodeStart":260,"sourceCodeEnd":291,"githubUrl":"https://github.com/666ghj/MiroFish/blob/b5b53acc57189a4a42e44a23e149dc655c98fe82/backend/app/utils/llm_client.py#L260-L291","documentation":"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.","triggerScenarios":"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.","commonSituations":"Overloaded prompts requesting several structures at once, models that echo the input JSON before answering, or few-shot examples showing multiple objects.","solutions":["Split the request so exactly one JSON object is expected per call","Make the system prompt explicit: 'Respond with exactly ONE JSON object' and remove multi-object few-shot examples","Use provider-enforced JSON mode (response_format json_object), which forbids a second value","If two values are genuinely needed, request a single object with nested keys instead"],"exampleFix":"# before\nprompt = \"Give me the entities and the relations as JSON.\"\n\n# after\nprompt = \"Return exactly one JSON object: {\\\"entities\\\": [...], \\\"relations\\\": [...]}\"","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    value = LLMClient._parse_json_response(resp)\nexcept LLMResponseError as e:\n    if \"multiple JSON values\" in str(e):\n        value = LLMClient._parse_json_response(retry_with_single_object_instruction())\n    else:\n        raise","preventionTips":["One JSON structure per request; nest related data under keys","Few-shot examples must show exactly one object","Use provider JSON mode to structurally prevent a second value"],"tags":["llm","json","ambiguous-output","prompt-engineering"],"backgroundTag":null,"analyzedSha":"b5b53acc57189a4a42e44a23e149dc655c98fe82","analyzedAt":"2026-08-14T22:29:33.146Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}