Last active
May 4, 2026 03:07
-
-
Save yrro/b0f2765ea55ae3414e06b319dd07ae8e to your computer and use it in GitHub Desktop.
Open WebUI filter to prevent thinking re-injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| title: Strip Thinking Tags | |
| author: Sam Morris <sam@robots.org.uk> | |
| author_url: https://gist.github.com/yrro/b0f2765ea55ae3414e06b319dd07ae8e | |
| version: 0.1 | |
| """ | |
| # Open WebUI re-injects thinking into the assistent messages in the conversation | |
| # history which is contrary to Gemma 4's requirements. | |
| # It's a workaround for <https://github.com/open-webui/open-webui/issues/23339>. | |
| from pydantic import BaseModel, Field | |
| from typing import Optional | |
| class Filter: | |
| def inlet(self, body: dict, __user__: Optional[dict] = None) -> dict: | |
| new_messages = list( | |
| self.strip_thinking(message) for message in body["messages"] | |
| ) | |
| return body | {"messages": new_messages} | |
| def strip_thinking(self, message: dict): | |
| new_message = {} | |
| if message["role"] == "assistant" and message["content"].startswith("<think>"): | |
| thinking, sep, new_content = message["content"].partition("</think>") | |
| new_message["content"] = new_content.lstrip() | |
| #print(f"new_message: {new_message!r}") | |
| return message | new_message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very very much for this! It fixed my only gripe with Gemma 4.