Skip to content

Commit cb91618

Browse files
author
marwan37
committed
coerce potential lists being returned in model responses to strings
1 parent 09c5704 commit cb91618

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

omni-reader/utils/extract_json.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
from typing import Any, Dict
2121

2222

23+
def _ensure_string_raw_text(result: Dict) -> Dict:
24+
"""Ensure raw_text in result is a string and not a list.
25+
26+
Args:
27+
result: Dictionary that may contain a raw_text field
28+
29+
Returns:
30+
Dictionary with raw_text converted to string if needed
31+
"""
32+
if "raw_text" in result and isinstance(result["raw_text"], list):
33+
result["raw_text"] = "\n".join(result["raw_text"])
34+
return result
35+
36+
2337
def try_extract_json_from_response(response: Any) -> Dict:
2438
"""Extract JSON from a response, handling various formats efficiently.
2539
@@ -30,7 +44,7 @@ def try_extract_json_from_response(response: Any) -> Dict:
3044
Dict with extracted data.
3145
"""
3246
if isinstance(response, dict) and "raw_text" in response:
33-
return response
47+
return _ensure_string_raw_text(response)
3448

3549
response_text = ""
3650
if hasattr(response, "choices") and response.choices:
@@ -40,30 +54,34 @@ def try_extract_json_from_response(response: Any) -> Dict:
4054
elif isinstance(response, str):
4155
response_text = response
4256
elif hasattr(response, "raw_text"):
43-
return {"raw_text": response.raw_text, "confidence": getattr(response, "confidence", None)}
57+
raw_text = response.raw_text
58+
result = {"raw_text": raw_text, "confidence": getattr(response, "confidence", None)}
59+
return _ensure_string_raw_text(result)
4460

4561
response_text = response_text.strip()
4662

4763
try:
4864
parsed = json.loads(response_text)
4965
if isinstance(parsed, dict):
50-
return parsed
66+
return _ensure_string_raw_text(parsed)
5167
except json.JSONDecodeError:
5268
pass
5369

5470
json_block = re.search(r"```json\s*(.*?)\s*```", response_text, re.DOTALL)
5571
if json_block:
5672
json_str = json_block.group(1).strip()
5773
try:
58-
return json.loads(json_str)
74+
parsed = json.loads(json_str)
75+
return _ensure_string_raw_text(parsed)
5976
except json.JSONDecodeError:
6077
pass
6178

6279
json_substring = re.search(r"\{.*\}", response_text, re.DOTALL)
6380
if json_substring:
6481
json_str = json_substring.group(0).strip()
6582
try:
66-
return json.loads(json_str)
83+
parsed = json.loads(json_str)
84+
return _ensure_string_raw_text(parsed)
6785
except json.JSONDecodeError:
6886
pass
6987

0 commit comments

Comments
 (0)