| """Pull executable Python source out of an LLM's completion. | |
| Qwen-2.5-Coder-Instruct wraps code in ```python ... ``` fences when given a | |
| system prompt asking for a code block. Sometimes it emits bare code, or | |
| multiple fences, or adds commentary before/after. This module normalizes | |
| all of those into a single Python source string. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| # Prefer the Python-tagged fence; fall back to any fenced block. | |
| _PY_FENCE = re.compile(r"```python\s*\n(.*?)```", re.DOTALL | re.IGNORECASE) | |
| _ANY_FENCE = re.compile(r"```[^\n]*\n(.*?)```", re.DOTALL) | |
| def extract_python_code(text: str) -> str: | |
| """Return the Python source from a possibly-fenced LLM completion. | |
| Preference order: | |
| 1. First ```python ... ``` block. | |
| 2. First ``` ... ``` block of any language. | |
| 3. The whole text (the model forgot to fence). | |
| Leading/trailing whitespace is stripped. | |
| """ | |
| match = _PY_FENCE.search(text) or _ANY_FENCE.search(text) | |
| if match: | |
| return match.group(1).strip("\n").rstrip() | |
| return text.strip() | |