fix(extract_json): tolerate non-strict model JSON (e.g. DeepSeek)#325
Closed
cnndabbler wants to merge 1 commit into
Closed
fix(extract_json): tolerate non-strict model JSON (e.g. DeepSeek)#325cnndabbler wants to merge 1 commit into
cnndabbler wants to merge 1 commit into
Conversation
extract_json() assumed the whole response is JSON and returned {} on any
parse failure, which then KeyError-crashed callers (toc_detector_single_page)
mid-index-build on models that wrap JSON in prose/fences. Add a balanced-brace
fallback that pulls the first {...}/[...] object out of the raw response, and
default toc_detector's key access so a single bad page can't abort the run.
Repros on deepseek/deepseek-v4-flash; OpenAI/glm happened to match the strict
path. Fixes intermittent 'Processing failed' on long PDFs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
extract_json()assumes the entire model response is JSON and returns{}on anyJSONDecodeError. Callers then access keys directly (e.g.toc_detector_single_pagedoesjson_content['toc_detected']), so a singleresponse with stray prose/code-fences around the JSON raises
KeyErrorand abortsthe whole index build with
Processing failed.This reproduces intermittently on models that don't return bare JSON — e.g.
deepseek/deepseek-v4-flashvia LiteLLM. OpenAI/GLM happened to match the strictpath, so it wasn't caught.
Fix
extract_json: add a balanced-brace fallback (_extract_balanced_json) that pullsthe first
{...}/[...]object out of the raw response when direct parsing fails —handles models that wrap JSON in prose or fences.
toc_detector_single_page: use.get('toc_detected', 'no')so one unparseable pagecan't crash the run.
No behavior change for responses that already parse. Verified end-to-end on a 39-page
PDF with
deepseek/deepseek-v4-flash(previously failed at TOC detection, now completesat 100% accuracy).
🤖 Generated with Claude Code