fix(json_in_md_parser): improve error messages for JSON parsing failures (#11948)
Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
parent
a056a9d601
commit
90f093eb67
@ -27,7 +27,7 @@ def parse_json_markdown(json_string: str) -> dict:
|
|||||||
extracted_content = json_string[start_index:end_index].strip()
|
extracted_content = json_string[start_index:end_index].strip()
|
||||||
parsed = json.loads(extracted_content)
|
parsed = json.loads(extracted_content)
|
||||||
else:
|
else:
|
||||||
raise Exception("Could not find JSON block in the output.")
|
raise ValueError("could not find json block in the output.")
|
||||||
|
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
@ -36,10 +36,10 @@ def parse_and_check_json_markdown(text: str, expected_keys: list[str]) -> dict:
|
|||||||
try:
|
try:
|
||||||
json_obj = parse_json_markdown(text)
|
json_obj = parse_json_markdown(text)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
raise OutputParserError(f"Got invalid JSON object. Error: {e}")
|
raise OutputParserError(f"got invalid json object. error: {e}")
|
||||||
for key in expected_keys:
|
for key in expected_keys:
|
||||||
if key not in json_obj:
|
if key not in json_obj:
|
||||||
raise OutputParserError(
|
raise OutputParserError(
|
||||||
f"Got invalid return object. Expected key `{key}` to be present, but got {json_obj}"
|
f"got invalid return object. expected key `{key}` to be present, but got {json_obj}"
|
||||||
)
|
)
|
||||||
return json_obj
|
return json_obj
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user