Skip to content

Conversation

@starryendymion
Copy link

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, the json.loads() call fails with a JSONDecodeError, potentially crashing the chat session.

The Issue: Fragile Parsing

The existing implementation in call_gemini_for_keywords and call_gemini_detect_intents assumes the LLM response is a perfectly formatted JSON string.

Examples of Failure Modes:

  • Markdown Formatting: The LLM wraps the JSON in ```json tags.
  • Conversational Filler: The LLM responds with: "Sure, here are the keywords: {"keywords": ["brain"]}".
  • Leading/Trailing Whitespace: Unexpected characters outside the JSON object structure.

The Fix: Robust Extraction

I introduced a private helper function _parse_llm_json that utilizes regular expressions and string slicing to extract the JSON object from a "noisy" response before parsing it.

Changes Made

  • backend/agents.py:
    • Implemented _parse_llm_json(text: str) -> dict to handle Markdown blocks and conversational text.
    • Updated call_gemini_for_keywords to use the new robust parser.
    • Updated call_gemini_detect_intents to use the new robust parser.

Verification

Verified with a test script covering:

  1. Clean JSON strings.
  2. JSON wrapped in Markdown code blocks.
  3. JSON embedded within conversational sentences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant