24 lines
634 B
Python
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()
|