Skip to content

Commit 7d6446e

Browse files
committed
fix: normalize Python-style booleans in LLM JSON responses
Some LLM providers (e.g. Volcengine) return Python-style booleans (True/False/None) instead of JSON-standard (true/false/null). This causes json.loads to fail with JSONDecodeError. Fixes #916
1 parent 1922759 commit 7d6446e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

rdagent/core/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def __reduce__(self) -> NoReturn:
5656

5757

5858
def parse_json(response: str) -> Any:
59+
import re
60+
61+
# Some LLM providers (e.g. Volcengine) return Python-style booleans
62+
# (True/False) instead of JSON-standard (true/false). Normalize them
63+
# before parsing to avoid JSONDecodeError.
64+
response = re.sub(r"\bTrue\b", "true", response)
65+
response = re.sub(r"\bFalse\b", "false", response)
66+
response = re.sub(r"\bNone\b", "null", response)
5967
try:
6068
return json.loads(response)
6169
except json.decoder.JSONDecodeError:

rdagent/log/utils/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ def extract_evoid(tag: str) -> str | None:
9999
def extract_json(log_content: str) -> dict | None:
100100
match = re.search(r"\{.*\}", log_content, re.DOTALL)
101101
if match:
102-
return cast(dict, json.loads(match.group(0)))
102+
raw = match.group(0)
103+
# Normalize Python-style booleans/null that some LLM providers return
104+
raw = re.sub(r"\bTrue\b", "true", raw)
105+
raw = re.sub(r"\bFalse\b", "false", raw)
106+
raw = re.sub(r"\bNone\b", "null", raw)
107+
return cast(dict, json.loads(raw))
103108
return None
104109

105110

0 commit comments

Comments
 (0)