Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

:bug: fix: fix gemini 3 model thinking issue

arvinxx69f4cf3dd95d
diff --git a/packages/model-runtime/src/providers/google/index.test.ts b/packages/model-runtime/src/providers/google/index.test.ts
index 36dcbd43012..4d95c2a30ae 100644
--- a/packages/model-runtime/src/providers/google/index.test.ts
+++ b/packages/model-runtime/src/providers/google/index.test.ts
@@ -6,7 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { LOBE_ERROR_KEY } from '../../core/streams';
import { AgentRuntimeErrorType } from '../../types/error';
import * as debugStreamModule from '../../utils/debugStream';
-import { LobeGoogleAI, resolveModelThinkingBudget } from './index';
+import { LobeGoogleAI } from './index';
const provider = 'google';
const bizErrorType = 'ProviderBizError';
@@ -584,44 +584,6 @@ describe('LobeGoogleAI', () => {
});
});
-describe('resolveModelThinkingBudget', () => {
- it('returns dynamic defaults for 2.5 pro models', () => {
- expect(resolveModelThinkingBudget('gemini-2.5-pro')).toBe(-1);
- });
-
- it('clamps manual budgets for 2.5 pro models', () => {
- expect(resolveModelThinkingBudget('gemini-2.5-pro', 0)).toBe(128);
- expect(resolveModelThinkingBudget('gemini-2.5-pro', 40_000)).toBe(32_768);
- });
-
- it('supports disabling and dynamic thinking for flash models', () => {
- expect(resolveModelThinkingBudget('gemini-2.5-flash')).toBe(-1);
- expect(resolveModelThinkingBudget('gemini-2.5-flash', 0)).toBe(0);
- expect(resolveModelThinkingBudget('gemini-2.5-flash', -1)).toBe(-1);
- expect(resolveModelThinkingBudget('gemini-2.5-flash', -5)).toBe(0);
- expect(resolveModelThinkingBudget('gemini-2.5-flash-preview', 30_000)).toBe(24_576);
- });
-
- it('enforces flash lite family defaults and ranges', () => {
- expect(resolveModelThinkingBudget('gemini-2.5-flash-lite')).toBe(0);
- expect(resolveModelThinkingBudget('gemini-2.5-flash-lite', 400)).toBe(512);
- expect(resolveModelThinkingBudget('gemini-2.5-flash-lite', 600)).toBe(600);
- expect(resolveModelThinkingBudget('gemini-2.5-flash-lite-preview', 25_000)).toBe(24_576);
- });
-
- it('applies robotics preview defaults and overrides', () => {
- expect(resolveModelThinkingBudget('robotics-er-1.5-preview')).toBe(0);
- expect(resolveModelThinkingBudget('robotics-er-1.5-preview', -1)).toBe(-1);
- expect(resolveModelThinkingBudget('robotics-er-1.5-preview', 256)).toBe(512);
- });
-
- it('falls back to generic behaviour for other models', () => {
- expect(resolveModelThinkingBudget('unknown-model')).toBeUndefined();
- expect(resolveModelThinkingBudget('unknown-model', 999)).toBe(999);
- expect(resolveModelThinkingBudget('unknown-model', 99_999)).toBe(24_576);
- });
-});
-
describe('thinkingConfig includeThoughts logic', () => {
it('should enable thinking when thinkingBudget is set', async () => {
const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
diff --git a/packages/model-runtime/src/providers/google/index.ts b/packages/model-runtime/src/providers/google/index.ts
index 78a302b7f36..b7811800bda 100644
--- a/packages/model-runtime/src/providers/google/index.ts
+++ b/packages/model-runtime/src/providers/google/index.ts
@@ -27,6 +27,7 @@ import { parseGoogleErrorMessage } from '../../utils/googleErrorParser';
import { StreamingResponse } from '../../utils/response';
import { createGoogleImage } from './createImage';
import { createGoogleGenerateObject, createGoogleGenerateObjectWithTools } from './generateObject';
+import { resolveGoogleThinkingConfig } from './thinkingResolver';
const log = debug('model-runtime:google');
@@ -60,68 +61,6 @@ const modelsDisableInstuction = new Set([
'google/gemini-3-pro-image-preview',
]);
-const PRO_THINKING_MIN = 128;
-const PRO_THINKING_MAX = 32_768;
-const FLASH_THINKING_MAX = 24_576;
-const FLASH_LITE_THINKING_MIN = 512;
-const FLASH_LITE_THINKING_MAX = 24_576;
-
-const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
-
-type ThinkingModelCategory = 'pro' | 'flash' | 'flashLite' | 'robotics' | 'other';
-
-const getThinkingModelCategory = (model?: string): ThinkingModelCategory => {
- if (!model) return 'other';
-
- const normalized = model.toLowerCase();
-
- if (normalized.includes('robotics-er-1.5-preview')) return 'robotics';
- if (normalized.includes('-2.5-flash-lite') || normalized.includes('flash-lite-latest'))
- return 'flashLite';
- if (normalized.includes('-2.5-flash') || normalized.includes('flash-latest')) return 'flash';
- if (normalized.includes('-2.5-pro') || normalized.includes('pro-latest')) return 'pro';
-
- return 'other';
-};
-
-export const resolveModelThinkingBudget = (
- model: string,
- thinkingBudget?: number | null,
-): number | undefined => {
- const category = getThinkingModelCategory(model);
- const hasBudget = thinkingBudget !== undefined && thinkingBudget !== null;
-
- switch (category) {
- case 'pro': {
- if (!hasBudget) return -1;
- if (thinkingBudget === -1) return -1;
-
- return clamp(thinkingBudget, PRO_THINKING_MIN, PRO_THINKING_MAX);
- }
-
- case 'flash': {
- if (!hasBudget) return -1;
- if (thinkingBudget === -1 || thinkingBudget === 0) return thinkingBudget;
-
- return clamp(thinkingBudget, 0, FLASH_THINKING_MAX);
- }
-
- case 'flashLite':
- case 'robotics': {
- if (!hasBudget) return 0;
- if (thinkingBudget === -1 || thinkingBudget === 0) return thinkingBudget;
-
- return clamp(thinkingBudget, FLASH_LITE_THINKING_MIN, FLASH_LITE_THINKING_MAX);
- }
-
- default: {
- if (!hasBudget) return undefined;
-
- return Math.min(thinkingBudget, FLASH_THINKING_MAX);
- }
- }
-};
-
export interface GoogleModelCard {
displayName: string;
inputTokenLimit: number;
@@ -204,26 +143,10 @@ export class LobeGoogleAI implements LobeRuntimeAI {
const { model, thinkingBudget, thinkingLevel, imageAspectRatio, imageResolution } = payload;
// https://ai.google.dev/gemini-api/docs/thinking#set-budget
- const resolvedThinkingBudget = resolveModelThinkingBudget(model, thinkingBudget);
-
- const thinkingConfig: ThinkingConfig = {
- includeThoughts:
- (!!thinkingBudget ||
- !!thinkingLevel ||
- (model &&
- (model.includes('-3-pro-image') ||
- model.includes('nano-banana-pro') ||
- model.includes('thinking')))) &&
- resolvedThinkingBudget !== 0
- ? true
- : undefined,
- thinkingBudget: resolvedThinkingBudget,
- };
-
- // Add thinkingLevel for 3.0 models
- if (model?.toLowerCase().includes('-3-') && thinkingLevel) {
- (thinkingConfig as any).thinkingLevel = thinkingLevel;
- }
+ const thinkingConfig = resolveGoogleThinkingConfig(model, {
+ thinkingBudget,
+ thinkingLevel,
+ }) as ThinkingConfig;
const contents = await buildGoogleMessages(payload.messages);
diff --git a/packages/model-runtime/src/providers/google/thinkingResolver.test.ts b/packages/model-runtime/src/providers/google/thinkingResolver.test.ts
new file mode 100644
index 00000000000..5a57608edea
--- /dev/null
+++ b/packages/model-runtime/src/providers/google/thinkingResolver.test.ts
@@ -0,0 +1,448 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ getGoogleThinkingModelCategory,
+ isGemini3Model,
+ isThinkingEnabledModel,
+ resolveGoogleThinkingBudget,
+ resolveGoogleThinkingConfig,
+} from './thinkingResolver';
+
+describe('thinkingResolver', () => {
+ describe('getGoogleThinkingModelCategory', () => {
+ it('should return "other" for undefined model', () => {
+ expect(getGoogleThinkingModelCategory(undefined)).toBe('other');
+ });
+
+ it('should return "other" for empty string', () => {
+ expect(getGoogleThinkingModelCategory('')).toBe('other');
+ });
+
+ // Pro models
+ describe('pro category', () => {
+ it.each([
+ 'gemini-2.5-pro',
+ 'gemini-2.5-pro-preview',
+ 'gemini-3-pro',
+ 'gemini-3-pro-preview',
+ 'gemini-3.0-pro',
+ 'pro-latest',
+ ])('should return "pro" for %s', (model) => {
+ expect(getGoogleThinkingModelCategory(model)).toBe('pro');
+ });
+ });
+
+ // Flash models
+ describe('flash category', () => {
+ it.each([
+ 'gemini-2.5-flash',
+ 'gemini-2.5-flash-preview',
+ 'gemini-3-flash',
+ 'gemini-3.0-flash',
+ 'flash-latest',
+ ])('should return "flash" for %s', (model) => {
+ expect(getGoogleThinkingModelCategory(model)).toBe('flash');
+ });
+ });
+
+ // Flash Lite models
+ describe('flashLite category', () => {
+ it.each([
+ 'gemini-2.5-flash-lite',
+ 'gemini-2.5-flash-lite-preview',
+ 'gemini-3-flash-lite',
+ 'flash-lite-latest',
+ ])('should return "flashLite" for %s', (model) => {
+ expect(getGoogleThinkingModelCategory(model)).toBe('flashLite');
+ });
+ });
+
+ // Robotics models
+ describe('robotics category', () => {
+ it('should return "robotics" for robotics-er-1.5-preview', () => {
+ expect(getGoogleThinkingModelCategory('robotics-er-1.5-preview')).toBe('robotics');
+ });
+ });
+
+ // Other models
+ describe('other category', () => {
+ it.each(['gemma-3-1b-it', 'unknown-model', 'custom-model'])(
+ 'should return "other" for %s',
+ (model) => {
+ expect(getGoogleThinkingModelCategory(model)).toBe('other');
+ },
+ );
+ });
+ });
+
+ describe('isGemini3Model', () => {
+ it('should return false for undefined', () => {
+ expect(isGemini3Model(undefined)).toBe(false);
+ });
+
+ it.each([
+ 'gemini-3-pro',
+ 'gemini-3-pro-preview',
+ 'gemini-3-flash',
+ 'gemini-3.0-pro',
+ 'gemini-3.0-flash',
+ 'gemini-3-pro-image-preview',
+ ])('should return true for %s', (model) => {
+ expect(isGemini3Model(model)).toBe(true);
+ });
+
+ it.each(['gemini-2.5-pro', 'gemini-2.5-flash', 'gemma-3-1b-it', 'pro-latest'])(
+ 'should return false for %s',
+ (model) => {
+ expect(isGemini3Model(model)).toBe(false);
+ },
+ );
+ });
+
+ describe('isThinkingEnabledModel', () => {
+ it('should return false for undefined', () => {
+ expect(isThinkingEnabledModel(undefined)).toBe(false);
+ });
+
+ it.each([
+ // Gemini 3 Pro/Flash models
+ 'gemini-3-pro',
+ 'gemini-3-pro-preview',
+ 'gemini-3.0-pro',
+ 'gemini-3-flash',
+ 'gemini-3.0-flash',
+ // Pro image models
+ 'gemini-3-pro-image-preview',
+ 'gemini-3-pro-image',
+ // Other thinking-enabled models
+ 'nano-banana-pro-preview',
+ 'nano-banana-pro',
+ 'gemini-thinking-preview',
+ 'some-thinking-model',
+ ])('should return true for %s', (model) => {
+ expect(isThinkingEnabledModel(model)).toBe(true);
+ });
+
+ it.each([
+ 'gemini-2.5-pro', // 2.5 Pro is also thinking-enabled
+ 'gemini-2.5-flash', // 2.5 Flash is also thinking-enabled
+ ])('should return true for %s', (model) => {
+ expect(isThinkingEnabledModel(model)).toBe(true);
+ });
+
+ it.each([
+ 'gemini-2.5-flash-lite', // flash-lite is NOT auto-enabled
+ 'gemma-3-1b-it',
+ ])('should return false for %s', (model) => {
+ expect(isThinkingEnabledModel(model)).toBe(false);
+ });
+ });
+
+ describe('resolveGoogleThinkingBudget', () => {
+ describe('pro models', () => {
+ const model = 'gemini-3-pro-preview';
+
+ it('should return -1 (dynamic) by default', () => {
+ expect(resolveGoogleThinkingBudget(model, undefined)).toBe(-1);
+ expect(resolveGoogleThinkingBudget(model, null)).toBe(-1);
+ });
+
+ it('should return -1 when explicitly set to -1', () => {
+ expect(resolveGoogleThinkingBudget(model, -1)).toBe(-1);
+ });
+
+ it('should clamp to min (128)', () => {
+ expect(resolveGoogleThinkingBudget(model, 50)).toBe(128);
+ expect(resolveGoogleThinkingBudget(model, 0)).toBe(128);
+ });
+
+ it('should clamp to max (32768)', () => {
+ expect(resolveGoogleThinkingBudget(model, 50000)).toBe(32_768);
+ });
+
+ it('should return value within range', () => {
+ expect(resolveGoogleThinkingBudget(model, 5000)).toBe(5000);
+ });
+ });
+
+ describe('flash models', () => {
+ const model = 'gemini-2.5-flash';
+
+ it('should return -1 (dynamic) by default', () => {
+ expect(resolveGoogleThinkingBudget(model, undefined)).toBe(-1);
+ });
+
+ it('should allow 0 (disabled)', () => {
+ expect(resolveGoogleThinkingBudget(model, 0)).toBe(0);
+ });
+
+ it('should allow -1 (dynamic)', () => {
+ expect(resolveGoogleThinkingBudget(model, -1)).toBe(-1);
+ });
+
+ it('should clamp to max (24576)', () => {
+ expect(resolveGoogleThinkingBudget(model, 30000)).toBe(24_576);
+ });
+ });
+
+ describe('flashLite models', () => {
+ const model = 'gemini-2.5-flash-lite';
+
+ it('should return 0 (disabled) by default', () => {
+ expect(resolveGoogleThinkingBudget(model, undefined)).toBe(0);
+ });
+
+ it('should allow 0 (disabled)', () => {
+ expect(resolveGoogleThinkingBudget(model, 0)).toBe(0);
+ });
+
+ it('should allow -1 (dynamic)', () => {
+ expect(resolveGoogleThinkingBudget(model, -1)).toBe(-1);
+ });
+
+ it('should clamp to min (512)', () => {
+ expect(resolveGoogleThinkingBudget(model, 100)).toBe(512);
+ });
+
+ it('should clamp to max (24576)', () => {
+ expect(resolveGoogleThinkingBudget(model, 30000)).toBe(24_576);
+ });
+ });
+
+ describe('robotics models', () => {
+ const model = 'robotics-er-1.5-preview';
+
+ it('should return 0 (disabled) by default', () => {
+ expect(resolveGoogleThinkingBudget(model, undefined)).toBe(0);
+ });
+
+ it('should clamp to min (512)', () => {
+ expect(resolveGoogleThinkingBudget(model, 100)).toBe(512);
+ });
+ });
+
+ describe('other models', () => {
+ const model = 'unknown-model';
+
+ it('should return undefined by default', () => {
+ expect(resolveGoogleThinkingBudget(model, undefined)).toBeUndefined();
+ });
+
+ it('should clamp to flash max (24576) if provided', () => {
+ expect(resolveGoogleThinkingBudget(model, 30000)).toBe(24_576);
+ });
+ });
+ });
+
+ describe('resolveGoogleThinkingConfig', () => {
+ describe('gemini-3-pro-preview (the original issue model)', () => {
+ const model = 'gemini-3-pro-preview';
+
+ it('should enable includeThoughts by default (Gemini 3 models are thinking-enabled)', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ });
+ });
+
+ it('should enable includeThoughts with thinkingLevel', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingLevel: 'high' });
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ thinkingLevel: 'high',
+ });
+ });
+
+ it('should enable includeThoughts with thinkingBudget', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingBudget: 5000 });
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: 5000,
+ });
+ });
+ });
+
+ describe('gemini-3-pro-image-preview (thinking-enabled model)', () => {
+ const model = 'gemini-3-pro-image-preview';
+
+ it('should enable includeThoughts by default (thinking-enabled model)', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ });
+ });
+ });
+
+ describe('gemini-2.5-pro (also thinking-enabled by default)', () => {
+ const model = 'gemini-2.5-pro';
+
+ it('should enable includeThoughts by default', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ });
+ });
+
+ it('should enable includeThoughts with thinkingBudget', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingBudget: 5000 });
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: 5000,
+ });
+ });
+
+ it('should not include thinkingLevel for 2.5 models (only 3.0+ supports thinkingLevel)', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingLevel: 'high' });
+
+ // thinkingLevel enables includeThoughts, but the level itself is not passed for 2.5
+ expect(result.includeThoughts).toBe(true);
+ expect(result.thinkingLevel).toBeUndefined();
+ });
+ });
+
+ describe('gemini-2.5-flash (also thinking-enabled by default)', () => {
+ const model = 'gemini-2.5-flash';
+
+ it('should enable includeThoughts by default', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ });
+ });
+
+ it('should enable includeThoughts with thinkingBudget', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingBudget: 10000 });
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: 10000,
+ });
+ });
+
+ it('should allow disabling thinking with budget 0', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingBudget: 0 });
+
+ expect(result).toEqual({
+ includeThoughts: undefined,
+ thinkingBudget: 0,
+ });
+ });
+ });
+
+ describe('gemini-3-flash (supports thinking and thinkingLevel)', () => {
+ const model = 'gemini-3-flash';
+
+ it('should enable includeThoughts by default', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ });
+ });
+
+ it('should include thinkingLevel for 3.0 models', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingLevel: 'low' });
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: -1,
+ thinkingLevel: 'low',
+ });
+ });
+
+ it('should support both thinkingBudget and thinkingLevel', () => {
+ const result = resolveGoogleThinkingConfig(model, {
+ thinkingBudget: 8000,
+ thinkingLevel: 'high',
+ });
+
+ expect(result).toEqual({
+ includeThoughts: true,
+ thinkingBudget: 8000,
+ thinkingLevel: 'high',
+ });
+ });
+ });
+
+ describe('gemini-2.5-flash-lite', () => {
+ const model = 'gemini-2.5-flash-lite';
+
+ it('should return disabled by default', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ expect(result).toEqual({
+ includeThoughts: undefined,
+ thinkingBudget: 0,
+ });
+ });
+
+ it('should not enable includeThoughts when budget is 0', () => {
+ const result = resolveGoogleThinkingConfig(model, { thinkingBudget: 0 });
+
+ expect(result.includeThoughts).toBeUndefined();
+ });
+ });
+
+ describe('nano-banana-pro-preview (thinking-enabled model)', () => {
+ const model = 'nano-banana-pro-preview';
+
+ it('should enable includeThoughts by default', () => {
+ const result = resolveGoogleThinkingConfig(model, {});
+
+ // nano-banana-pro is 'other' category, so thinkingBudget is undefined
+ expect(result.includeThoughts).toBe(true);
+ });
+ });
+ });
+});
+
+describe('resolveGoogleThinkingBudget', () => {
+ it('returns dynamic defaults for 2.5 pro models', () => {
+ expect(resolveGoogleThinkingBudget('gemini-2.5-pro')).toBe(-1);
+ });
+
+ it('clamps manual budgets for 2.5 pro models', () => {
+ expect(resolveGoogleThinkingBudget('gemini-2.5-pro', 0)).toBe(128);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-pro', 40_000)).toBe(32_768);
+ });
+
+ it('supports disabling and dynamic thinking for flash models', () => {
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash')).toBe(-1);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash', 0)).toBe(0);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash', -1)).toBe(-1);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash', -5)).toBe(0);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash-preview', 30_000)).toBe(24_576);
+ });
+
+ it('enforces flash lite family defaults and ranges', () => {
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash-lite')).toBe(0);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash-lite', 400)).toBe(512);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash-lite', 600)).toBe(600);
+ expect(resolveGoogleThinkingBudget('gemini-2.5-flash-lite-preview', 25_000)).toBe(24_576);
+ });
+
+ it('applies robotics preview defaults and overrides', () => {
+ expect(resolveGoogleThinkingBudget('robotics-er-1.5-preview')).toBe(0);
+ expect(resolveGoogleThinkingBudget('robotics-er-1.5-preview', -1)).toBe(-1);
+ expect(resolveGoogleThinkingBudget('robotics-er-1.5-preview', 256)).toBe(512);
+ });
+
+ it('falls back to generic behaviour for other models', () => {
+ expect(resolveGoogleThinkingBudget('unknown-model')).toBeUndefined();
+ expect(resolveGoogleThinkingBudget('unknown-model', 999)).toBe(999);
+ expect(resolveGoogleThinkingBudget('unknown-model', 99_999)).toBe(24_576);
+ });
+});
diff --git a/packages/model-runtime/src/providers/google/thinkingResolver.ts b/packages/model-runtime/src/providers/google/thinkingResolver.ts
new file mode 100644
index 00000000000..31c11194796
--- /dev/null
+++ b/packages/model-runtime/src/providers/google/thinkingResolver.ts
@@ -0,0 +1,292 @@
+/* eslint-disable sort-keys-fix/sort-keys-fix*/
+/**
+ * Google Gemini Thinking Resolver
+ *
+ * Resolves thinking configuration for Google Gemini models.
+ * Uses regex patterns for model matching instead of hardcoded strings.
+ */
+
+// ============================================================================
+// Types
+// ============================================================================
+
+/**
+ * Thinking model category for Google Gemini models
+ * Different categories have different thinking budget constraints
+ */
+export type GoogleThinkingModelCategory = 'pro' | 'flash' | 'flashLite' | 'robotics' | 'other';
+
+/**
+ * Thinking level for Gemini 3.0+ models
+ */
+export type GoogleThinkingLevel = 'low' | 'high';
+
+/**
+ * Options for resolving Google thinking configuration
+ */
+export interface GoogleThinkingResolverOptions {
+ /** User-specified thinking budget (tokens) */
+ thinkingBudget?: number | null;
+ /** User-specified thinking level (for 3.0+ models) */
+ thinkingLevel?: GoogleThinkingLevel;
+}
+
+/**
+ * Resolved Google thinking configuration ready for API call
+ */
+export interface ResolvedGoogleThinkingConfig {
+ /** Whether to include thoughts in the response */
+ includeThoughts: boolean | undefined;
+ /** Resolved thinking budget in tokens */
+ thinkingBudget: number | undefined;
+ /** Thinking level for 3.0+ models */
+ thinkingLevel?: GoogleThinkingLevel;
+}
+
+// ============================================================================
+// Constants
+// ============================================================================
+
+/**
+ * Budget constraints for each model category
+ */
+const THINKING_BUDGET_CONSTRAINTS = {
+ PRO_MIN: 128,
+ PRO_MAX: 32_768,
+ FLASH_MAX: 24_576,
+ FLASH_LITE_MIN: 512,
+ FLASH_LITE_MAX: 24_576,
+} as const;
+
+/**
+ * Model category patterns - evaluated in order, first match wins
+ */
+const MODEL_CATEGORY_PATTERNS: Record<Exclude<GoogleThinkingModelCategory, 'other'>, RegExp[]> = {
+ robotics: [/robotics-er-1\.5-preview/i],
+ flashLite: [
+ /gemini-\d+(?:\.\d+)?-flash-lite/i, // gemini-2.5-flash-lite, gemini-3.0-flash-lite
+ /flash-lite-latest/i,
+ ],
+ flash: [
+ /gemini-\d+(?:\.\d+)?-flash(?!-lite)/i, // gemini-2.5-flash, gemini-3.0-flash (but not flash-lite)
+ /flash-latest/i,
+ ],
+ pro: [
+ /gemini-\d+(?:\.\d+)?-pro/i, // gemini-2.5-pro, gemini-3.0-pro, gemini-3-pro
+ /pro-latest/i,
+ ],
+};
+
+/**
+ * Models that inherently support/enable thinking
+ * These models will have includeThoughts=true even without explicit thinkingBudget
+ */
+const THINKING_ENABLED_PATTERNS: RegExp[] = [
+ /gemini-\d+(?:\.\d+)?-pro(?!-image)/i, // gemini-2.5-pro, gemini-3-pro (but not pro-image, handled separately)
+ /gemini-\d+(?:\.\d+)?-flash(?!-lite)/i, // gemini-2.5-flash, gemini-3-flash (but not flash-lite)
+ /gemini-\d+-pro-image/i, // gemini-3-pro-image-preview
+ /nano-banana-pro/i,
+ /thinking/i, // Any model with "thinking" in the name
+];
+
+/**
+ * Patterns to detect Gemini 3.0+ models (which support thinkingLevel)
+ */
+const GEMINI_3_PATTERNS: RegExp[] = [
+ /gemini-3(?:\.\d+)?-/i, // gemini-3-pro, gemini-3.0-flash
+];
+
+// ============================================================================
+// Helper Functions
+// ============================================================================
+
+/**
+ * Clamps a value to a range
+ */
+const clamp = (value: number, min: number, max: number): number =>
+ Math.min(Math.max(value, min), max);
+
+/**
+ * Tests if a model matches any of the given patterns
+ */
+const matchesPatterns = (model: string, patterns: RegExp[]): boolean =>
+ patterns.some((pattern) => pattern.test(model));
+
+// ============================================================================
+// Exported Functions
+// ============================================================================
+
+/**
+ * Determines the thinking model category for a Google Gemini model
+ * @param model - The model identifier
+ * @returns The category of the model
+ */
+export const getGoogleThinkingModelCategory = (model?: string): GoogleThinkingModelCategory => {
+ if (!model) return 'other';
+
+ // Check categories in priority order
+ const categoryOrder: Exclude<GoogleThinkingModelCategory, 'other'>[] = [
+ 'robotics',
+ 'flashLite',
+ 'flash',
+ 'pro',
+ ];
+
+ for (const category of categoryOrder) {
+ if (matchesPatterns(model, MODEL_CATEGORY_PATTERNS[category])) {
+ return category;
+ }
+ }
+
+ return 'other';
+};
+
+/**
+ * Checks if a model is a Gemini 3.0+ model (supports thinkingLevel)
+ * @param model - The model identifier
+ * @returns true if the model is Gemini 3.0+
+ */
+export const isGemini3Model = (model?: string): boolean => {
+ if (!model) return false;
+ return matchesPatterns(model, GEMINI_3_PATTERNS);
+};
+
+/**
+ * Checks if a model inherently supports thinking (includeThoughts)
+ * @param model - The model identifier
+ * @returns true if the model inherently supports thinking
+ */
+export const isThinkingEnabledModel = (model?: string): boolean => {
+ if (!model) return false;
+ return matchesPatterns(model, THINKING_ENABLED_PATTERNS);
+};
+
+/**
+ * Resolves the thinking budget for a Google Gemini model
+ *
+ * @param model - The model identifier
+ * @param thinkingBudget - User-specified thinking budget (tokens)
+ * @returns Resolved thinking budget or undefined
+ *
+ * Special values:
+ * - `-1`: Dynamic/unlimited thinking
+ * - `0`: Thinking disabled
+ */
+export const resolveGoogleThinkingBudget = (
+ model: string,
+ thinkingBudget?: number | null,
+): number | undefined => {
+ const category = getGoogleThinkingModelCategory(model);
+ const hasBudget = thinkingBudget !== undefined && thinkingBudget !== null;
+
+ switch (category) {
+ case 'pro': {
+ // Pro models: 128-32768 tokens, default -1 (dynamic)
+ if (!hasBudget) return -1;
+ if (thinkingBudget === -1) return -1;
+ return clamp(
+ thinkingBudget,
+ THINKING_BUDGET_CONSTRAINTS.PRO_MIN,
+ THINKING_BUDGET_CONSTRAINTS.PRO_MAX,
+ );
+ }
+
+ case 'flash': {
+ // Flash models: 0-24576 tokens, supports 0 (disabled) and -1 (dynamic), default -1
+ if (!hasBudget) return -1;
+ if (thinkingBudget === -1 || thinkingBudget === 0) return thinkingBudget;
+ return clamp(thinkingBudget, 0, THINKING_BUDGET_CONSTRAINTS.FLASH_MAX);
+ }
+
+ case 'flashLite':
+ case 'robotics': {
+ // FlashLite/Robotics: 512-24576 tokens, default 0 (disabled)
+ if (!hasBudget) return 0;
+ if (thinkingBudget === -1 || thinkingBudget === 0) return thinkingBudget;
+ return clamp(
+ thinkingBudget,
+ THINKING_BUDGET_CONSTRAINTS.FLASH_LITE_MIN,
+ THINKING_BUDGET_CONSTRAINTS.FLASH_LITE_MAX,
+ );
+ }
+
+ default: {
+ // Unknown models: no default, clamp to flash max if provided
+ if (!hasBudget) return undefined;
+ return Math.min(thinkingBudget, THINKING_BUDGET_CONSTRAINTS.FLASH_MAX);
+ }
+ }
+};
+
+/**
+ * Determines if includeThoughts should be enabled
+ */
+const shouldIncludeThoughts = (
+ model: string,
+ options: GoogleThinkingResolverOptions,
+ resolvedBudget: number | undefined,
+): boolean | undefined => {
+ const { thinkingBudget, thinkingLevel } = options;
+
+ // Conditions that enable thinking:
+ // 1. thinkingBudget is explicitly set (and not 0)
+ // 2. thinkingLevel is explicitly set
+ // 3. Model is in the thinking-enabled list
+ const hasExplicitThinking = !!thinkingBudget || !!thinkingLevel;
+ const isThinkingModel = isThinkingEnabledModel(model);
+
+ // If thinking is requested AND budget is not 0, enable includeThoughts
+ if ((hasExplicitThinking || isThinkingModel) && resolvedBudget !== 0) {
+ return true;
+ }
+
+ return undefined;
+};
+
+/**
+ * Main resolver function - resolves complete Google thinking configuration
+ *
+ * @param model - The model identifier
+ * @param options - Thinking options from the payload
+ * @returns Resolved thinking configuration
+ *
+ * @example
+ * // Gemini 2.5 Pro with default dynamic thinking
+ * resolveGoogleThinkingConfig('gemini-2.5-pro', {})
+ * // Returns: { includeThoughts: undefined, thinkingBudget: -1 }
+ *
+ * @example
+ * // Gemini 3.0 Pro with explicit thinking level
+ * resolveGoogleThinkingConfig('gemini-3-pro-preview', { thinkingLevel: 'high' })
+ * // Returns: { includeThoughts: true, thinkingBudget: -1, thinkingLevel: 'high' }
+ *
+ * @example
+ * // Gemini 2.5 Flash Lite with thinking disabled
+ * resolveGoogleThinkingConfig('gemini-2.5-flash-lite', { thinkingBudget: 0 })
+ * // Returns: { includeThoughts: undefined, thinkingBudget: 0 }
+ */
+export const resolveGoogleThinkingConfig = (
+ model: string,
+ options: GoogleThinkingResolverOptions = {},
+): ResolvedGoogleThinkingConfig => {
+ const { thinkingBudget, thinkingLevel } = options;
+
+ // Resolve the thinking budget
+ const resolvedBudget = resolveGoogleThinkingBudget(model, thinkingBudget);
+
+ // Determine includeThoughts
+ const includeThoughts = shouldIncludeThoughts(model, options, resolvedBudget);
+
+ // Build result
+ const result: ResolvedGoogleThinkingConfig = {
+ includeThoughts,
+ thinkingBudget: resolvedBudget,
+ };
+
+ // Add thinkingLevel for 3.0+ models
+ if (isGemini3Model(model) && thinkingLevel) {
+ result.thinkingLevel = thinkingLevel;
+ }
+
+ return result;
+};