Skip to content

feat(utils): add json_utils module for robust LLM JSON parsing#101

Open
JasonOA888 wants to merge 1 commit into666ghj:mainfrom
JasonOA888:feat/json-utils-helper
Open

feat(utils): add json_utils module for robust LLM JSON parsing#101
JasonOA888 wants to merge 1 commit into666ghj:mainfrom
JasonOA888:feat/json-utils-helper

Conversation

@JasonOA888
Copy link

Problem

Some LLM models don't respect json_object format and return markdown-wrapped JSON:

```json
{"key": "value"}

This causes `json.loads()` to fail with JSONDecodeError.

Affected models: MiniMax M2.5, GLM-4.7, GLM-5

Related issues: #72, #64, #58, #48

## Solution

Add `json_utils.py` module with:

- `clean_llm_json_response()` - Strip markdown code blocks
- `parse_llm_json()` - Parse with auto-cleanup, optional default value
- `safe_parse_llm_json()` - Non-throwing version returning `(success, result)`

## Usage

```python
from app.utils import parse_llm_json

# Auto-strips ```json ... ``` wrappers
result = parse_llm_json(llm_response)

# With default fallback
result = parse_llm_json(llm_response, default={})

# Safe version (no exceptions)
success, result = safe_parse_llm_json(llm_response)

Changes

  • New file: backend/app/utils/json_utils.py
  • Update: backend/app/utils/llm_client.py uses new helper
  • Update: backend/app/utils/__init__.py exports new functions

Some models (MiniMax M2.5, GLM-4.7, GLM-5) don't respect json_object
format and return markdown-wrapped JSON, causing json.loads() to fail.

Changes:
- Add json_utils.py with clean_llm_json_response(), parse_llm_json()
- Auto-strip markdown code blocks (```json ... ```)
- Provide safe_parse_llm_json() for non-throwing usage
- Update llm_client.py to use new helper

Related: 666ghj#72, 666ghj#64, 666ghj#58, 666ghj#48
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request labels Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant