feat: implement robust LLM JSON parsing to handle markdown and filler…#40
Closed
divye-joshi wants to merge 1 commit intoINCF:mainfrom
Closed
feat: implement robust LLM JSON parsing to handle markdown and filler…#40divye-joshi wants to merge 1 commit intoINCF:mainfrom
divye-joshi wants to merge 1 commit intoINCF:mainfrom
Conversation
Collaborator
|
The _parse_llm_json utility is genuinely useful and handles markdown code fences and fallback extraction correctly — this is a real problem worth solving. However this PR also bundles rerank_results_using_metadata and expand_query functions that appear copied from other open PRs (#38, #39), and the reranking score math has the same issue (publication_year / 10000 as a score boost is numerically incorrect). Please open a focused PR with only the JSON parsing fix and drop the unrelated functions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: