MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / parse_json_from_llm

Function parse_json_from_llm

src/codegraphcontext/viz/server.py:415–433  ·  view source on GitHub ↗
(text: str)

Source from the content-addressed store, hash-verified

413
414
415def parse_json_from_llm(text: str) -> dict:
416 match = re.search(r"```json\s*(.*?)\s*```", text, re.DOTALL | re.IGNORECASE)
417 if match:
418 text = match.group(1)
419 else:
420 match = re.search(r"```\s*(.*?)\s*```", text, re.DOTALL)
421 if match:
422 text = match.group(1)
423 text = text.strip()
424 try:
425 return json.loads(text)
426 except json.JSONDecodeError:
427 match = re.search(r"(\{.*\})", text, re.DOTALL)
428 if match:
429 try:
430 return json.loads(match.group(1))
431 except json.JSONDecodeError:
432 pass
433 raise ValueError(f"Could not parse valid JSON from LLM response:\n{text}")
434
435
436def parse_node(node, nodes_dict):

Callers 1

ai_queryFunction · 0.85

Calls 1

searchMethod · 0.80

Tested by

no test coverage detected