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": {}}
Problem
There are redundant parsing functions (
parse_llm_responseandparse_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