Reasoning LLM Output Cleaner
Strip the Chain-of-Thought Off a Reasoning Model's Answer
- cleaned_text
Reasoning LLMs are great at writing prompts, and terrible at shutting up about it. A model like Gemini or DeepSeek-R1 doesn't just hand you an answer - it hands you a wall of <think> / <thinking> chain-of-thought first, and sometimes the reasoning is wrapped around the answer so the whole thing comes back as one blob. Feed that to a CLIP text encoder and you're encoding the model's inner monologue instead of your prompt. This node's entire job is to delete the thinking and keep the answer.
How it works
It's a regex and a trim. The text input gets scrubbed with re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL) - the DOTALL flag matters, because the think block is usually multi-line and a plain . wouldn't match newlines - and the non-greedy .*? keeps it from eating everything between two separate tags. Then it strip()s leading and trailing whitespace. One input (text), one output (cleaned_text).
Simple as it looks, it's the right complement to the pack's own OpenRouterApiTlantV1: that node already extracts reasoning_content into a separate output, but if you've got remove_think_tags off, or you're chaining a different LLM's output through a text node, the <think> blocks land in your prompt string and this is the clean-up pass.
Install
Part of the Tlant pack:
cd ComfyUI/custom_nodes
git clone https://github.com/Tlant/ComfyUI-OllamaPromptsGeneratorTlant
restart, or search "Reasoning LLM Output Cleaner" in ComfyUI Manager.
Where people get tripped up
- It only handles
<think>/<thinking>, not every wrapper. The OpenRouter node also emitsreasoning_contentas a separate field - this cleaner won't touch that (it's already separate). And some models wrap reasoning in other markers ([Thought],---fences) that this regex doesn't know about. If your output still has noise, that's why. - Empty output is fine. If the whole string was a think block,
cleaned_textcomes back as an empty string. That's honest, if annoying - check for it before encoding. - It's not a general text cleaner. It won't strip XML, markdown, or the model saying "Here is your prompt:". For that you want a proper formatter node.
There's no magic here - it's one regex done right, in a spot every reasoning-LLM workflow eventually needs. Keep it between your LLM output and your text encoder.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| cleaned_text | STRING | — |