[GH-ISSUE #9900] Code interpreter doesn't work if the upstream model streams <code_interpreter>...</code_interpreter> into a single chunk #15690

Closed
opened 2026-04-19 21:50:32 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @xmcp on GitHub (Feb 13, 2025).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/9900

Bug Report

Installation Method

docker

Environment

  • Open WebUI Version: v0.5.11

  • Ollama (if applicable): N/A

  • Operating System: Ubuntu 24.04

  • Browser (if applicable): N/A

Confirmation:

  • I have read and followed all the instructions provided in the README.md.
  • I am on the latest version of both Open WebUI and Ollama.
  • I have included the browser console logs.
  • I have included the Docker container logs.
  • I have provided the exact steps to reproduce the bug in the "Steps to Reproduce" section below.

Expected Behavior:

The code interpreter repeatedly fails, causing a dead loop for the model.

Actual Behavior:

The code interpreter should work.

Description

Bug Summary:

I have an OpenAI endpoint that uses fake streaming, i.e., it will put the whole output into a single text/event-stream chunk when stream=True. In this setup, when using the code interpreter feature, Open WebUI cannot end the <code_interpreter> block successfully, leading to errors when executing the code.

Reproduction Details

Steps to Reproduce:

  • Have an OpenAI endpoint that uses fake streaming by putting the whole output into a single text/event-stream chunk
  • Use Open WebUI to ask it print the current python version.
  • See the screenshots below, where we can see that </code_interpreter> and contents after that are considered part of the code block, leading to a SyntaxError.

Logs and Screenshots

Browser Console Logs:
Image

Docker Container Logs:

[{'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.', 'started_at': 1739447846.2873282, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.', 'started_at': 1739447909.5165014, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.', 'started_at': 1739447972.9585047, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above imports the sys module and prints the current Python version installed in this environment, including detailed build information such as the version number, build date, and compiler details.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above imports the sys module and prints the current Python version installed in this environment, including detailed build information such as the version number, build date, and compiler details.', 'started_at': 1739448037.8162143, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThis code imports the sys module and prints the current Python version along with additional build details (such as build date and compiler information) from sys.version. When executed in the Python environment, you\'ll see the full version information displayed.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above imports the sys module and prints the current Python version installed in this environment, including detailed build information such as the version number, build date, and compiler details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThis code imports the sys module and prints the current Python version along with additional build details (such as build date and compiler information) from sys.version. When executed in the Python environment, you\'ll see the full version information displayed.', 'started_at': 1739448102.314626, 'output': ''}, {'type': 'text', 'content': ''}] <details type="code_interpreter" done="false">
<summary>Analyzing...</summary>
```python

import sys
print("Current Python version:")
print(sys.version)
</code_interpreter>

The code above prints the current Python version being used in this environment. After running it, you'll see detailed output including version number and additional build details.
```
</details>

The above log seems to be printed at middleware.py:1794.

Screenshots/Screen Recordings (if applicable):

Image

Additional Information

I think I know the root cause of this issue, and can contribute a PR for it. I will elaborate in a separate comment below.

Originally created by @xmcp on GitHub (Feb 13, 2025). Original GitHub issue: https://github.com/open-webui/open-webui/issues/9900 # Bug Report ## Installation Method docker ## Environment - **Open WebUI Version:** v0.5.11 - **Ollama (if applicable):** N/A - **Operating System:** Ubuntu 24.04 - **Browser (if applicable):** N/A **Confirmation:** - [x] I have read and followed all the instructions provided in the README.md. - [x] I am on the latest version of both Open WebUI and Ollama. - [x] I have included the browser console logs. - [x] I have included the Docker container logs. - [x] I have provided the exact steps to reproduce the bug in the "Steps to Reproduce" section below. ## Expected Behavior: The code interpreter repeatedly fails, causing a dead loop for the model. ## Actual Behavior: The code interpreter should work. ## Description **Bug Summary:** I have an OpenAI endpoint that uses fake streaming, i.e., it will put the whole output into a single `text/event-stream` chunk when stream=True. In this setup, when using the code interpreter feature, Open WebUI cannot end the `<code_interpreter>` block successfully, leading to errors when executing the code. ## Reproduction Details **Steps to Reproduce:** - Have an OpenAI endpoint that uses fake streaming by putting the whole output into a single `text/event-stream` chunk - Use Open WebUI to ask it `print the current python version`. - See the screenshots below, where we can see that `</code_interpreter>` and contents after that are considered part of the code block, leading to a `SyntaxError`. ## Logs and Screenshots **Browser Console Logs:** ![Image](https://github.com/user-attachments/assets/17df56bc-c874-45d3-bdb3-046bca2b7e83) **Docker Container Logs:** [{'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.', 'started_at': 1739447846.2873282, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.', 'started_at': 1739447909.5165014, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.', 'started_at': 1739447972.9585047, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above imports the sys module and prints the current Python version installed in this environment, including detailed build information such as the version number, build date, and compiler details.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above imports the sys module and prints the current Python version installed in this environment, including detailed build information such as the version number, build date, and compiler details.', 'started_at': 1739448037.8162143, 'output': ''}, {'type': 'text', 'content': '<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThis code imports the sys module and prints the current Python version along with additional build details (such as build date and compiler information) from sys.version. When executed in the Python environment, you\'ll see the full version information displayed.'}, {'type': 'code_interpreter', 'tag': 'code_interpreter', 'attributes': {'type': 'code', 'lang': 'python'}, 'content': '\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used in this environment. After running it, you\'ll see detailed output including version number and additional build details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the current Python version being used, along with additional build details provided by the sys.version attribute. When executed, you\'ll see information such as the version number, build date, and compiler details, which can help you verify the Python environment.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above prints the Python version installed in the current environment. Running it will display detailed information such as the Python version number, build date, and other relevant details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThe code above imports the sys module and prints the current Python version installed in this environment, including detailed build information such as the version number, build date, and compiler details.<code_interpreter type="code" lang="python">\nimport sys\nprint("Current Python version:")\nprint(sys.version)\n</code_interpreter>\n\nThis code imports the sys module and prints the current Python version along with additional build details (such as build date and compiler information) from sys.version. When executed in the Python environment, you\'ll see the full version information displayed.', 'started_at': 1739448102.314626, 'output': ''}, {'type': 'text', 'content': ''}] <details type="code_interpreter" done="false"> <summary>Analyzing...</summary> ```python import sys print("Current Python version:") print(sys.version) </code_interpreter> The code above prints the current Python version being used in this environment. After running it, you'll see detailed output including version number and additional build details. ``` </details> The above log seems to be printed at [middleware.py:1794](https://github.com/open-webui/open-webui/blob/v0.5.11/backend/open_webui/utils/middleware.py#L1794). **Screenshots/Screen Recordings (if applicable):** ![Image](https://github.com/user-attachments/assets/70b7ac0c-b974-462d-bc1f-00d936e7de4e) ## Additional Information I think I know the root cause of this issue, and can contribute a PR for it. I will elaborate in a separate comment below.
Author
Owner

@xmcp commented on GitHub (Feb 13, 2025):

The root cause seems to be the tag_content_handler function in middleware.py, which looks like:

def tag_content_handler(content_type, tags, content, content_blocks):
    if content_blocks[-1]["type"] == "text":
        ... # checks for start tag, i.e., `<code_interpreter attrs>`
    elif content_blocks[-1]["type"] == content_type:
        ... # checks for end tag, i.e., `</code_interpreter>`
    return content, content_blocks, end_flag

This function will be called once for each streamed chunk. If <code_interpreter>...</code_interpreter> is returned within a single chunk, only the code path for the start tag is executed, skipping the code path for the end tag.

Therefore, the whole chunk including the content after </code_interpreter> is incorrectly considered the content in the code block.

<!-- gh-comment-id:2656525900 --> @xmcp commented on GitHub (Feb 13, 2025): The root cause seems to be the `tag_content_handler` function in `middleware.py`, which looks like: ```python def tag_content_handler(content_type, tags, content, content_blocks): if content_blocks[-1]["type"] == "text": ... # checks for start tag, i.e., `<code_interpreter attrs>` elif content_blocks[-1]["type"] == content_type: ... # checks for end tag, i.e., `</code_interpreter>` return content, content_blocks, end_flag ``` This function will be called once for each streamed chunk. If `<code_interpreter>...</code_interpreter>` is returned within a single chunk, only the code path for the start tag is executed, skipping the code path for the end tag. Therefore, the whole chunk including the content after `</code_interpreter>` is incorrectly considered the content in the code block.
Author
Owner

@xmcp commented on GitHub (Feb 13, 2025):

FYI, the below patch solves this issue in my setup

diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py
index fd2d0d259..fc4c4f379 100644
--- a/backend/open_webui/utils/middleware.py
+++ b/backend/open_webui/utils/middleware.py
@@ -1210,6 +1210,7 @@ async def process_chat_response(
 
                             if after_tag:
                                 content_blocks[-1]["content"] = after_tag
+                                return tag_content_handler(content_type, tags, content, content_blocks)
 
                             break
                 elif content_blocks[-1]["type"] == content_type:

Image

<!-- gh-comment-id:2656627038 --> @xmcp commented on GitHub (Feb 13, 2025): FYI, the below patch solves this issue in my setup ```diff diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index fd2d0d259..fc4c4f379 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1210,6 +1210,7 @@ async def process_chat_response( if after_tag: content_blocks[-1]["content"] = after_tag + return tag_content_handler(content_type, tags, content, content_blocks) break elif content_blocks[-1]["type"] == content_type: ``` ![Image](https://github.com/user-attachments/assets/6c26e977-d0b9-4c55-81c3-074d3610487c)
Author
Owner

@tjbck commented on GitHub (Feb 13, 2025):

PR Welcome!

<!-- gh-comment-id:2657715722 --> @tjbck commented on GitHub (Feb 13, 2025): PR Welcome!
Author
Owner

@tjbck commented on GitHub (Feb 14, 2025):

I highly suspect the issue is caused from the model-end. Suggested PR does not resolve the issue entirely.

<!-- gh-comment-id:2658413718 --> @tjbck commented on GitHub (Feb 14, 2025): I highly suspect the issue is caused from the model-end. Suggested PR does not resolve the issue entirely.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#15690