Question Classifier can fail with JSONDecodeError "Extra data" when model returns multiple JSON objects

Open Beginner friendly
#42,006 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start in api/libs/json_in_md_parser.py at parse_json_markdown() and reproduce the failure with the two-object input shown in the issue. Check the existing fenced-content fallback and expected-key validation before testing a first-value parsing approach. Done means the first valid JSON value is parsed without Extra data while existing fallback and validation behavior remains intact.

Written by the indexing model from the issue text.

Description

Self Checks
  • I have read the Contributing Guide and searched for existing issues, including closed ones.
  • This is a bug report.
  • I am submitting this report in English.
Dify version

Observed on self-hosted Dify app version 1.13.3 (Enterprise chart 3.9.10 and 3.9.11). The same parser implementation is still present on current main in api/libs/json_in_md_parser.py.

Cloud or Self Hosted

Self Hosted

Steps to reproduce

The failure occurs when a Question Classifier model returns more than one JSON object without a code fence.

A minimal parser-level reproduction is:

from libs.json_in_md_parser import parse_json_markdown

text = '{"a": 1}\n{"a": 2}'
parse_json_markdown(text)

parse_json_markdown() finds the first opening { but uses rfind("}") / rfind("]") to determine the end of the JSON block. Therefore the extracted slice contains both JSON objects:

{"a": 1}
{"a": 2}

Passing that slice to json.loads() raises:

json.decoder.JSONDecodeError: Extra data

In a real Question Classifier workload we reproduced the same class of failure at low load, so it is not a saturation/concurrency issue. The exact Extra data position varies because the model output shape is nondeterministic.

Expected Behavior

Question Classifier should parse the first valid JSON object returned by the model, or otherwise reject malformed output in a way that does not incorrectly concatenate multiple JSON objects into one parse attempt.

Actual Behavior

parse_json_markdown() currently anchors from the first { / [ to the last } / ] in the entire model response. If the response contains two JSON objects (or JSON plus another JSON-like block), json.loads() receives both and raises Extra data.

This can cause a Question Classifier workflow execution to fail even when the model produced a valid first JSON object.

Relevant implementation

Current api/libs/json_in_md_parser.py does roughly:

start_candidates = [i for i in (json_string.find("{"), json_string.find("[")) if i != -1]
start_index = min(start_candidates)
end_index = max(json_string.rfind("}"), json_string.rfind("]"))
extracted_content = json_string[start_index:end_index + 1].strip()
return json.loads(extracted_content)

The rfind() end selection is the problematic part when more than one JSON value is present.

Suggested fix

Use json.JSONDecoder().raw_decode() starting at the first JSON token and consume only the first complete JSON value, instead of slicing through the last closing bracket in the whole response.

For example, conceptually:

decoder = json.JSONDecoder()
obj, end = decoder.raw_decode(json_string[start_index:])
return obj

The exact implementation should preserve the existing fenced-content fallback behavior and expected-key validation.

Additional context
  • Reproduced under low concurrency, so this is independent of load.
  • The failure position varies across runs, consistent with varying model output shape rather than a fixed input-specific problem.
  • Structured output is not available for this Question Classifier path in the observed version, so the parser should be robust to ordinary model output variation.
  • No customer-specific data is required to reproduce this issue; the two-object example above reproduces the parser behavior directly.
Dominant language
TypeScript
Stars
157k
Forks
24.7k
Avg merge
22h 32m
Merged PRs (30d)
611

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/dify

All issues in langgenius/dify

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.