Commit 885fff8
refactor(llm): robust JSON parsing + centralized prompt engine
Three-layer defense for LLM JSON output reliability:
1. JSON parser rewrite (json_parser.py):
- 4-stage repair pipeline: direct parse → control-char sanitize →
json_repair library → structural repair for truncated output
- Removed fragile _repair_unescaped_quotes heuristic in favor of
json_repair library which handles ~95% of LLM JSON defects
- Added json-repair>=0.30 to core dependencies
2. Centralized JSON instruction prompts (router.py):
- _JSON_RULES constant with explicit quote-escaping guidance
- _SYSTEM_TOOL_RESPONSE, _SYSTEM_JSON_ONLY, _SYSTEM_ROUTE
- All 3 system prompt paths now include backslash-escape examples
- Critical for small/medium models (7B-14B) that embed narrative
dialogue with unescaped double quotes
3. Single parsing entry point:
- All LLM→JSON parsing routes through _extract_json_object()
- Consistent behavior across route(), generate_json(), and
_generate_tool_response()
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 9c754b6 commit 885fff8
3 files changed
Lines changed: 276 additions & 216 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
0 commit comments