File size: 1,072 Bytes
0dd7c80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""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()