Skip to content

[Bug] Brittle JSON Extraction in llm_analysis.py leads to Unhandled Exceptions #1008

@sonusharma6-dsa

Description

@sonusharma6-dsa

Summary

In backend/app/services/llm_analysis.py, the _extract_json() method naively searches for the first { and the last } to slice out the JSON payload. If the LLM generates any valid braces before or after the main JSON block, the slice becomes invalid JSON. Additionally, the method invokes json.loads without catching json.JSONDecodeError, allowing the exception to bubble up and crash async tasks unexpectedly.

Impact

Frequent 500 errors or failed background jobs when the LLM hallucination rate is high, reducing service stability and reliability.

Proposed Fix

Implement a robust JSON extraction mechanism (e.g., using regex to find matching top-level JSON objects) and wrap json.loads in a try...except block, raising a custom LLMAnalysisError upon failure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions