Skip to content

2. Code Duplication in Parsing Functions #3

Description

@sniperx-19

Problem

There are redundant parsing functions (parse_llm_response and parse_llm_summary) that perform similar tasks, leading to code duplication.
This increases maintenance overhead and creates risk of inconsistent behavior when one parser is updated but the other is not.

Fix

Create a generic parsing function that can handle different parsing requirements, following the DRY (Don't Repeat Yourself) principle.
This centralizes parsing logic and makes future changes easier and safer.

Updated Code

import re
import json

def parse_llm_response_generic(
    response_text: str,
    json_pattern: str = r'```json\n(.*?)```'
) -> dict:
    """
    Generic function to parse LLM response for both thinking process and JSON results.
    """
    try:
        # Extract thinking process
        think_pattern = r'<think>(.*?)</think>'
        thinking = re.search(think_pattern, response_text, re.DOTALL)
        thinking = thinking.group(1).strip() if thinking else ""

        # Extract JSON response
        json_match = re.search(json_pattern, response_text, re.DOTALL)
        json_str = json_match.group(1).strip() if json_match else "{}"
        json_data = json.loads(json_str)

        return {
            "thinking": thinking,
            "response": json_data
        }

    except Exception as e:
        print(f"Error parsing LLM response: {str(e)}")
        return {"thinking": "", "response": {}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions