run-llama/llama_index · error · ValueError

Could not extract json string from output: {text}

Error message

Could not extract json string from output: {text}

What it means

Error "Could not extract json string from output: {text}" thrown in run-llama/llama_index.

Source

Thrown at llama-index-core/llama_index/core/output_parsers/utils.py:118

        # partly because there can be cases where only one and not the other
        # is present, and partly because we don't need to be so worried
        # about it being a string in a programming language
        if candidate.startswith("```"):
            candidate = re.sub(r"^```[a-zA-Z]*", "", candidate)

        if candidate.endswith("```"):
            candidate = candidate[:-3]
        code = [candidate.strip()]

    return code


def extract_json_str(text: str) -> str:
    """Extract JSON string from text."""
    # NOTE: this regex parsing is taken from langchain.output_parsers.pydantic
    match = re.search(r"\{.*\}", text.strip(), re.MULTILINE | re.IGNORECASE | re.DOTALL)
    if not match:
        raise ValueError(f"Could not extract json string from output: {text}")

    return match.group()

View on GitHub (pinned to afd0fef371)

Solutions

  1. Prompt the LLM to wrap the JSON in a fenced code block or return raw JSON.
  2. Pre-process the output text to strip prose around the JSON before parsing.

When it happens

Trigger: Thrown at llama-index-core/llama_index/core/output_parsers/utils.py:118 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/b7842834bf1d2ee5. Report an issue: GitHub.