Skip to content

fix: correct authType for Gemini CLI API key authentication#926

Closed
tommy-ca wants to merge 2 commits intoeyaltoledano:nextfrom
tommy-ca:feature/gemini-cli-provider-fix-v2
Closed

fix: correct authType for Gemini CLI API key authentication#926
tommy-ca wants to merge 2 commits intoeyaltoledano:nextfrom
tommy-ca:feature/gemini-cli-provider-fix-v2

Conversation

@tommy-ca
Copy link
Contributor

@tommy-ca tommy-ca commented Jul 6, 2025

Summary

  • Fixes the "Failed to initialize Gemini model: Error: Error creating contentGenerator: Unsupported authType: undefined" error
  • Changes authType from 'api-key' to 'gemini-api-key' for API key authentication

Root Cause

The ai-sdk-provider-gemini-cli package's initializeGeminiClient function only recognizes these authType values:

  • 'gemini-api-key' → maps to AuthType.USE_GEMINI
  • 'oauth-personal' → maps to AuthType.LOGIN_WITH_GOOGLE_PERSONAL
  • 'vertex-ai' → maps to AuthType.USE_VERTEX_AI

Our implementation was using 'api-key' which didn't match any condition, causing authType to become undefined and triggering the error.

Test plan

  • All 46 Gemini CLI provider tests pass
  • All 64 AI provider tests pass
  • Both OAuth and API key authentication work correctly
  • No breaking changes or regressions

🤖 Generated with Claude Code

Resolves the "Failed to initialize Gemini model: Error: Error creating contentGenerator: Unsupported authType: undefined" error.

## Root Cause
The ai-sdk-provider-gemini-cli package's initializeGeminiClient function only recognizes these authType values:
- 'gemini-api-key' → maps to AuthType.USE_GEMINI
- 'oauth-personal' → maps to AuthType.LOGIN_WITH_GOOGLE_PERSONAL
- 'vertex-ai' → maps to AuthType.USE_VERTEX_AI

Our implementation was using 'api-key' which didn't match any condition, causing authType to become undefined and triggering the error.

## Solution
Changed authType from 'api-key' to 'gemini-api-key' for API key authentication to match the SDK's expected values.

## Files Changed
- src/ai-providers/gemini-cli.js: Updated authType value
- tests/unit/ai-providers/gemini-cli.test.js: Updated test expectations

## Testing
All 46 Gemini CLI provider tests pass, confirming the fix resolves the authentication error while maintaining full functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 6, 2025 01:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Addresses an authentication mismatch for the Gemini CLI provider by aligning the authType string with the underlying SDK’s expectations.

  • Changes authType from 'api-key' to 'gemini-api-key' in provider code and all related tests.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/ai-providers/gemini-cli.js Updated the authType literal to gemini-api-key
tests/unit/ai-providers/gemini-cli.test.js Updated expected authType values and test names

// API key provided - use it for compatibility
authOptions = {
authType: 'api-key',
authType: 'gemini-api-key',
Copy link

Copilot AI Jul 6, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the 'gemini-api-key' string into a shared constant (e.g. GEMINI_AUTH_TYPE_API_KEY) to reduce duplication and prevent typos in both code and tests.

Copilot uses AI. Check for mistakes.
Addresses PR review feedback by replacing magic strings with named constants for better maintainability.

## Changes
- Added GEMINI_AUTH_TYPES constant with API_KEY and OAUTH_PERSONAL values
- Updated implementation to use constants instead of hardcoded strings
- Updated all tests to import and use the constants
- Exported constants for reuse in tests and other modules

## Benefits
- Reduces risk of typos when referencing auth types
- Makes code more maintainable when auth type values need to change
- Improves readability and self-documentation
- Follows DRY principle by centralizing auth type definitions

## Testing
All 46 Gemini CLI provider tests pass, confirming the refactoring maintains full functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Jul 6, 2025

⚠️ No Changeset found

Latest commit: 8d12e07

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@jaruesink
Copy link

@tommy-ca

When trying to authenticate with Google CLI provider with oauth-personal I had to change this.
image

Have you tried this or run into this?

@ben-vargas
Copy link
Contributor

Good catch, I honestly didn’t have/test a Google API Key - fix seems reasonable.

I’m curious, what is the use case for using Gemini CLI with API Key through task master, instead of just using the Google provider directly? It would seem to me that if paying for use with API Key, Google APIs direct would be preferred to Gemini CLI?

Doesn’t mean it shouldn’t work, that’s why I included support for it… but just curious why one would use API Key through Gemini CLI in Task Master instead of direct to Google APIs. Maybe I’m missing an interesting use case?

@ben-vargas
Copy link
Contributor

@jaruesink nice catch - It looks like Google merged a PR after this PR had been written/tested/merged into the ‘next’ branch. They introduced a breaking change in google-cli with this commit:

● Bash(git show 3587054d32372874a4e067ae050f861ad1cec2b4 --stat)
⎿  commit 3587054d32372874a4e067ae050f861ad1cec2b4
Author: Tommaso Sciortino sciortino@gmail.com
Date: Mon Jun 30 17:11:54 2025 -0700
… +17 lines (ctrl+r to expand)

● Yes! The AuthType enum did change recently. Here's what happened:

On June 30, 2025 (commit 3587054d), there was a breaking change that renamed:

  • LOGIN_WITH_GOOGLE_PERSONAL → LOGIN_WITH_GOOGLE

The commit message was: "Rename AuthType LOGIN_WITH_GOOGLE_PERSONAL -> LOGIN_WITH_GOOGLE (#2769)"

This change affected 13 files across the codebase. The enum value itself ('oauth-personal') remained the same, but the enum key changed from LOGIN_WITH_GOOGLE_PERSONAL to LOGIN_WITH_GOOGLE.

This explains why your library is using LOGIN_WITH_GOOGLE_PERSONAL - it was the correct enum value until just a fewdays ago.

The change appears to be part of a broader effort to simplify the authentication options, as there was also an
earlier commit (June 25) that merged "Login with Google Workspace" into the general "Login with Google" option.

@ben-vargas
Copy link
Contributor

@tommy-ca and @jaruesink - I've made updates to ai-sdk-provider-gemini-cli to support both the ai-sdk standard api-key as well as retained support for gemini-api-key AuthTypes used by gemini-cli. You might be interested in reviewing the changes in this PR in relation to need for this PR... ben-vargas/ai-sdk-provider-gemini-cli#1 - Any testing/validation you could do to confirm API Key is working would be great.

@Crunchyman-ralph
Copy link
Collaborator

@ben-vargas since we merged your PR, is this PR still relevant ? I'm struggling to understand what this PR truly does other than a simple enum refactor

@ben-vargas
Copy link
Contributor

@ben-vargas since we merged your PR, is this PR still relevant ? I'm struggling to understand what this PR truly does other than a simple enum refactor

This PR can be closed - @tommy-ca and @jaruesink found valid issues and helped a lot on root cause to prompt toward a fix. One issue caused by Google's breaking change on OAuth and one caused by the ai-sdk provider implementation expecting gemini-cli's "gemini-api-key" but task-master using "api-key" (ai-sdk standard).

I thought it better ai-sdk-provider-gemini-cli handle both "api-key" or "gemini-api-key" auth options interchangeably for API Key to deliver to standards AI-SDK uses but also provide backwards compatibility for apps opting to use "gemini-api-key" since that's what gemini-cli uses.

@Crunchyman-ralph
Copy link
Collaborator

Great stuff, thanks for the feedback @ben-vargas

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.

5 participants