Skip to content

fix: parse single-line fenced json responses#118

Open
pollychen-lab wants to merge 5 commits into
alphacrack:mainfrom
pollychen-lab:fix/issue-97-single-line-fenced-json
Open

fix: parse single-line fenced json responses#118
pollychen-lab wants to merge 5 commits into
alphacrack:mainfrom
pollychen-lab:fix/issue-97-single-line-fenced-json

Conversation

@pollychen-lab

Copy link
Copy Markdown
Contributor

Summary

  • allow fenced JSON extraction when the opening fence and JSON payload are on the same line
  • add a regression test that proves the fenced-block fast path wins over malformed preamble braces

Fixes #97

Validation

  • python3 -m pytest tests/test_llm_backend.py -q
  • . .venv/bin/activate && python -m pytest -q
  • . .venv/bin/activate && ruff check src/ tests/
  • git diff --check
  • rg -n 'Karim13014|claude-code@anthropic.com' . -g '!.git' -g '!.venv'

@alphacrack

Copy link
Copy Markdown
Owner

Thanks — the single-line fence case from #97 is real, and your regression test correctly fails on main (verified; full suite 282 passed locally at this head). One regression to address before merge, though:

The widened regex now captures non-JSON fences. With the \n requirement gone and (?:json)? matching empty, any fenced block — or inline triple-backtick span — takes the fence fast path, swallowing the language tag into the payload. Concretely:

Setup:
```bash
pip install x

{"feasible": true}


main finds no JSON fence here and the brace-scan fallback returns the JSON; this branch extracts `bash\npip install x`, `json.loads` fails, and `complete_json` burns its retries on every such planner/distiller response — or exhausts them and hard-fails the stage.

Suggested guard: take the fence path only when the payload parses as JSON (fall through to the brace scanner otherwise), or require the `json` tag when the opener has no trailing newline.

Also worth noting: this is the repo's only `extract_json` test, and the PR changes the function's only regex. Could you add cases pinning the previously-working shapes — multi-line ```json fence, bare ``` fence, the no-fence brace fallback, and a ```bash fence followed by raw JSON? That last one exposes the regression above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

llm: extract_json fenced-block fast path misses single-line fenced JSON responses

2 participants