fix: strip markdown code fences from LLM responses before json.loads

- glm-5.2 returns JSON wrapped in ```json ... ``` code fences
- Added fence stripping in services.py, query_understanding.py, jobs.py
This commit is contained in:
Agent Zero
2026-07-19 02:30:07 +02:00
parent 6ed0752536
commit 5c3fc027bc
3 changed files with 24 additions and 0 deletions
@@ -225,6 +225,12 @@ async def deep_analysis(
content = response.choices[0].message.content
if not content:
return
# Strip markdown code fences if present
content = content.strip()
if content.startswith("```"):
content = content.split("\n", 1)[-1] if "\n" in content else content[3:]
if content.endswith("```"):
content = content[:-3].strip()
suggestion_data = json.loads(content)
if not suggestion_data.get("title") or not suggestion_data.get("content"):
return
@@ -435,6 +435,12 @@ async def generate_suggestion(
content = response.choices[0].message.content
if not content:
return None
# Strip markdown code fences if present (e.g. ```json ... ```)
content = content.strip()
if content.startswith("```"):
content = content.split("\n", 1)[-1] if "\n" in content else content[3:]
if content.endswith("```"):
content = content[:-3].strip()
result = json.loads(content)
# Validate required fields
if not result.get("title") or not result.get("content"):