json_in_md_parser.py 1.3 KB

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