Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ai-sdk-provider-gemini-cli

ben-vargas72.7kMIT1.1.2TypeScript support: included

Community AI SDK provider for Google Gemini using the official CLI/SDK

ai, vercel, ai-sdk, provider, gemini, google, gemini-cli, language-model, llm, streaming, generative-ai

readme

stable status npm stable version install size npm downloads Node.js ≥ 18 License: MIT Mentioned in Awesome Gemini CLI

AI SDK Provider for Gemini CLI

Stable Release: This version is compatible with AI SDK v5. For AI SDK v4 support, use version 0.x.

A community provider for the Vercel AI SDK that enables using Google's Gemini models through the @google/gemini-cli-core library and Google Cloud Code endpoints.

Note: This provider includes robust compatibility measures for @google/gemini-cli-core, protecting against breaking changes in patch versions through intelligent proxy patterns and exact version pinning.

Version Compatibility

Provider Version AI SDK Version NPM Tag Status Branch
1.x v5 latest Stable main
0.x v4 ai-sdk-v4 Stable ai-sdk-v4

Installing the Right Version

For AI SDK v5 (current, default):

npm install ai-sdk-provider-gemini-cli ai

For AI SDK v4 (legacy):

npm install ai-sdk-provider-gemini-cli@ai-sdk-v4 ai@^4.3.16

Disclaimer

This is an unofficial community provider and is not affiliated with or endorsed by Google or Vercel. By using this provider:

  • You understand that your data will be sent to Google's servers through the Gemini CLI Core library
  • You agree to comply with Google's Terms of Service
  • You acknowledge this software is provided "as is" without warranties of any kind

Please ensure you have appropriate permissions and comply with all applicable terms when using this provider.

Features

  • 🚀 Compatible with Vercel AI SDK (v4 and v5)
  • ☁️ Uses Google Cloud Code endpoints (https://cloudcode-pa.googleapis.com)
  • 🔄 Streaming support for real-time responses
  • 🛠️ Tool/function calling capabilities
  • 🖼️ Multimodal support (text and base64 images)
  • 🔐 OAuth authentication using Gemini CLI credentials
  • 📝 TypeScript support with full type safety
  • 🎯 Structured object generation with Zod schemas

Installation

1. Install and set up the Gemini CLI

npm install -g @google/gemini-cli
gemini  # Follow the interactive authentication setup

2. Add the provider

# For AI SDK v5 (current, default)
npm install ai-sdk-provider-gemini-cli ai

# For AI SDK v4 (legacy)
npm install ai-sdk-provider-gemini-cli@ai-sdk-v4 ai@^4.3.16

Quick Start

AI SDK v5

import { generateText } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';

// Create provider with OAuth authentication
const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

const result = await generateText({
  model: gemini('gemini-2.5-pro'),
  prompt: 'Write a haiku about coding',
});

console.log(result.content[0].text);

AI SDK v4

import { generateText } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

const { text } = await generateText({
  model: gemini('gemini-2.5-pro'),
  prompt: 'Write a haiku about coding',
});

console.log(text);

Breaking Changes in v1.x

See CHANGELOG.md for details on migrating from v0.x to v1.x.

Key changes:

  • Requires AI SDK v5
  • New response format with content arrays
  • Updated parameter names (maxTokens → maxOutputTokens)
  • New streaming API patterns
  • Updated token usage properties

Documentation

Examples

The examples/ directory contains comprehensive examples demonstrating all features:

Getting Started

  • check-auth.mjs - Verify your authentication setup
  • basic-usage.mjs - Simple text generation examples
  • streaming.mjs - Real-time streaming responses
  • conversation-history.mjs - Multi-turn conversations

Advanced Features

  • generate-object-basic.mjs - Structured output with Zod schemas
  • generate-object-nested.mjs - Complex nested data structures
  • generate-object-constraints.mjs - Data validation and constraints
  • system-messages.mjs - Control model behavior with system prompts
  • error-handling.mjs - Robust error handling patterns

Run Examples

# First build the project
npm run build

# Check authentication
npm run example:check

# Run basic examples
npm run example:basic

# Run all tests
npm run example:test

See the examples README for detailed documentation.

Authentication

The provider uses OAuth authentication with Google Cloud Code endpoints:

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

This uses your existing Gemini CLI credentials from ~/.gemini/oauth_creds.json. To set up authentication:

# Initial setup - follow interactive prompts
gemini

# Or change auth method inside CLI with slash command
/auth

API Key Authentication

// Using AI SDK standard auth type (recommended)
const gemini = createGeminiProvider({
  authType: 'api-key',
  apiKey: process.env.GEMINI_API_KEY,
});

// Alternative: Gemini-specific auth type
const gemini = createGeminiProvider({
  authType: 'gemini-api-key',
  apiKey: process.env.GEMINI_API_KEY,
});

Get your API key from Google AI Studio and set it as an environment variable:

export GEMINI_API_KEY="your-api-key-here"

Usage Examples

Text Generation

**AI SDK v5:

import { generateText } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

const result = await generateText({
  model: gemini('gemini-2.5-pro'),
  prompt: 'Explain quantum computing in simple terms',
  maxOutputTokens: 500,
});

console.log(result.content[0].text);
console.log(`Tokens used: ${result.usage?.totalTokens}`);

AI SDK v4:

const { text, usage } = await generateText({
  model: gemini('gemini-2.5-pro'),
  prompt: 'Explain quantum computing in simple terms',
  maxTokens: 500,
});

console.log(text);
console.log(`Tokens used: ${usage?.totalTokens}`);

Streaming Responses

import { streamText } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

const result = await streamText({
  model: gemini('gemini-2.5-pro'),
  prompt: 'Write a story about a robot learning to paint',
});

// v5: Access text stream
for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

// v4: Same API for streaming

Object Generation (Structured Output)

import { generateObject } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';
import { z } from 'zod';

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

const result = await generateObject({
  model: gemini('gemini-2.5-pro'),
  schema: z.object({
    name: z.string().describe('Product name'),
    price: z.number().describe('Price in USD'),
    features: z.array(z.string()).describe('Key features'),
  }),
  prompt: 'Generate a laptop product listing',
});

console.log(result.object);

System Messages

**AI SDK v5:

import { generateText } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
});

