openclaw - ✅(Solved) Fix Bug: Gemini model responses expose internal <think> blocks to the user [1 pull requests, 4 comments, 5 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#48968Fetched 2026-04-08 00:50:20
View on GitHub
Comments
4
Participants
5
Timeline
7
Reactions
1
Author
Timeline (top)
commented ×4cross-referenced ×2subscribed ×1

When the agent is configured to use a Gemini model (e.g., google-gemini-cli/gemini-2.5-pro), the internal reasoning block (<think>...</think>) is not stripped from the final response. The entire raw output, including the agent's thoughts, is sent to the user interface.

To Reproduce

  1. Configure an agent to use a Gemini model.
  2. Start a conversation with the agent.
  3. Observe that the agent's reply includes the <think>...</think> block.

Expected Behavior

The <think>...</think> block should be processed internally and stripped from the final output before it is sent to the user. Only the content intended for the user should be visible.

Actual Behavior

The raw model output, including the <think> block, is delivered to the user, exposing the agent's internal monologue and creating a confusing user experience.

Environment

  • Model: google-gemini-cli/gemini-2.5-pro
  • OpenClaw Version: 2026.3.14
  • Interface: openclaw-tui

Root Cause

When the agent is configured to use a Gemini model (e.g., google-gemini-cli/gemini-2.5-pro), the internal reasoning block (<think>...</think>) is not stripped from the final response. The entire raw output, including the agent's thoughts, is sent to the user interface.

To Reproduce

  1. Configure an agent to use a Gemini model.
  2. Start a conversation with the agent.
  3. Observe that the agent's reply includes the <think>...</think> block.

Expected Behavior

The <think>...</think> block should be processed internally and stripped from the final output before it is sent to the user. Only the content intended for the user should be visible.

Actual Behavior

The raw model output, including the <think> block, is delivered to the user, exposing the agent's internal monologue and creating a confusing user experience.

Environment

  • Model: google-gemini-cli/gemini-2.5-pro
  • OpenClaw Version: 2026.3.14
  • Interface: openclaw-tui

Fix Action

Fixed

PR fix notes

PR #48593: Fix Gemini final tags leaking into delivered messages

Description (problem / solution / changelog)

Summary

Fixes issue #48587 where Google Gemini model responses leak <final> tag fragments into delivered messages on channels like Telegram.

The root cause was inconsistent regex patterns for matching <final> tags across different parts of the codebase:

  • The main reasoning tags logic used a robust regex that handles attributes: /<\s*\/?\s*final\b[^<>]*>/gi
  • The streaming logic used a restrictive regex that only matched basic tags: /<\s*(\/?)\s*final\s*>/gi

When Gemini outputs tags with attributes like <final id="1">content</final>, the streaming regex would miss them, causing fragments like </final> to leak through to the delivered message.

Changes

Updated all final tag regexes to use the robust pattern that handles attributes:

  • src/agents/pi-embedded-subscribe.ts: Updated FINAL_TAG_SCAN_RE
  • src/agents/pi-embedded-helpers/errors.ts: Updated FINAL_TAG_RE
  • src/agents/pi-embedded-subscribe.handlers.messages.ts: Updated inline regex
  • src/gateway/gateway-models.profiles.live.test.ts: Updated test helper regex

Test plan

  • Review existing tests in reasoning-tags.test.ts to ensure they cover final tag behavior
  • Verify regex patterns now consistently match final tags with attributes
  • Confirm the fix addresses the specific issue described in #48587

Impact

This fix ensures that Google Gemini model responses are properly cleaned before delivery to messaging channels, eliminating the tag fragments that were previously leaking through.

🤖 Generated with Claude Code

Changed files

  • src/agents/pi-embedded-helpers/errors.ts (modified, +1/-1)
  • src/agents/pi-embedded-subscribe.handlers.messages.ts (modified, +1/-1)
  • src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.filters-final-suppresses-output-without-start-tag.test.ts (modified, +38/-0)
  • src/agents/pi-embedded-subscribe.ts (modified, +15/-3)
  • src/gateway/gateway-models.profiles.live.test.ts (modified, +1/-1)
  • src/shared/text/reasoning-tags.test.ts (modified, +4/-0)
  • src/shared/text/reasoning-tags.ts (modified, +6/-1)
RAW_BUFFERClick to expand / collapse

Description

When the agent is configured to use a Gemini model (e.g., google-gemini-cli/gemini-2.5-pro), the internal reasoning block (<think>...</think>) is not stripped from the final response. The entire raw output, including the agent's thoughts, is sent to the user interface.

To Reproduce

  1. Configure an agent to use a Gemini model.
  2. Start a conversation with the agent.
  3. Observe that the agent's reply includes the <think>...</think> block.

Expected Behavior

The <think>...</think> block should be processed internally and stripped from the final output before it is sent to the user. Only the content intended for the user should be visible.

Actual Behavior

The raw model output, including the <think> block, is delivered to the user, exposing the agent's internal monologue and creating a confusing user experience.

Environment

  • Model: google-gemini-cli/gemini-2.5-pro
  • OpenClaw Version: 2026.3.14
  • Interface: openclaw-tui

extent analysis

Fix Plan

To address the issue of the internal reasoning block (<think>...</think>) not being stripped from the final response, we need to modify the response processing pipeline.

Here are the steps:

  • Update the response_processor.py to include a function that removes the <think>...</think> block from the model output.
  • Modify the agent.py to use this new function when generating the final response.

Example Code

import re

def strip_think_block(response):
    """Remove the <think>...</think> block from the response."""
    pattern = r'<think>.*?</think>'
    return re.sub(pattern, '', response, flags=re.DOTALL)

# In agent.py
class Agent:
    # ...
    def generate_response(self, model_output):
        # ...
        final_response = strip_think_block(model_output)
        return final_response

Verification

To verify that the fix worked, follow these steps:

  • Configure an agent to use a Gemini model.
  • Start a conversation with the agent.
  • Check that the agent's reply no longer includes the <think>...</think> block.

Extra Tips

  • Make sure to test the strip_think_block function with different model outputs to ensure it correctly removes the <think>...</think> block in all cases.
  • Consider adding logging to track any issues with response processing.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - ✅(Solved) Fix Bug: Gemini model responses expose internal <think> blocks to the user [1 pull requests, 4 comments, 5 participants]