fix: robust JSON parsing for vision models emitting <think> blocks - #11
Merged
Merged
Conversation
- Strengthen _strip_reasoning() to recover JSON from inside think blocks - Add _find_first_json_object() using proper brace-depth parsing - Improve _parse_json_object() with multiple fallback strategies - Harden EXTRACTION_SCHEMA_PROMPT with explicit no-<think> rules This fixes 'model output could not be parsed as JSON (starts with: " <think>") errors when using qwen/qwen3.6-27b and similar reasoning vision models on Groq. Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.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.
Fixes repeated extraction failures for medical prescription images (and other documents) when the Groq vision model (qwen/qwen3.6-27b) emits reasoning/think blocks.
Problem
Vision model responses often start with:
This caused:
Changes
_strip_reasoning()to recover JSON that lives inside<think>...</think>blocks_find_first_json_object()— robust brace-depth parser (correctly handles strings, escapes, nesting)_parse_json_object()with layered fallbacks (direct parse → depth finder → fenced → outer span → raw)EXTRACTION_SCHEMA_PROMPT+ injected schema suffix with explicit "NO " rulesThe fix is backward compatible and makes the pipeline much more resilient to reasoning models on Groq.
Tested against the exact failing output prefix from production logs.