{"record":{"id":"786bf32f17b13315","repo":"huggingface/smolagents","slug":"the-json-blob-you-used-is-invalid-due-to-the-follo","errorCode":null,"errorMessage":"The JSON blob you used is invalid due to the following error: {e}.\\nJSON blob was: {json_blob}, decoding failed on that specific part of the blob:\\n'{json_blob[place - 4 : place + 5]}'.","messagePattern":"The JSON blob you used is invalid due to the following error: (.+?)\\.\\\\nJSON blob was: (.+?), decoding failed on that specific part of the blob:\\\\n'(.+?)'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/smolagents/utils.py","lineNumber":182,"sourceCode":"\n\ndef parse_json_blob(json_blob: str) -> tuple[dict[str, str], str]:\n    \"Extracts the JSON blob from the input and returns the JSON data and the rest of the input.\"\n    try:\n        first_accolade_index = json_blob.find(\"{\")\n        last_accolade_index = [a.start() for a in list(re.finditer(\"}\", json_blob))][-1]\n        json_str = json_blob[first_accolade_index : last_accolade_index + 1]\n        json_data = json.loads(json_str, strict=False)\n        return json_data, json_blob[:first_accolade_index]\n    except IndexError:\n        raise ValueError(\"The model output does not contain any JSON blob.\")\n    except json.JSONDecodeError as e:\n        place = e.pos\n        if json_blob[place - 1 : place + 2] == \"},\\n\":\n            raise ValueError(\n                \"JSON is invalid: you probably tried to provide multiple tool calls in one action. PROVIDE ONLY ONE TOOL CALL.\"\n            )\n        raise ValueError(\n            f\"The JSON blob you used is invalid due to the following error: {e}.\\n\"\n            f\"JSON blob was: {json_blob}, decoding failed on that specific part of the blob:\\n\"\n            f\"'{json_blob[place - 4 : place + 5]}'.\"\n        )\n\n\ndef extract_code_from_text(text: str, code_block_tags: tuple[str, str]) -> str | None:\n    \"\"\"Extract code from the LLM's output.\"\"\"\n    pattern = rf\"{code_block_tags[0]}(.*?){code_block_tags[1]}\"\n    matches = re.findall(pattern, text, re.DOTALL)\n    if matches:\n        return \"\\n\\n\".join(match.strip() for match in matches)\n    return None\n\n\ndef parse_code_blobs(text: str, code_block_tags: tuple[str, str]) -> str:\n    \"\"\"Extract code blocs from the LLM's output.\n","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/utils.py#L164-L200","documentation":"The generic JSON-decode failure branch of parse_json_blob: the extracted blob is not valid JSON. The message embeds the json.JSONDecodeError, the full blob, and a 9-character window around the failing position to pinpoint the syntax error.","triggerScenarios":"Model emits malformed JSON: unescaped quotes/newlines inside strings, trailing commas, Python-style True/None/single quotes, or braces in prose being captured into the blob because parsing takes first '{' to last '}'.","commonSituations":"Models writing Python dict literals instead of JSON; code or prose containing extra '}' after the JSON causing weird slicing; strict=False only relaxes control characters, not structural errors.","solutions":["Feed the error back to the model and retry the step (agent loop does this automatically)","Switch to or configure a model that reliably emits JSON, or use native function-calling models (e.g. OpenAIServerModel with tool calling)","Sanitize common issues (single quotes, Python literals) in a pre-parse hook if you control the text","Increase max_tokens to avoid truncated JSON"],"exampleFix":"# before (model output)\n{'name': 'search', 'arguments': {'q': 'cats'}}  # single quotes\n\n# after\n{\"name\": \"search\", \"arguments\": {\"q\": \"cats\"}}","handlingStrategy":"retry","validationCode":"import json, re\ndef try_parse_action(text):\n    m = re.search(r'\\{.*\\}', text, re.DOTALL)\n    if not m:\n        return None\n    try:\n        return json.loads(m.group(0))\n    except json.JSONDecodeError:\n        return None  # trigger retry instead of crashing","typeGuard":"def is_valid_model_json(text: str) -> bool:\n    import json, re\n    m = re.search(r'\\{.*\\}', text, re.DOTALL)\n    if not m:\n        return False\n    try:\n        json.loads(m.group(0))\n        return True\n    except json.JSONDecodeError:\n        return False","tryCatchPattern":"try:\n    data, _ = parse_json_blob(text)\nexcept ValueError as e:\n    # message contains position context; log blob excerpt and retry the model step\n    raise","preventionTips":["Use JSON-mode or function-calling capable models","Instruct 'output strict JSON, no trailing commas, double quotes only'","Increase max_tokens"],"tags":["smolagents","llm-output-parsing","json","syntax-error"],"backgroundTag":"invalid-model-json-output","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}