json_in_md_parser.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import json
  2. from langchain.schema import OutputParserException
  3. def parse_json_markdown(json_string: str) -> dict:
  4. # Remove the triple backticks if present
  5. json_string = json_string.strip()
  6. start_index = json_string.find("```json")
  7. end_index = json_string.find("```", start_index + len("```json"))
  8. if start_index != -1 and end_index != -1:
  9. extracted_content = json_string[start_index + len("```json"):end_index].strip()
  10. # Parse the JSON string into a Python dictionary
  11. parsed = json.loads(extracted_content)
  12. elif start_index != -1 and end_index == -1 and json_string.endswith("``"):
  13. end_index = json_string.find("``", start_index + len("```json"))
  14. extracted_content = json_string[start_index + len("```json"):end_index].strip()
  15. # Parse the JSON string into a Python dictionary
  16. parsed = json.loads(extracted_content)
  17. elif json_string.startswith("{"):
  18. # Parse the JSON string into a Python dictionary
  19. parsed = json.loads(json_string)
  20. else:
  21. raise Exception("Could not find JSON block in the output.")
  22. return parsed
  23. def parse_and_check_json_markdown(text: str, expected_keys: list[str]) -> dict:
  24. try:
  25. json_obj = parse_json_markdown(text)
  26. except json.JSONDecodeError as e:
  27. raise OutputParserException(f"Got invalid JSON object. Error: {e}")
  28. for key in expected_keys:
  29. if key not in json_obj:
  30. raise OutputParserException(
  31. f"Got invalid return object. Expected key `{key}` "
  32. f"to be present, but got {json_obj}"
  33. )
  34. return json_obj