Skip to content

Commit 885fff8

Browse files
dennys246claude
andcommitted
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

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
"scipy>=1.15,<2.0",
3131
"llama-cpp-python>=0.3.8",
3232
"ddgs>=6.0.0",
33+
"json-repair>=0.30",
3334
"pyyaml>=6.0",
3435
"openai>=1.0.0",
3536
]

0 commit comments

Comments
 (0)