feat: implement robust LLM JSON parsing to handle markdown and filler… #40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR implements robust JSON parsing for LLM-generated responses within the backend agents. Currently, the system relies on
json.loads()directly on the raw response text. If the LLM returns Markdown code blocks (e.g.,```json ... ```) or conversational filler, thejson.loads()call fails with aJSONDecodeError, potentially crashing the chat session.The Issue: Fragile Parsing
The existing implementation in
call_gemini_for_keywordsandcall_gemini_detect_intentsassumes the LLM response is a perfectly formatted JSON string.Examples of Failure Modes:
```jsontags.{"keywords": ["brain"]}".The Fix: Robust Extraction
I introduced a private helper function
_parse_llm_jsonthat utilizes regular expressions and string slicing to extract the JSON object from a "noisy" response before parsing it.Changes Made
backend/agents.py:_parse_llm_json(text: str) -> dictto handle Markdown blocks and conversational text.call_gemini_for_keywordsto use the new robust parser.call_gemini_detect_intentsto use the new robust parser.Verification
Verified with a test script covering: