Skip to content

Update Gemini 3.1 Pro Integration#584

Merged
ngoiyaeric merged 2 commits intomainfrom
update-gemini-3.1-pro-8732759200226483373
May 6, 2026
Merged

Update Gemini 3.1 Pro Integration#584
ngoiyaeric merged 2 commits intomainfrom
update-gemini-3.1-pro-8732759200226483373

Conversation

@ngoiyaeric
Copy link
Copy Markdown
Collaborator

@ngoiyaeric ngoiyaeric commented May 6, 2026

This PR updates the application to support Google's Gemini 3.1 Pro model.

Key changes:

  • Switched the underlying model ID from gemini-3-pro-preview to gemini-3.1-pro-preview in the getModel utility.
  • Updated the Model Selection form in the settings to reflect the 3.1 version.
  • Implemented a fallback in the switch case to ensure users who already had "Gemini 3" selected are automatically upgraded to 3.1 Pro without any manual intervention.
  • Renamed GEMINI_3_PRO_INTEGRATION.md to GEMINI_3.1_PRO_INTEGRATION.md and updated its content.
  • Updated .env.local.example to mention Gemini 3.1 Pro.

Verified with unit tests for the getModel logic.


PR created automatically by Jules for task 8732759200226483373 started by @ngoiyaeric

Summary by CodeRabbit

  • New Features

    • Gemini 3.1 Pro is now available as a selectable AI model option.
  • Documentation

    • Updated integration documentation for Gemini 3.1 Pro support.
    • Updated environment variable configuration examples for the new model.

- Update model identifier to `gemini-3.1-pro-preview` in `lib/utils/index.ts`
- Update model selection UI to display "Gemini 3.1 Pro"
- Add backward compatibility for users with "Gemini 3" selected
- Rename and update documentation to `GEMINI_3.1_PRO_INTEGRATION.md`
- Update `.env.local.example` with 3.1 Pro references

Co-authored-by: ngoiyaeric <115367894+ngoiyaeric@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
qcx Ready Ready Preview, Comment May 6, 2026 10:27am

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@qodo-code-review
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Update Gemini integration to 3.1 Pro with backward compatibility

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Update Gemini model from 3 Pro to 3.1 Pro across codebase
• Add backward compatibility for existing "Gemini 3" model selection
• Update model identifier to gemini-3.1-pro-preview in API calls
• Refresh documentation and environment configuration files
Diagram
flowchart LR
  A["getModel function"] -->|"Update model ID"| B["gemini-3.1-pro-preview"]
  A -->|"Add case for"| C["Gemini 3.1 Pro"]
  C -->|"Backward compat"| D["Gemini 3 case"]
  E["Documentation"] -->|"Rename & update"| F["GEMINI_3.1_PRO_INTEGRATION.md"]
  G[".env.local.example"] -->|"Update references"| H["Gemini 3.1 Pro"]
  I["Model Selection UI"] -->|"Update display"| J["Gemini 3.1 Pro badge"]
Loading

Grey Divider

File Changes

1. lib/utils/index.ts ✨ Enhancement +6/-5

Update Gemini model ID and add backward compatibility

• Updated model identifier from gemini-3-pro-preview to gemini-3.1-pro-preview
• Added 'Gemini 3.1 Pro' case to switch statement for explicit model selection
• Maintained 'Gemini 3' case for backward compatibility with existing user selections
• Updated error and warning messages to reference "Gemini 3.1 Pro"

lib/utils/index.ts


2. .env.local.example 📝 Documentation +1/-1

Update environment variable documentation

• Updated comment from "Gemini 3 Pro" to "Gemini 3.1 Pro"
• Clarified environment variable documentation for the updated model version

.env.local.example


3. GEMINI_3.1_PRO_INTEGRATION.md 📝 Documentation +11/-11

Rename and update Gemini 3.1 Pro integration documentation

• Renamed file from GEMINI_3_PRO_INTEGRATION.md to GEMINI_3.1_PRO_INTEGRATION.md
• Updated all references from "Gemini 3 Pro" to "Gemini 3.1 Pro" throughout document
• Updated model identifier references from gemini-3-pro-preview to gemini-3.1-pro-preview
• Updated documentation links and descriptions to reflect 3.1 Pro capabilities

GEMINI_3.1_PRO_INTEGRATION.md


View more (1)
4. components/settings/components/model-selection-form.tsx ✨ Enhancement +5/-5

Update model selection UI for Gemini 3.1 Pro

• Updated model ID and display name from "Gemini 3" to "Gemini 3.1 Pro"
• Updated model description to emphasize reasoning and agentic task capabilities
• Changed badge from "Multimodal" to "Advanced"
• Updated GPT-5.1 badge from "Advanced" to "Expert" for clarity

components/settings/components/model-selection-form.tsx


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review Bot commented May 6, 2026

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0)

Grey Divider


Action required

1. Legacy model not migrated 🐞 Bug ≡ Correctness
Description
Existing users with a persisted selectedModel value of "Gemini 3" will no longer have a matching
option in the Model Selection UI after the option was renamed to "Gemini 3.1 Pro", so the UI renders
as unselected and the stored value is not upgraded. This contradicts the intended automatic upgrade
behavior and leaves legacy values in the DB indefinitely.
Code

components/settings/components/model-selection-form.tsx[R46-52]

+    id: "Gemini 3.1 Pro",
+    name: "Gemini 3.1 Pro",
+    description: "Google's latest reasoning model, excelling at multimodal understanding and complex agentic tasks.",
    icon: Sparkles,
-    badge: "Multimodal",
+    badge: "Advanced",
    badgeVariant: "outline" as const,
  },
Evidence
The Settings page initializes the form from the DB-selected model without any mapping, but the
ModelSelectionForm only renders options from its local models list. After renaming the Gemini
option to "Gemini 3.1 Pro", the value "Gemini 3" (still possible in the DB) won’t match any
SelectItem/RadioGroupItem value, so nothing will appear selected and the user is not actually
upgraded at the settings/persistence layer.

components/settings/components/model-selection-form.tsx[28-61]
components/settings/components/model-selection-form.tsx[74-107]
components/settings/components/settings.tsx[84-99]
lib/actions/users.ts[146-158]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
Users may still have `users.selectedModel = "Gemini 3"` stored in the database, but the Settings UI no longer offers an option with that value (it now offers only `"Gemini 3.1 Pro"`). This causes the Settings Model Selection UI to render with no option selected, and the persisted value is never upgraded.

### Issue Context
- The ModelSelectionForm’s Select/RadioGroup options are driven by a local `models` array.
- Settings initializes `selectedModel` directly from `getSelectedModel()`.
- `getModel()` has a runtime fallback for legacy "Gemini 3", but this does not migrate the stored value nor fix the Settings UI.

### Fix approach
Implement a one-time migration/mapping from `"Gemini 3"` → `"Gemini 3.1 Pro"` when reading or when initializing the form, and optionally persist the migrated value.

Good options:
1. **Server-side (preferred):** In `getSelectedModel()`, if the DB value is `"Gemini 3"`, update it to `"Gemini 3.1 Pro"` and return the new value.
2. **Client-side:** In Settings `fetchData()`, map `selectedModel === "Gemini 3"` to `"Gemini 3.1 Pro"`, call `form.setValue()` with the mapped value, and optionally call `saveSelectedModel()` to persist.

### Fix Focus Areas
- components/settings/components/settings.tsx[84-99]
- lib/actions/users.ts[146-181]
- components/settings/components/model-selection-form.tsx[28-107]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

Warning

Rate limit exceeded

@ngoiyaeric has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 34 minutes and 8 seconds before requesting another review.

To continue reviewing without waiting, purchase usage credits in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f01df125-0d00-4a52-b067-dcf9094477b2

📥 Commits

Reviewing files that changed from the base of the PR and between 0a58d1c and c392619.

📒 Files selected for processing (4)
  • GEMINI_3.1_PRO_INTEGRATION.md
  • components/settings/components/settings.tsx
  • lib/agents/tools/geospatial.tsx
  • lib/utils/index.ts

Walkthrough

The PR adds support for Gemini 3.1 Pro as an AI provider option. It updates environment variable documentation, implements model selection logic with API key configuration, adds the new model to the UI model picker, and provides integration documentation with technical details and priority ordering.

Changes

Gemini 3.1 Pro Provider Integration

Layer / File(s) Summary
Configuration
.env.local.example
Environment variable section header updated from "Gemini 3 Pro" to "Gemini 3.1 Pro".
Model Selection Logic
lib/utils/index.ts
Added Gemini 3.1 Pro case with GEMINI_3_PRO_API_KEY check, Google Generative AI client initialization, and gemini-3.1-pro-preview endpoint routing; fallback default prefers Gemini 3.1 Pro when unavailable.
UI Model Selection
components/settings/components/model-selection-form.tsx
Gemini 3.1 Pro model entry added to the model options array with Sparkles icon and Advanced badge; GPT-5.1 badge updated to Expert.
Documentation
GEMINI_3.1_PRO_INTEGRATION.md
Integration guide documents Gemini 3.1 Pro provider, environment variable setup (GEMINI_3_PRO_API_KEY), provider priority logic, technical details including model ID (gemini-3.1-pro-preview), and API endpoint reference.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • Update Gemini 3.1 Pro #583: Both changes add and wire up the Gemini 3.1 Pro model across environment configuration, documentation, UI options, and utility logic.

Possibly related PRs

  • QueueLab/QCX#389: Both PRs modify the same provider integration points (env var, lib/utils getModel logic, docs, and model-selection UI) to add support for a Gemini 3 family provider variant.

Suggested labels

Review effort 2/5

Poem

🐰 A gemini hops with sparkles bright,
Version 3.1 shines with new delight,
Through env and UI, the wiring's done,
Another model choice, the garden's won!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: updating the application to support Gemini 3.1 Pro across multiple components including model utilities, UI forms, and documentation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch update-gemini-3.1-pro-8732759200226483373

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment on lines +46 to 52
id: "Gemini 3.1 Pro",
name: "Gemini 3.1 Pro",
description: "Google's latest reasoning model, excelling at multimodal understanding and complex agentic tasks.",
icon: Sparkles,
badge: "Multimodal",
badge: "Advanced",
badgeVariant: "outline" as const,
},
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.

Action required

1. Legacy model not migrated 🐞 Bug ≡ Correctness

Existing users with a persisted selectedModel value of "Gemini 3" will no longer have a matching
option in the Model Selection UI after the option was renamed to "Gemini 3.1 Pro", so the UI renders
as unselected and the stored value is not upgraded. This contradicts the intended automatic upgrade
behavior and leaves legacy values in the DB indefinitely.
Agent Prompt
### Issue description
Users may still have `users.selectedModel = "Gemini 3"` stored in the database, but the Settings UI no longer offers an option with that value (it now offers only `"Gemini 3.1 Pro"`). This causes the Settings Model Selection UI to render with no option selected, and the persisted value is never upgraded.

### Issue Context
- The ModelSelectionForm’s Select/RadioGroup options are driven by a local `models` array.
- Settings initializes `selectedModel` directly from `getSelectedModel()`.
- `getModel()` has a runtime fallback for legacy "Gemini 3", but this does not migrate the stored value nor fix the Settings UI.

### Fix approach
Implement a one-time migration/mapping from `"Gemini 3"` → `"Gemini 3.1 Pro"` when reading or when initializing the form, and optionally persist the migrated value.

Good options:
1. **Server-side (preferred):** In `getSelectedModel()`, if the DB value is `"Gemini 3"`, update it to `"Gemini 3.1 Pro"` and return the new value.
2. **Client-side:** In Settings `fetchData()`, map `selectedModel === "Gemini 3"` to `"Gemini 3.1 Pro"`, call `form.setValue()` with the mapped value, and optionally call `saveSelectedModel()` to persist.

