Merged in feature/print_string_when_failed_json_load (pull request #489)

Enhance universal_json_load function with detailed error handling and traceback logging

* Enhance universal_json_load function with detailed error handling and traceback logging

* Add type hint for string_dict parameter in universal_json_load function

* Update return type annotation for universal_json_load function to reflect possible return values


Approved-by: Katon Minhas
This commit is contained in:
Alex Galarce
2025-04-22 21:48:45 +00:00
parent e5e5d5d1fa
commit e709a851c7
+20 -6
View File
@@ -5,6 +5,7 @@ import warnings
import numpy as np
import pandas as pd
import traceback
from src.prompts import preprocessing_prompts
import src.utils.llm_utils as llm_utils
@@ -224,15 +225,28 @@ def primary_string_to_dict(string_dict, filename):
data.append(dict_)
return data
def universal_json_load(string_dict):
"""Matches json dicts and list of dicts - NOT simple lists"""
def universal_json_load(string_dict: str):
"""Matches json dicts and list of dicts - NOT simple lists
Args:
string_dict (str): The string containing the JSON-like content.
Returns:
list[dict] | dict: The dictionary or list of dictionaries obtained from parsing the cleaned and corrected string.
Raises:
ValueError: If no valid JSON object is found in the input string. In addition,
the traceback as well as the offending string are printed.
"""
match = re.search(r'\{.*\}|\[\s*?\{.*\}\s*?\]', string_dict, re.DOTALL) # Updated regex
if match:
matched_str = match.group()
try:
return json.loads(match.group())
except json.JSONDecodeError:
pass
raise ValueError("No valid JSON object found in the input string")
return json.loads(matched_str)
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {e}")
print(f"Problematic JSON string: {matched_str}")
print(f"Original string: {string_dict[:200]}{'...' if len(string_dict) > 200 else ''}") # Print first 200 chars
print(f"Traceback: {traceback.format_exc()}")
raise ValueError(f"JSON parsing failed: {e.msg} at position {e.pos}") from e
else:
return {}