const result = await generateText({
  model: gemini('gemini-2.5-pro'),
  system: 'You are a helpful coding assistant. Always include code examples.',
  prompt: 'How do I read a file in Node.js?',
});

console.log(result.content[0].text);

AI SDK v4:

const { text } = await generateText({
  model: gemini('gemini-2.5-pro'),
  system: 'You are a helpful coding assistant. Always include code examples.',
  prompt: 'How do I read a file in Node.js?',
});

console.log(text);

Conversation History

**AI SDK v5:

const result = await generateText({
  model: gemini('gemini-2.5-pro'),
  messages: [
    { role: 'user', content: 'My name is Alice' },
    { role: 'assistant', content: 'Nice to meet you, Alice!' },
    { role: 'user', content: 'What is my name?' },
  ],
});

console.log(result.content[0].text); // Should mention "Alice"

AI SDK v4:

const { text } = await generateText({
  model: gemini('gemini-2.5-pro'),
  messages: [
    { role: 'user', content: 'My name is Alice' },
    { role: 'assistant', content: 'Nice to meet you, Alice!' },
    { role: 'user', content: 'What is my name?' },
  ],
});

console.log(text); // Should mention "Alice"

Supported Models

  • gemini-2.5-pro - Most capable model for complex tasks (64K output tokens)
  • gemini-2.5-flash - Faster model for simpler tasks (64K output tokens)

Note: This provider uses Google Cloud Code endpoints, which may have different model availability and rate limits than the direct Gemini API. The provider defaults to 64K output tokens to take full advantage of Gemini 2.5's capabilities.

Configuration

Model Settings

**AI SDK v5:

const model = gemini('gemini-2.5-pro', {
  // Standard AI SDK v5 parameters:
  temperature: 0.7,
  maxOutputTokens: 1000,
  topP: 0.95,
});

AI SDK v4:

const model = gemini('gemini-2.5-pro', {
  // Standard AI SDK v4 parameters:
  temperature: 0.7,
  maxTokens: 1000,
  topP: 0.95,
});

Provider Options

const gemini = createGeminiProvider({
  authType: 'oauth-personal',
  // Uses ~/.gemini/oauth_creds.json by default
});