### Fix Focus Areas
- components/settings/components/settings.tsx[84-99]
- lib/actions/users.ts[146-181]
- components/settings/components/model-selection-form.tsx[28-107]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@components/settings/components/model-selection-form.tsx`:
- Around line 46-58: Normalize any legacy saved model id "Gemini 3" to the new
id "Gemini 3.1 Pro" before comparing against SelectItem/RadioGroupItem options
in the ModelSelectionForm component; update the value used for rendering and
selection checks (the prop/state that feeds SelectItem/RadioGroupItem, e.g.,
selectedModel, value, or defaultValue) to map "Gemini 3" -> "Gemini 3.1 Pro" so
existing saved configs appear selected, and ensure the same normalization is
applied when initializing form state and when persisting changes.

In `@lib/utils/index.ts`:
- Around line 53-67: The console.error calls inside the Gemini branch currently
hard-code "Gemini 3.1 Pro"; update those error messages to use the actual
selected model label (the variable used in the switch, e.g., selectedModel) and
optionally include the resolved target ("gemini-3.1-pro") so telemetry reflects
whether "Gemini 3" or "Gemini 3.1 Pro" was chosen; modify the two console.error
invocations in the case handling that uses gemini3ProApiKey /
createGoogleGenerativeAI(...) to reference selectedModel (and the resolved
target string) instead of the fixed literal.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: da1f5c34-fdb7-41ba-9d28-2e0a5a6bae91

📥 Commits

Reviewing files that changed from the base of the PR and between 99505ba and 0a58d1c.

📒 Files selected for processing (4)
  • .env.local.example
  • GEMINI_3.1_PRO_INTEGRATION.md
  • components/settings/components/model-selection-form.tsx
  • lib/utils/index.ts
📜 Review details
🔇 Additional comments (3)
.env.local.example (1)

20-21: Header update is consistent with runtime configuration.

The rename to Gemini 3.1 Pro aligns with the integration, and keeping GEMINI_3_PRO_API_KEY preserves compatibility with current code paths.

lib/utils/index.ts (1)

95-103: Gemini fallback now correctly targets 3.1 Pro.

The default provider chain update to gemini-3.1-pro-preview is consistent with the migration objective.

GEMINI_3.1_PRO_INTEGRATION.md (1)

1-79: Documentation update is coherent with the code migration.

The model name, provider priority, env var guidance, and model identifier are consistently updated to Gemini 3.1 Pro.

Comment on lines +46 to +58
id: "Gemini 3.1 Pro",
name: "Gemini 3.1 Pro",
description: "Google's latest reasoning model, excelling at multimodal understanding and complex agentic tasks.",
icon: Sparkles,
badge: "Multimodal",
badge: "Advanced",
badgeVariant: "outline" as const,
},
{
id: "GPT-5.1",
name: "GPT-5.1",
description: "The cutting-edge of language models, offering unparalleled performance in creative and analytical tasks.",
icon: Zap,
badge: "Advanced",
badge: "Expert",
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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Normalize legacy "Gemini 3" form values before rendering options.

Line 46 removes the old option id, so existing saved values of "Gemini 3" won’t match any SelectItem/RadioGroupItem. Users can see an apparently unselected model despite having one configured.

Suggested patch
+const normalizeModelValue = (value?: string) =>
+  value === "Gemini 3" ? "Gemini 3.1 Pro" : value;
+
 export function ModelSelectionForm({ form }: ModelSelectionFormProps) {
   return (
@@
-              <Select onValueChange={field.onChange} value={field.value}>
+              <Select onValueChange={field.onChange} value={normalizeModelValue(field.value)}>
@@
             <RadioGroup
               onValueChange={field.onChange}
-              value={field.value}
+              value={normalizeModelValue(field.value)}
               className="space-y-3"
             >
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@components/settings/components/model-selection-form.tsx` around lines 46 -
58, Normalize any legacy saved model id "Gemini 3" to the new id "Gemini 3.1
Pro" before comparing against SelectItem/RadioGroupItem options in the
ModelSelectionForm component; update the value used for rendering and selection
checks (the prop/state that feeds SelectItem/RadioGroupItem, e.g.,
selectedModel, value, or defaultValue) to map "Gemini 3" -> "Gemini 3.1 Pro" so
existing saved configs appear selected, and ensure the same normalization is
applied when initializing form state and when persisting changes.

