Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions extensions/google/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,22 @@ describe("google provider plugin hooks", () => {
allowSyntheticToolResults: true,
});

// Gemini 3.x emits native `thought` parts; asking it for <think> tags on
// top made it narrate the tag instruction instead of following it.
expect(
provider.resolveReasoningOutputMode?.({
provider: "google",
modelApi: "google-generative-ai",
modelId: "gemini-3.1-pro-preview",
} as never),
).toBe("native");

expect(
provider.resolveReasoningOutputMode?.({
provider: "google",
modelApi: "google-generative-ai",
modelId: "gemini-2.5-flash",
} as never),
).toBe("tagged");

const sanitized = await Promise.resolve(
Expand Down
16 changes: 16 additions & 0 deletions src/plugin-sdk/provider-model-shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ describe("buildProviderReplayFamilyHooks", () => {
},
{
family: "google-gemini" as const,
// Gemini 3.x emits native `thought` parts — tagging on top made it
// narrate the tag instruction instead of following it.
ctx: {
provider: "google",
modelApi: "google-generative-ai",
Expand All @@ -95,6 +97,20 @@ describe("buildProviderReplayFamilyHooks", () => {
allowSyntheticToolResults: true,
},
hasSanitizeReplayHistory: true,
reasoningMode: "native",
},
{
family: "google-gemini" as const,
ctx: {
provider: "google",
modelApi: "google-generative-ai",
modelId: "gemini-2.5-flash",
},
match: {
validateGeminiTurns: true,
allowSyntheticToolResults: true,
},
hasSanitizeReplayHistory: true,
reasoningMode: "tagged",
},
{
Expand Down
8 changes: 6 additions & 2 deletions src/plugin-sdk/provider-model-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
buildOpenAICompatibleReplayPolicy,
buildPassthroughGeminiSanitizingReplayPolicy,
buildStrictAnthropicReplayPolicy,
hasNativeGeminiThoughtParts,
resolveGoogleGeminiReasoningOutputMode,
resolveTaggedReasoningOutputMode,
sanitizeGoogleGeminiReplayHistory,
} from "../plugins/provider-replay-helpers.js";
Expand Down Expand Up @@ -80,6 +82,8 @@ export {
buildNativeAnthropicReplayPolicyForModel,
buildOpenAICompatibleReplayPolicy,
buildPassthroughGeminiSanitizingReplayPolicy,
hasNativeGeminiThoughtParts,
resolveGoogleGeminiReasoningOutputMode,
resolveTaggedReasoningOutputMode,
sanitizeGoogleGeminiReplayHistory,
buildStrictAnthropicReplayPolicy,
Expand Down Expand Up @@ -219,8 +223,8 @@ export function buildProviderReplayFamilyHooks(
buildReplayPolicy: () => buildGoogleGeminiReplayPolicy(),
sanitizeReplayHistory: (ctx: ProviderSanitizeReplayHistoryContext) =>
sanitizeGoogleGeminiReplayHistory(ctx),
resolveReasoningOutputMode: (_ctx: ProviderReasoningOutputModeContext) =>
resolveTaggedReasoningOutputMode(),
resolveReasoningOutputMode: (ctx: ProviderReasoningOutputModeContext) =>
resolveGoogleGeminiReasoningOutputMode(ctx),
};
case "passthrough-gemini":
return {
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/provider-replay-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,19 @@ export function sanitizeGoogleGeminiReplayHistory(
export function resolveTaggedReasoningOutputMode(): ProviderReasoningOutputMode {
return "tagged";
}

/**
* Gemini 3.x streams reasoning as native `thought` parts, which the Google
* transport already routes into thinking blocks. Asking it for `<think>` tags
* on top of that makes it narrate the tag instruction itself instead of
* following it, and the chain-of-thought lands in the text stream verbatim.
*/
export function hasNativeGeminiThoughtParts(modelId: string | undefined | null): boolean {
return /gemini-3(?:\.\d+)?-(?:pro|flash)/.test(normalizeLowercaseStringOrEmpty(modelId));
}

export function resolveGoogleGeminiReasoningOutputMode(
ctx: ProviderReplayPolicyContext,
): ProviderReasoningOutputMode {
return hasNativeGeminiThoughtParts(ctx.modelId) ? "native" : resolveTaggedReasoningOutputMode();
}
25 changes: 25 additions & 0 deletions src/utils/provider-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ describe("resolveReasoningOutputMode", () => {
},
);

// Gemini 3.x streams native `thought` parts. Asking it for <think> tags on top
// made it narrate the tag instruction as prose, leaking the chain-of-thought
// (and internal tool names) into the answer.
it.each([
["gemini-3.6-flash", "native"],
["gemini-3-flash", "native"],
["gemini-3.1-pro-preview", "native"],
["gemini-2.5-flash", "tagged"],
["gemini-2.5-flash-lite", "tagged"],
] as const)("built-in map resolves %s to %s", (modelId, expected) => {
expect(
resolveReasoningOutputMode({
provider: "google-generative-ai",
modelId,
workspaceDir: process.cwd(),
}),
).toBe(expected);
});

it("keeps the built-in tagged mode when no model is known", () => {
expect(
resolveReasoningOutputMode({ provider: "google-generative-ai", workspaceDir: process.cwd() }),
).toBe("tagged");
});

it.each([
["google", "tagged"],
["Google", "tagged"],
Expand Down
3 changes: 2 additions & 1 deletion src/utils/provider-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { hasNativeGeminiThoughtParts } from "../plugins/provider-replay-helpers.js";
import type { ProviderRuntimeModel } from "../plugins/provider-runtime-model.types.js";
import { resolveProviderReasoningOutputModeWithPlugin } from "../plugins/provider-runtime.js";
import {
Expand Down Expand Up @@ -51,7 +52,7 @@ export function resolveReasoningOutputMode(params: {
const builtInMode =
BUILTIN_REASONING_OUTPUT_MODES[normalized as keyof typeof BUILTIN_REASONING_OUTPUT_MODES];
if (builtInMode) {
return builtInMode;
return hasNativeGeminiThoughtParts(params.modelId) ? "native" : builtInMode;
}

// Keep a tiny built-in fallback for non-plugin Google surfaces.
Expand Down
Loading