Key Features

This provider uses Google's Cloud Code endpoints through the Gemini CLI Core library:

  • 🔐 Secure OAuth authentication
  • ☁️ Access to Google Cloud Code models
  • 🚀 Core Vercel AI SDK features
  • 📊 Structured output with JSON schemas
  • 🔄 Streaming support for real-time responses

Limitations

  • Requires Node.js ≥ 18
  • OAuth authentication requires the Gemini CLI to be installed globally
  • Rate limits may vary from the direct Gemini API
  • Very strict character length constraints in schemas may be challenging for the model
  • Image URLs not supported (use base64-encoded images)
  • Some AI SDK parameters not supported: frequencyPenalty, presencePenalty, seed
  • Only function tools supported (no provider-defined tools)
  • Abort signals have limited support: While the provider correctly handles abort signals and throws AbortError, the underlying gemini-cli-core does not support request cancellation. This means aborted requests will continue running in the background until completion, though the provider will throw an AbortError as soon as it detects the abort signal

Contributing

Contributions are welcome! Please see our Contributing Guide for details.

License

MIT - see LICENSE for details.

changelog

Changelog

All notable changes to this project will be documented in this file.

[1.1.2] - 2025-10-01

Fixed

  • Multimodal Support: Fixed image handling crash due to AI SDK v5 API change
    • Updated LanguageModelV2FilePart property from contentType to mediaType
    • Resolves critical issue preventing image attachments from working
  • Import Style: Changed to use import type for type-only imports for better tree-shaking

Added

  • ToolChoice Support: Complete implementation of AI SDK toolChoice functionality
    • Added mapGeminiToolConfig() function to convert AI SDK toolChoice to Gemini format
    • Proper allowedFunctionNames mapping when specific tool is forced
    • Support for all toolChoice types: auto, none, required, tool
  • Streaming Parity: Added toolConfig to both doGenerate and doStream methods
  • Test Coverage: Added 5 comprehensive tests for mapGeminiToolConfig covering all toolChoice scenarios

Changed

  • Dependency Update: Updated @google/gemini-cli-core from 0.1.22 to 0.6.1
    • Fully tested for backward compatibility
    • All 205 tests passing
    • All 12 examples verified working

Technical Details

  • Combines fixes from community PRs #16 (multimodal crash) and #17 (toolChoice support)
  • Enhanced with additional refinements, streaming parity, and comprehensive testing
  • No breaking changes - fully backward compatible

1.1.1 - 2025-08-22

Fixed

  • Critical OAuth Fix: Added isBrowserLaunchSuppressed() config method to prevent crashes during OAuth authentication (LOGIN_WITH_GOOGLE)
  • Compatibility: Full compatibility with @google/gemini-cli-core@0.1.22
    • Updated generateContent and generateContentStream to use UUID for userPromptId parameter
    • Added third sessionId parameter to createContentGenerator call
    • Pinned exact version 0.1.22 to prevent breaking changes from patch updates

Added

  • Robust Proxy Pattern: Enhanced config Proxy to handle multiple method patterns
    • Supports is* methods (return false by default)
    • Supports has* methods (return false by default)
    • Existing get* methods with intelligent defaults based on naming
  • Session Management: Generate and cache stable session ID per provider instance for better telemetry correlation
  • Comprehensive Documentation: Added docs/dependency-notes.md explaining version pinning rationale and Proxy implementation

Changed

  • Improved type consistency for authType in config object
  • Updated tests to cover OAuth methods and Proxy behavior

1.1.0 - 2025-08-18

Added

  • Zod 4 Compatibility: Added support for Zod v4 while maintaining backward compatibility with Zod v3
    • Runtime detection automatically uses the appropriate conversion method
    • Zod v3: Uses zod-to-json-schema package
    • Zod v4: Uses native z.toJSONSchema() function
    • Both versions listed in peerDependencies: "^3.0.0 || ^4.0.0"

Changed

  • Moved zod from dependencies to devDependencies to allow users to choose their version
  • Updated tool mapping to handle different JSON Schema outputs between Zod versions
    • Union types: Arrays in v3 vs anyOf in v4

