band-aider-pub/band_aider/ai_client/json_response_decoder.py
2026-07-16 18:38:58 -07:00

24 lines
634 B
Python

# SPDX-FileCopyrightText: 2026 Art Sea LLC
#
# SPDX-License-Identifier: AGPL-3.0-only
from __future__ import annotations
def strip_json_fence(response: str) -> str:
"""Strip one markdown JSON/text fence from a model response when present."""
text = response.strip()
if not text.startswith("```"):
return text
lines = text.splitlines()
opening = lines[0].strip().lower()
language = opening[3:].strip()
if language not in {"", "json", "text"}:
return text
inner = lines[1:]
if inner and inner[-1].strip() == "```":
inner = inner[:-1]
return "\n".join(inner).strip()