Skip to content

fix: guard against non-string content delta and thinking blocks#68855

Closed
tianhaocui wants to merge 3 commits intoopenclaw:mainfrom
tianhaocui:fix-mistral-thinking-object-object
Closed

fix: guard against non-string content delta and thinking blocks#68855
tianhaocui wants to merge 3 commits intoopenclaw:mainfrom
tianhaocui:fix-mistral-thinking-object-object

Conversation

@tianhaocui
Copy link
Copy Markdown
Contributor

Fixes #68309

Summary

Mistral (and potentially other providers) may send thinking content as an object in choice.delta.content rather than a string. Without a type guard, the object is coerced to "[object Object]" when appended to the text buffer via appendTextDelta.

Changes

  1. src/agents/openai-transport-stream.ts:1185 — Added typeof === "string" check on choice.delta.content before appending to the text buffer. Non-string content (objects) is now skipped instead of being coerced.

  2. ui/src/ui/chat/message-extract.ts:51 — Extended extractThinking to handle object-form thinking blocks where the text lives in a .text property (e.g. { type: "thinking", thinking: { text: "..." } }) in addition to plain string form.

Test Plan

  • With Mistral Small thinking enabled, verify thinking content renders as readable text, not [object Object]
  • With Anthropic/OpenAI models, verify no regression in normal text streaming or thinking display

Mistral (and potentially other providers) may send thinking content as
an object in choice.delta.content rather than a string. Without a type
guard, the object is coerced to "[object Object]" when appended to the
text buffer.

Two fixes:
1. openai-transport-stream.ts: add typeof === "string" check on
   choice.delta.content before appending to the text buffer
2. message-extract.ts: handle object-form thinking blocks where the
   text lives in a .text property instead of being a plain string

Fixes openclaw#68309
@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui agents Agent runtime and tooling size: XS labels Apr 19, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 19, 2026

Greptile Summary

This PR adds two defensive guards for non-standard Mistral (and similar provider) response shapes: a typeof === \"string\" check before appending choice.delta.content to the text buffer in the OpenAI transport stream, and object-form thinking-block support (item.thinking.text) in extractThinking. Both fixes are minimal, well-targeted, and preserve existing behavior for Anthropic/OpenAI responses.

Confidence Score: 5/5

Safe to merge — changes are narrow defensive guards with no regression risk on existing providers.

Both changes are simple, targeted type-guards that prevent silent coercion bugs. The only finding is a P2 style nit about if (thinkingValue) vs if (thinkingValue !== null) that has no practical impact since Mistral is unlikely to send an empty thinking text.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/message-extract.ts
Line: 60

Comment:
**Empty string thinking silently dropped**

`if (thinkingValue)` is falsy for `""`, so a thinking block whose `.text` property is an empty string is silently discarded. This is likely fine in practice, but if `thinkingValue !== null` is the intended gate (to distinguish "present but empty" from "absent"), the check should be explicit.

```suggestion
        if (thinkingValue !== null) {
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix: guard against non-string content de..." | Re-trigger Greptile

typeof (item.thinking as Record<string, unknown>).text === "string"
? ((item.thinking as Record<string, unknown>).text as string)
: null;
if (thinkingValue) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Empty string thinking silently dropped

if (thinkingValue) is falsy for "", so a thinking block whose .text property is an empty string is silently discarded. This is likely fine in practice, but if thinkingValue !== null is the intended gate (to distinguish "present but empty" from "absent"), the check should be explicit.

Suggested change
if (thinkingValue) {
if (thinkingValue !== null) {
Prompt To Fix With AI
This is a comment left during a code review.
Path: ui/src/ui/chat/message-extract.ts
Line: 60

Comment:
**Empty string thinking silently dropped**

`if (thinkingValue)` is falsy for `""`, so a thinking block whose `.text` property is an empty string is silently discarded. This is likely fine in practice, but if `thinkingValue !== null` is the intended gate (to distinguish "present but empty" from "absent"), the check should be explicit.

```suggestion
        if (thinkingValue !== null) {
```

How can I resolve this? If you propose a fix, please make it concise.

@prtags
Copy link
Copy Markdown

prtags Bot commented Apr 23, 2026

Related work from PRtags group golden-werewolf-3vn3

Title: Open PR duplicate: [Bug]: A problem with mistral small thinking enabled

Number Title
#68418 fix(agents): unpack typed-block delta.content arrays in openai-completions stream
#68855* fix: guard against non-string content delta and thinking blocks

* This PR

@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented Apr 26, 2026

Closing this as duplicate or superseded after Codex automated review.

PR #68855 should close as superseded by open PR #68418. Current main still lacks the fix, but #68418 is the broader canonical parser-side fix for the same #68309 Mistral Small thinking bug, while #68855 only skips non-string content and adds UI extraction for one object-shaped thinking form.

Best possible solution:

Close #68855 as superseded and focus maintainer review on #68418. The right product/code path is to fix processOpenAICompletionsStream by unpacking array-shaped delta.content into text and thinking deltas, preserving string behavior, and landing regression coverage for the Mistral Small thinking shape.

What I checked:

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Codex Review notes: model gpt-5.5, reasoning high; reviewed against d54d2d6b9b8a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling app: web-ui App: web-ui size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: A problem with mistral small thinking enabled

1 participant