Technical Details

  • Added convertZodToJsonSchema function for runtime version detection
  • Tests updated to handle both Zod v3 and v4 union type representations
  • Maintained full compatibility with existing API

1.0.1 - 2025-08-15

Changed

  • Stable Release: Vercel AI SDK v5 is now stable (no longer beta)
  • Updated all references from "v5-beta" to "v5"
  • Package marked as stable release

Fixed

  • Improved documentation clarity for abort signal limitations
  • Updated examples to reflect stable v5 API

Notes

This is the stable release of v1.0.0-beta.1 with Vercel AI SDK v5 now being officially stable. No breaking changes from v1.0.0-beta.1.

1.0.0-beta.1 - 2025-07-24

BREAKING CHANGES

This version is compatible with Vercel AI SDK v5. For v4 compatibility, please use version 0.x.x.

Changed

  • Provider Interface: Migrated from ProviderV1 to ProviderV2 interface

    • Updated createGeminiProvider() to return ProviderV2 interface
    • Provider now extends ProviderV2 base class
  • Language Model: Migrated from LanguageModelV1 to LanguageModelV2 interface

    • Changed specificationVersion from 'v1' to 'v2'
    • Updated response format to use v5 patterns
    • Improved streaming implementation with promise-based responses
  • Message Format: Updated to v5 message format

    • Messages now use ModelMessage types from v5
    • Tool results integrated into message flow
    • System messages properly supported
  • Response Format: Updated response structure

    • Streaming now returns promise with stream properties
    • Direct access to result.text and result.usage
    • Improved token usage tracking
  • Parameter Changes: Updated parameter names

    • maxTokensmaxOutputTokens in generation options
    • Token usage: promptTokensinputTokens, completionTokensoutputTokens
  • Error Handling: Enhanced error handling

    • Proper AbortError support for AI SDK retry logic
    • Better error mapping from Gemini errors
    • Safety status mapping for blocked content

Added

  • Comprehensive abort signal support (with documented limitations)
  • New documentation structure in docs/ai-sdk-v5/
    • BREAKING_CHANGES.md - Migration guide
    • GUIDE.md - Comprehensive usage guide
    • TROUBLESHOOTING.md - Common issues and solutions
    • DEVELOPMENT_STATUS.md - Implementation status

Fixed

  • System message implementation now works correctly
  • Error handling for "Failed after 3 attempts" issue
  • Stream error simulation in examples
  • Progress indicators in long-running tasks example

Known Issues

  • Abort signals work but underlying gemini-cli-core doesn't support request cancellation
  • maxOutputTokens may cause empty responses with gemini-2.5-pro
  • Schema validation errors show misleading "could not parse" messages

0.1.1 - 2025-01-20

Added

  • Compatibility with gemini-cli-core 0.1.12+ breaking changes
  • Comprehensive test suite with 98.85% coverage
  • GitHub Actions for automated testing

Fixed

  • Authentication type handling for new gemini-cli-core API
  • Error messages and types alignment

0.1.0 - 2025-01-15

Added

  • Full support for Vercel AI SDK v4
  • OAuth authentication via Gemini CLI
  • API key authentication support
  • Comprehensive examples directory
  • Tool/function calling support
  • Multimodal support (text and images)
  • Streaming responses

Changed

  • Stable API for v4 compatibility
  • Improved error handling
  • Better TypeScript types

0.0.4 - 2025-01-10

Fixed

  • Authentication configuration issues
  • Type definition exports

0.0.3 - 2025-01-05

Added

  • System message support
  • Object generation with Zod schemas
  • More comprehensive examples

Fixed

  • Message mapping for complex conversations

0.0.2 - 2025-06-28

BREAKING CHANGES

  • Removed pre-configured geminiProvider export
  • Users must now use createGeminiProvider() to create provider instances

Added

  • ESLint with modern flat config and TypeScript support
  • Vitest test suite with initial tests
  • Test coverage reporting (31.69% initial coverage)
  • Alpha warning badge to README

Changed

  • Simplified provider structure
  • Improved type safety throughout codebase

Fixed

  • TypeScript strict mode compliance issues

0.0.1 - 2025-06-25

Added

  • Initial release
  • Basic text generation support
  • OAuth authentication via Gemini CLI
  • Streaming support
  • Basic error handling