What Is the clean_thinking_content Utility Function in Open Notebook?

The clean_thinking_content function strips internal LLM reasoning metadata from AI responses, returning only user-facing content by extracting and discarding <thinking> tags and normalizing whitespace.

In the lfnovo/open-notebook repository, AI-generated responses often contain hidden reasoning chains wrapped in <thinking> tags or malformed blocks. The clean_thinking_content utility provides a convenient way to sanitize this output, ensuring end-users see only the final, polished result while the workflow engine retains access to the raw metadata for debugging.

Why LLM Responses Need Sanitization

Large language models frequently embed internal reasoning within their outputs using <thinking>...</thinking> XML tags. While this metadata is valuable for debugging and workflow orchestration, it creates noise when presented directly to users. The clean_thinking_content function solves this by extracting the thought process and returning only the substantive response, with proper whitespace handling to ensure presentation-ready formatting.

How clean_thinking_content Works

The function operates as a convenience wrapper around the lower-level parse_thinking_content parser. Rather than handling tuple unpacking and indexing throughout the codebase, callers use this single-purpose utility to get immediately usable text.

Delegation to parse_thinking_content

When invoked, clean_thinking_content calls parse_thinking_content internally, which returns a tuple containing both the extracted thinking content and the remaining user-facing text. The utility discards the first element (the thinking metadata) and returns only the second element.

Whitespace Normalization

The function ensures the output is clean and consistent. Through its delegation to parse_thinking_content, it automatically handles:

  • Removal of the <thinking> tags themselves
  • Stripping of leading and trailing whitespace
  • Normalization of internal spacing in the final answer

Implementation Details

According to the source code analysis, the function follows these key principles:

  • Extract and discard – It calls parse_thinking_content and ignores the first tuple element (thinking_content), keeping only the user-facing portion.
  • Single-source-of-truth – By centralizing the parsing logic in parse_thinking_content, the utility prevents code duplication across the codebase.
  • Malformed input handling – The underlying parser gracefully handles malformed blocks (such as missing opening tags), ensuring robust extraction even when LLM output is imperfect.

Usage Examples

The following examples demonstrate how clean_thinking_content handles various input scenarios:


# Example 1 – Standard thinking tags

raw = "<thinking>Internal reasoning about the query...</thinking>Here is the final answer."
print(clean_thinking_content(raw))

# → "Here is the final answer."

# Example 2 – Malformed output (missing opening tag)

raw = "Thinking about it...Result ready."
print(clean_thinking_content(raw))

# → "Result ready."

# Example 3 – No thinking content present

raw = "Direct answer without any metadata."
print(clean_thinking_content(raw))

# → "Direct answer without any metadata."

Summary

  • clean_thinking_content delegates parsing to parse_thinking_content and returns only the user-facing text portion.
  • It automatically handles whitespace cleanup and malformed tag scenarios.
  • The function serves as the presentation layer for AI responses in Open Notebook, ensuring users see clean output while the system retains debugging metadata.
  • It prevents code duplication by centralizing extraction logic in a single utility function.

Frequently Asked Questions

What is the difference between clean_thinking_content and parse_thinking_content?

parse_thinking_content returns a tuple containing both the extracted thinking content and the remaining text, useful when you need to analyze the reasoning chain. clean_thinking_content is a convenience wrapper that calls the parser and returns only the cleaned user-facing text, discarding the thinking metadata entirely.

Does clean_thinking_content handle malformed thinking tags?

Yes. According to the Open Notebook source code implementation, the function handles malformed blocks (such as missing opening <thinking> tags) gracefully. The underlying parser extracts what it can and returns the most coherent user-facing text possible.

When should I use clean_thinking_content in the Open Notebook codebase?

Use this utility whenever you need to display AI-generated content to end-users or store final results in the database. It is ideal for workflow steps where the reasoning chain is no longer needed and only the substantive answer should persist.

Does this function modify the original string or return a new string?

clean_thinking_content returns a new string with the thinking content removed and whitespace normalized. It does not modify the input string in place, following standard Python string immutability patterns.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →