MCPcopy Create free account
hub / github.com/VectifyAI/OpenKB / _parse_json

Function _parse_json

openkb/agent/compiler.py:518–531  ·  view source on GitHub ↗

Parse JSON from LLM response, handling fences, prose, and malformed JSON.

(text: str)

Source from the content-addressed store, hash-verified

516
517
518def _parse_json(text: str) -> list | dict:
519 """Parse JSON from LLM response, handling fences, prose, and malformed JSON."""
520 from json_repair import repair_json
521
522 cleaned = text.strip()
523 if cleaned.startswith("```"):
524 first_nl = cleaned.find("\n")
525 cleaned = cleaned[first_nl + 1 :] if first_nl != -1 else cleaned[3:]
526 if cleaned.endswith("```"):
527 cleaned = cleaned[:-3]
528 result = json.loads(repair_json(cleaned.strip()))
529 if not isinstance(result, (dict, list)):
530 raise ValueError(f"Expected JSON object or array, got {type(result).__name__}")
531 return result
532
533
534def _parse_page_json(text: str) -> dict | None:

Callers 11

test_plain_jsonMethod · 0.90
test_fenced_jsonMethod · 0.90
test_invalid_jsonMethod · 0.90
test_dict_formatMethod · 0.90
test_fenced_dictMethod · 0.90
_parse_page_jsonFunction · 0.85
_compile_conceptsFunction · 0.85
compile_short_docFunction · 0.85

Calls

no outgoing calls

Tested by 8

test_plain_jsonMethod · 0.72
test_fenced_jsonMethod · 0.72
test_invalid_jsonMethod · 0.72
test_dict_formatMethod · 0.72
test_fenced_dictMethod · 0.72