...a text.
    """
    t = text.strip()

    # remove ```json fences if present
    t = re.sub(r"^\s*```(?:json)?\s*", "", t, flags=re.IGNORECASE)
    t = re.sub(r"\s*```\s*$", "", t)

    # find first {...} block (greedy but bounded by last brace)
    first = t.find("{")
    last = t.rfind("}")
    if first != -1 and last != -1 and last > first:
        t = t[first:last + 1].strip()
    return t


def ...