"""Remove markdown code block wrapping from LLM responses."""

import re


def strip_markdown_codeblock(text: str) -> str:
    """Remove markdown code block wrapping from LLM responses.

    LLMs sometimes wrap JSON (or other content) in markdown code fences like:
        ```json
        {"key": "value"}
        ```
    This function unwraps the content so it can be parsed directly.

    Args:
        text: Text that may be wrapped in a markdown code block.

    Returns:
        Unwrapped text with leading/trailing whitespace stripped.
    """
    text = text.strip()
    text = re.sub(r'^```(?:json)?\s*\n?', '', text, flags=re.IGNORECASE)
    text = re.sub(r'\n?```\s*$', '', text)
    return text.strip()