Comment thread lib/utils/index.ts
Comment on lines 53 to 67
case 'Gemini 3':
case 'Gemini 3.1 Pro':
if (gemini3ProApiKey) {
const google = createGoogleGenerativeAI({
apiKey: gemini3ProApiKey,
});
try {
return google('gemini-3-pro-preview');
return google('gemini-3.1-pro-preview');
} catch (error) {
console.error('Selected model "Gemini 3" is configured but failed to initialize.', error);
console.error('Selected model "Gemini 3.1 Pro" is configured but failed to initialize.', error);
throw new Error('Failed to initialize selected model.');
}
} else {
console.error('User selected "Gemini 3" but GEMINI_3_PRO_API_KEY is not set.');
console.error('User selected "Gemini 3.1 Pro" but GEMINI_3_PRO_API_KEY is not set.');
throw new Error('Selected model is not configured.');
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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use the requested model label in Gemini error logs.

Line 62 and Line 66 always log "Gemini 3.1 Pro". If Line 53 ("Gemini 3") hits this branch, telemetry is misleading during migration/debugging. Log selectedModel (optionally with resolved target) instead.

Suggested patch
-            console.error('Selected model "Gemini 3.1 Pro" is configured but failed to initialize.', error);
+            console.error(
+              `Selected model "${selectedModel}" (resolved to "Gemini 3.1 Pro") is configured but failed to initialize.`,
+              error
+            );
             throw new Error('Failed to initialize selected model.');
           }
         } else {
-            console.error('User selected "Gemini 3.1 Pro" but GEMINI_3_PRO_API_KEY is not set.');
+            console.error(
+              `User selected "${selectedModel}" (resolved to "Gemini 3.1 Pro") but GEMINI_3_PRO_API_KEY is not set.`
+            );
             throw new Error('Selected model is not configured.');
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/utils/index.ts` around lines 53 - 67, The console.error calls inside the
Gemini branch currently hard-code "Gemini 3.1 Pro"; update those error messages
to use the actual selected model label (the variable used in the switch, e.g.,
selectedModel) and optionally include the resolved target ("gemini-3.1-pro") so
telemetry reflects whether "Gemini 3" or "Gemini 3.1 Pro" was chosen; modify the
two console.error invocations in the case handling that uses gemini3ProApiKey /
createGoogleGenerativeAI(...) to reference selectedModel (and the resolved
target string) instead of the fixed literal.

@ngoiyaeric
Copy link
Copy Markdown
Collaborator Author

@coderabbitai Gemini 3.1 pro is not connected, grok is still inferencing. Debug the code for the right configuration.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

Let me inspect the relevant code files to identify the root cause.


🏁 Script executed:

#!/bin/bash
echo "=== lib/utils/index.ts ==="
cat lib/utils/index.ts 2>/dev/null || fd -i 'index.ts' lib/utils --exec cat {}

echo ""
echo "=== model-selection-form.tsx ==="
fd -i 'model-selection-form.tsx' --exec cat {}

echo ""
echo "=== any AI provider/routing files ==="
fd -i 'route.ts' app/api --exec echo {} 
fd -i 'route.ts' app/api -x cat {}

echo ""
echo "=== .env.local.example ==="
cat .env.local.example 2>/dev/null || fd -i '.env.local.example' --exec cat {}

echo ""
echo "=== Search for getModel usages ==="
rg -n 'getModel\|Gemini\|grok\|GEMINI' --type ts -C2

echo ""
echo "=== Search for model switch/case logic ==="
rg -n '"Gemini\|gemini\|Grok\|grok' --type ts -C3

Length of output: 26162


@ngoiyaeric I've analyzed the code and found three likely root causes:


🔍 Debug Findings

Root Cause 1 — Default Fallback Priority Prefers Grok (Most Likely)

In lib/utils/index.ts, the fallback chain when no model is explicitly selected is:

Grok → Gemini 3.1 Pro → Bedrock → OpenAI

If XAI_API_KEY is set in your .env.local (which it clearly is, since Grok is inferencing), Grok always wins in the default path. This happens if getSelectedModel() returns null or undefined — e.g., the user hasn't selected a model yet, or the persisted selection isn't being retrieved.

Fix: Either remove XAI_API_KEY from .env.local if you don't want Grok, or reorder the fallback to put Gemini first:

// Default behavior: Gemini -> Grok -> Bedrock -> OpenAI
+ if (gemini3ProApiKey) {
+   const google = createGoogleGenerativeAI({ apiKey: gemini3ProApiKey });
+   try {
+     return google('gemini-3.1-pro-preview');
+   } catch (error) {
+     console.warn('Gemini 3.1 Pro API unavailable, falling back:', error);
+   }
+ }

  if (xaiApiKey) {
    const xai = createXai({ apiKey: xaiApiKey, baseURL: 'https://api.x.ai/v1' });
    try {
      return xai('grok-4-fast-non-reasoning');
    } catch (error) {
      console.warn('xAI API unavailable, falling back:', error);
    }
  }

- if (gemini3ProApiKey) { ... }

Root Cause 2 — GEMINI_3_PRO_API_KEY May Not Be Set

The .env.local.example defines GEMINI_3_PRO_API_KEY, but if your actual .env.local doesn't have it, the switch case for 'Gemini 3.1 Pro' will throw:

User selected "Gemini 3.1 Pro" but GEMINI_3_PRO_API_KEY is not set.

Fix: Make sure your .env.local contains:

GEMINI_3_PRO_API_KEY=your_actual_google_api_key

Root Cause 3 — app/api/chat/route.ts Does Not Call getModel()

The chat API route file does not invoke getModel() at all — it only saves chat messages to the database. This means the actual AI streaming/inference route may be hardcoded to use Grok directly, bypassing model selection entirely. You need to verify that whichever route handles streaming calls getModel().

Let me check the getSelectedModel implementation and the actual inference route:


🏁 Script executed:

#!/bin/bash
echo "=== getSelectedModel in lib/actions/users ==="
fd -i 'users.ts' lib/actions --exec cat {}
fd -i 'users.ts' lib --exec cat {}

echo ""
echo "=== Search for any file that calls xai or grok directly ==="
rg -n "grok|createXai|xai(" --type ts -C3

echo ""
echo "=== Search for streaming/inference route (streamText, generateText, getModel) ==="
rg -n "streamText|generateText|getModel" --type ts -C4

echo ""
echo "=== Look for any route that handles AI chat streaming ==="
fd -i '*.ts' app -x grep -l "streamText\|generateText\|getModel" {}

Length of output: 25957


@ngoiyaeric I now have a complete picture of the problem. Here is the full root cause analysis and fixes:


🐛 Root Cause Analysis

All agents (researcher.tsx, writer.tsx, task-manager.tsx, etc.) correctly call getModel(), so the routing path is fine. The problem is in what getModel() returns when getSelectedModel() is null.

The Bug: Default Fallback Priority

In lib/utils/index.ts, the entire selection logic collapses to the fallback chain whenever getSelectedModel() returns null:

// Default behavior: Grok -> Gemini -> Bedrock -> OpenAI
if (xaiApiKey) → returns Grok  ← 🔴 Wins immediately if XAI_API_KEY is set
if (gemini3ProApiKey) → returns Gemini 3.1 Pro
...

Since XAI_API_KEY is clearly set in your environment (Grok is running), Grok always wins the fallback.

Why getSelectedModel() Returns null

getSelectedModel() in lib/actions/users.ts reads from users.selectedModel in the database. It returns null in any of these three scenarios:

Scenario Cause
① User not authenticated getCurrentUserIdOnServer() returns null
② DB column missing selectedModel column doesn't exist — Drizzle migration not run → catch block returns null
③ Preference not saved Settings form doesn't call saveSelectedModel() on submit

In all three cases, getSelectedModel() silently returns null, and Grok takes over.


✅ Fixes

Fix 1 — Reorder the Default Fallback (Critical)

In lib/utils/index.ts, swap Grok and Gemini in the fallback so Gemini 3.1 Pro is tried first:

- // Default behavior: Grok -> Gemini -> Bedrock -> OpenAI
+ // Default behavior: Gemini 3.1 Pro -> Grok -> Bedrock -> OpenAI
+ if (gemini3ProApiKey) {
+   const google = createGoogleGenerativeAI({ apiKey: gemini3ProApiKey });
+   try {
+     return google('gemini-3.1-pro-preview');
+   } catch (error) {
+     console.warn('Gemini 3.1 Pro API unavailable, falling back to next provider:', error);
+   }
+ }

  if (xaiApiKey) {
    const xai = createXai({ apiKey: xaiApiKey, baseURL: 'https://api.x.ai/v1' });
    try {
      return xai('grok-4-fast-non-reasoning');
    } catch (error) {
      console.warn('xAI API unavailable, falling back to next provider:');
    }
  }

- if (gemini3ProApiKey) {
-   const google = createGoogleGenerativeAI({ apiKey: gemini3ProApiKey });
-   try {
-     return google('gemini-3.1-pro-preview');
-   } catch (error) {
-     console.warn('Gemini 3.1 Pro API unavailable, falling back to next provider:', error);
-   }
- }

Fix 2 — Check Your .env.local

Make sure this is present and set to a valid key:

GEMINI_3_PRO_API_KEY=your_actual_google_ai_api_key_here

Without this, the Gemini branch is skipped entirely even after reordering.

Fix 3 — Verify the DB Migration Was Run

getSelectedModel() reads from users.selectedModel via Drizzle ORM. If the column doesn't exist in your database (migration not applied), the query throws and the catch block returns null silently. Check your schema and run migrations:

npx drizzle-kit push  # or `migrate` depending on your setup

Then verify the column exists:

SELECT column_name FROM information_schema.columns 
WHERE table_name = 'users' AND column_name = 'selectedModel';

Fix 4 — Verify the Settings Form Saves the Selection

Check that your settings page calls saveSelectedModel('Gemini 3.1 Pro') on form submission. You can add a quick log to confirm:

// In your settings form submit handler
const result = await saveSelectedModel(selectedModel);
console.log('[Settings] saveSelectedModel result:', result);

Summary Checklist

  • Set GEMINI_3_PRO_API_KEY in .env.local
  • Reorder default fallback in lib/utils/index.ts (Gemini first, Grok second)
  • Run Drizzle migration to ensure selectedModel column exists in users table
  • Confirm settings form calls saveSelectedModel() successfully
  • Restart the dev server after .env.local changes (CTRL+C then npm run dev)

@google-labs-jules
Copy link
Copy Markdown
Contributor

@coderabbitai Gemini 3.1 pro is not connected, grok is still inferencing. Debug the code for the right configuration.

@ngoiyaeric Thank you for the feedback. I've addressed the issue by:

  1. Swapping the model priority in lib/utils/index.ts to favor Gemini 3.1 Pro over Grok when both API keys are present.
  2. Fixing a case-sensitivity issue in lib/agents/tools/geospatial.tsx that was preventing Gemini detection during tool execution.
  3. Updating the default model selection in the settings to Gemini 3.1 Pro for new users.
  4. Updating the grounding model ID in the geospatial tool to gemini-3.1-pro-preview.

Gemini 3.1 Pro should now be correctly prioritized and connected.

- Swap model priority to favor Gemini 3.1 Pro over Grok in `getModel`
- Fix case-insensitive detection of Gemini models in `geospatial` tool
- Update grounding model ID to `gemini-3.1-pro-preview` in `geospatial` tool
- Set Gemini 3.1 Pro as the default selected model in settings
- Update integration documentation to reflect the new priority order

Co-authored-by: ngoiyaeric <115367894+ngoiyaeric@users.noreply.github.com>
@ngoiyaeric ngoiyaeric merged commit 57617bc into main May 6, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants