mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2026-01-05 23:34:29 +01:00
Fix various JSON input bugs in correction
By not having correct_json(json_str) in the try/except, it was still easily possible to throw Invalid JSON errors. When responses were received with no JSON at all, parsing would fail on attempting to locate the braces.
This commit is contained in:
@@ -35,8 +35,8 @@ def fix_and_parse_json(
|
|||||||
json_str = json_str.replace('\t', '')
|
json_str = json_str.replace('\t', '')
|
||||||
return json.loads(json_str)
|
return json.loads(json_str)
|
||||||
except json.JSONDecodeError as _: # noqa: F841
|
except json.JSONDecodeError as _: # noqa: F841
|
||||||
json_str = correct_json(json_str)
|
|
||||||
try:
|
try:
|
||||||
|
json_str = correct_json(json_str)
|
||||||
return json.loads(json_str)
|
return json.loads(json_str)
|
||||||
except json.JSONDecodeError as _: # noqa: F841
|
except json.JSONDecodeError as _: # noqa: F841
|
||||||
pass
|
pass
|
||||||
@@ -53,6 +53,7 @@ def fix_and_parse_json(
|
|||||||
last_brace_index = json_str.rindex("}")
|
last_brace_index = json_str.rindex("}")
|
||||||
json_str = json_str[:last_brace_index+1]
|
json_str = json_str[:last_brace_index+1]
|
||||||
return json.loads(json_str)
|
return json.loads(json_str)
|
||||||
|
# Can throw a ValueError if there is no "{" or "}" in the json_str
|
||||||
except (json.JSONDecodeError, ValueError) as e: # noqa: F841
|
except (json.JSONDecodeError, ValueError) as e: # noqa: F841
|
||||||
if try_to_fix_with_gpt:
|
if try_to_fix_with_gpt:
|
||||||
print("Warning: Failed to parse AI output, attempting to fix."
|
print("Warning: Failed to parse AI output, attempting to fix."
|
||||||
|
|||||||
Reference in New Issue
Block a user