run-llama/llama_index · warning · ValueError

Invalid JSON Path: {expression}

Error message

Invalid JSON Path: {expression}

What it means

After the LLM emits one or more JSONPath expressions, default_output_processor parses each with jsonpath_ng. If parse(expression) or .find(json_value) throws (malformed syntax, or an expression the extension parser rejects), the exception is wrapped in ValueError('Invalid JSON Path: <expression>').

Source

Thrown at llama-index-core/llama_index/core/indices/struct_store/json_query.py:85

    try:
        from jsonpath_ng.ext import parse  # pants: no-infer-dep
        from jsonpath_ng.jsonpath import DatumInContext  # pants: no-infer-dep
    except ImportError as exc:
        IMPORT_ERROR_MSG = "You need to install jsonpath-ng to use this function!"
        raise ImportError(IMPORT_ERROR_MSG) from exc

    results: Dict[str, str] = {}

    for expression in expressions:
        try:
            datum: List[DatumInContext] = parse(expression).find(json_value)
            if datum:
                key = expression.split(".")[
                    -1
                ]  # Extracting "title" from "$.title", for example
                results[key] = ", ".join(str(i.value) for i in datum)
        except Exception as exc:
            raise ValueError(f"Invalid JSON Path: {expression}") from exc

    return results


class JSONQueryEngine(BaseQueryEngine):
    """
    GPT JSON Query Engine.

    Converts natural language to JSON Path queries.

    Args:
        json_value (JSONType): JSON value
        json_schema (JSONType): JSON schema
        json_path_prompt (BasePromptTemplate): The JSON Path prompt to use.
        output_processor (Callable): The output processor that executes the
            JSON Path query.
        output_kwargs (dict): Additional output processor kwargs for the
            output_processor function.

View on GitHub (pinned to afd0fef371)

Solutions

  1. Retry the query (LLM output varies run to run) or lower temperature to reduce malformed output
  2. Provide better few-shot examples via a custom json_path_prompt covering the syntax you expect
  3. Pre-validate/sanitize: only forward answers matching a JSONPath sanity regex before parsing, and split on newlines instead of commas if your prompts allow it
  4. Use JSONAdapter/JSONQueryEngine with a stronger model for accurate path generation

Example fix

# before
response = query_engine.query("what is the title and the price?")

# after (catch and retry with tighter prompt)
from llama_index.core.output_parsers.utils import parse_json_markdown
for attempt in range(3):
    try:
        response = query_engine.query("what is the title? (answer with a single JSONPath)")
        break
    except ValueError as e:
        continue
Defensive patterns

Strategy: retry

Validate before calling

import re
JSONPATH_RE = re.compile(r'^\$[^,]*$')
raw = llm_output.replace('JSONPath: ', '').strip()
expressions = [e.strip() for e in raw.split(',') if JSONPATH_RE.match(e.strip())]
if not expressions:
    raise ValueError('no valid JSONPath expressions in LLM output')

Type guard

def looks_like_jsonpath(expr: str) -> bool:
    return bool(expr) and expr.lstrip().startswith('$') and not any(c.isspace() for c in expr)

Try / catch

for attempt in range(3):
    try:
        response = query_engine.query(query_str)
        break
    except ValueError as e:
        if 'Invalid JSON Path' not in str(e) or attempt == 2:
            raise

Prevention

When it happens

Trigger: The model hallucinating invalid syntax like '$..title[' or '$.store.book[*].authors()[0]' variants the parser rejects; expressions containing stray text from the prompt (the code strips 'JSONPath: ' prefixes and splits on ',', so commas inside filter expressions like $.a[?(@.x>1,2)] also corrupt expressions); querying JSON whose shape differs from what the model assumed.

Common situations: Smaller/weaker models producing non-JSONPath output; few-shot examples in default_output_parser_prompt that teach wrong syntax; commas in the natural-language answer leaking into the expression list; JSON payloads that changed shape between prompt construction and query.

Understand the failure class

Related errors


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