INNER CODE UNIT · Python
_json_object
zachlagden/Pi-hole-Optimized-Blocklists · .github/triage/triage/ai_review.py:344
def _json_object(raw: str) -> dict:
try:
return _outer_object(THINK_RE.sub("", raw).strip())
except ValueError:
thinking, closed, answer = raw.rpartition("</think>")
opening = thinking.rfind("{")
if not closed or opening < 0 or thinking[opening:].strip() not in {"{", '{"'}:
raise
return _outer_object(thinking[opening:].strip() + answer.strip())
def _call_json(system: str, content: list[dict] | str, api_key: str, attempts: int = 2, timeout: float = 240) -> dict:
for attempt in range(attempts):
try:
return _json_object(_call(system, content, api_key, timeout))
except ValueError:
if attempt == attempts - 1:
raise