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
@@ -104,6 +104,12 @@ async def llm_analyze_query(
response = await litellm.acompletion(**litellm_kwargs)
content = response.choices[0].message.content
# 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()
return json.loads(content)
except Exception:
logger.warning("LLM query analysis failed, using fallback", exc_info=True)
@@ -150,6 +156,12 @@ async def llm_aggregate_results(
response = await litellm.acompletion(**litellm_kwargs)
content = response.choices[0].message.content
# 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()
return json.loads(content)
except Exception:
logger.warning("LLM result aggregation failed, using fallback", exc_info=True)