Skip to content

fix: robust JSON parsing for vision models emitting <think> blocks - #11

Merged
arena-ai-coding-agent[bot] merged 1 commit into
mainfrom
arena/019fd7ba-nalam
Aug 6, 2026
Merged

arena-ai-coding-agent[bot] merged 1 commit into
mainfrom
arena/019fd7ba-nalam

Conversation

@arena-ai-coding-agent

Copy link
Copy Markdown
Contributor

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:

 <think> The user wants me to extract data from a medical prescription image. ...

This caused:

ValueError: model output could not be parsed as JSON (starts with: ' <think> ...')

Changes

  • Strengthened _strip_reasoning() to recover JSON that lives inside <think>...</think> blocks
  • Added _find_first_json_object() — robust brace-depth parser (correctly handles strings, escapes, nesting)
  • Rewrote _parse_json_object() with layered fallbacks (direct parse → depth finder → fenced → outer span → raw)
  • Hardened EXTRACTION_SCHEMA_PROMPT + injected schema suffix with explicit "NO " rules

The 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.

- 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>
@arena-ai-coding-agent
arena-ai-coding-agent Bot merged commit 89e8973 into main Aug 6, 2026
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.

1 participant