[GH-ISSUE #20941] feat: Code Interpreter: strip markdown code fences before executing in Pyodide #34865

Closed
opened 2026-04-25 09:02:55 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @mgcrea on GitHub (Jan 26, 2026).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/20941

Check Existing Issues

  • I have searched for all existing open AND closed issues and discussions for similar requests. I have found none that is comparable to my request.

Verify Feature Scope

  • I have read through and understood the scope definition for feature requests in the Issues section. I believe my feature request meets the definition and belongs in the Issues section instead of the Discussions.

Problem Description

Bug description

When using the Code Interpreter with models that wrap code in markdown fences (```python ... ```), the Pyodide runtime receives the backticks as part of the source code, causing:

File "<exec>", line 1
    ```py
    ^
SyntaxError: invalid syntax

This is a very common LLM behavior, most models are trained to emit markdown-fenced code blocks. The current approach relies entirely on the system prompt to suppress this, which is fragile and model-dependent.

Desired Solution you'd like

Proposed fix

Before passing code to Pyodide's eval_code_async, strip leading/trailing markdown code fences:

import re

def sanitize_code_block(code: str) -> str:
    """Strip markdown code fences if present."""
    code = code.strip()
    # Remove opening fence (```python, ```py, ``` etc.)
    code = re.sub(r'^```\w*\n?', '', code)
    # Remove closing fence
    code = re.sub(r'\n?```\s*$', '', code)
    return code.strip()

This is a defensive, non-breaking change — if the code doesn't contain fences, it passes through unchanged.

Alternatives Considered

Why this matters

Additional Context

Environment

  • Open WebUI: v0.7.2
  • Code Interpreter: Pyodide (browser-side)
  • Model: Qwen (via vLLM + LiteLLM), but model-independent issue
Originally created by @mgcrea on GitHub (Jan 26, 2026). Original GitHub issue: https://github.com/open-webui/open-webui/issues/20941 ### Check Existing Issues - [x] I have searched for all existing **open AND closed** issues and discussions for similar requests. I have found none that is comparable to my request. ### Verify Feature Scope - [x] I have read through and understood the scope definition for feature requests in the Issues section. I believe my feature request meets the definition and belongs in the Issues section instead of the Discussions. ### Problem Description #### Bug description When using the Code Interpreter with models that wrap code in markdown fences (` ```python ... ``` `), the Pyodide runtime receives the backticks as part of the source code, causing: ``` File "<exec>", line 1 ```py ^ SyntaxError: invalid syntax ``` This is a very common LLM behavior, most models are trained to emit markdown-fenced code blocks. The current approach relies entirely on the system prompt to suppress this, which is fragile and model-dependent. ### Desired Solution you'd like #### Proposed fix Before passing code to Pyodide's `eval_code_async`, strip leading/trailing markdown code fences: ```python import re def sanitize_code_block(code: str) -> str: """Strip markdown code fences if present.""" code = code.strip() # Remove opening fence (```python, ```py, ``` etc.) code = re.sub(r'^```\w*\n?', '', code) # Remove closing fence code = re.sub(r'\n?```\s*$', '', code) return code.strip() ``` This is a defensive, non-breaking change — if the code doesn't contain fences, it passes through unchanged. ### Alternatives Considered #### Why this matters - Related: #11908, Discussion #9440 - The existing issues recommend "use a better model" or "fix the prompt" — but this is a one-line fix in the execution layer that makes the feature robust regardless of model - Currently affects most non-GPT models (tested with Qwen via vLLM/LiteLLM, also reported for Gemma 3, Claude 3.5 Sonnet) - Models that self-correct after errors waste tokens and user patience (in my case, 2 failed attempts before succeeding) ### Additional Context #### Environment - Open WebUI: v0.7.2 - Code Interpreter: Pyodide (browser-side) - Model: Qwen (via vLLM + LiteLLM), but model-independent issue
Author
Owner

@tjbck commented on GitHub (Jan 26, 2026):

Should be addressed in dev.

<!-- gh-comment-id:3799783820 --> @tjbck commented on GitHub (Jan 26, 2026): Should be addressed in dev.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#34865