AI SDK Provider for Gemini CLI
The 'ai-sdk-provider-gemini-cli' package serves as a community-developed provider for the Vercel AI SDK, enabling seamless integration with Google's Gemini large language models. Unlike direct API clients, this provider leverages the official Google Gemini CLI/SDK, specifically the '@google/gemini-cli-core' package, and utilizes Google Cloud Code endpoints. The current stable version is 2.0.1, offering full compatibility with AI SDK v6. This project maintains an active release cadence, frequently updating to support new Gemini models and AI SDK features. Key differentiators include its reliance on the robust Gemini CLI for authentication and model access, support for multimodal inputs (text, images, PDFs, audio, video), advanced tool/function calling, and native structured output with Zod schemas. It also provides specific configuration options like 'thinkingConfig' for fine-tuning reasoning levels in Gemini 3 models, and a flexible logging system. To use, developers must first globally install and authenticate the '@google/gemini-cli'.
Common errors
-
Error: Command failed: gemini generate --help gemini: command not found
cause The `@google/gemini-cli` global command-line interface is not installed or not accessible in your system's PATH.fixRun `npm install -g @google/gemini-cli` to install the Gemini CLI globally. Ensure your npm global bin directory is in your system's PATH. -
Error: Request failed with status code 401: You must be logged in to use this command. Run 'gemini' for interactive setup.
cause The Gemini CLI has not been authenticated with your Google account, or the OAuth credentials have expired/are invalid.fixExecute `gemini` in your terminal and follow the interactive prompts to authenticate your Google account via OAuth. -
TypeError: (0, _ai_sdk__WEBPACK_IMPORTED_MODULE_0__.generateText) is not a function
cause This error typically indicates a CommonJS/ESM module incompatibility. The Vercel AI SDK (and this provider since v2.x) are primarily designed for ESM, while your project might be configured for CommonJS, or the import path is incorrect.fixEnsure your `package.json` includes `"type": "module"` for ESM support. Use `import { generateText } from 'ai';` and other ESM import syntax. If using older AI SDK versions, verify your bundler configuration. -
Error: The model 'gemini-1.5-flash-latest' does not support structured output with 'responseJsonSchema'.
cause You are attempting to use the `responseJsonSchema` feature (for native structured output) with a Gemini model that does not natively support it, or an older provider version that doesn't handle it gracefully.fixUse a Gemini model explicitly listed as supporting `responseJsonSchema`, such as `gemini-3-pro-preview`. Ensure your `ai-sdk-provider-gemini-cli` version is `>=1.4.0` for native structured output support. -
Error: Your Node.js version (18.x.x) is not supported. This package requires Node.js >= 20.
cause The underlying `@google/gemini-cli-core` and consequently `ai-sdk-provider-gemini-cli` (since v1.3.0) require Node.js version 20 or higher.fixUpgrade your Node.js installation to version 20 or newer using a version manager like `nvm` (e.g., `nvm install 20 && nvm use 20`).
Warnings
- breaking Version 2.0.0 introduced significant breaking changes to align with Vercel AI SDK v6. This includes changes to the provider interface (`ProviderV2` → `ProviderV3`), token usage reporting (flat → hierarchical), warning format (`unsupported-setting` → `unsupported`), method renames (`textEmbeddingModel()` → `embeddingModel()`), and the finish reason structure (string → `{ unified, raw }` object).
- breaking A Node.js version of `>=20` is required since `ai-sdk-provider-gemini-cli` v1.3.0 and `gemini-cli-core` v0.16.0. Earlier Node.js versions will lead to runtime errors.
- gotcha This provider requires the `@google/gemini-cli` to be installed globally and authenticated separately before it can function. Without this setup, the provider cannot access Gemini models.
- gotcha Different major versions of `ai-sdk-provider-gemini-cli` are tied to specific major versions of the Vercel AI SDK. Version 2.x supports AI SDK v6 (NPM tag `latest`), version 1.x supports AI SDK v5 (NPM tag `ai-sdk-v5`), and version 0.x supports AI SDK v4 (NPM tag `ai-sdk-v4`).
- gotcha Prior to v2.0.1, when defining function tool parameter schemas, if only `properties` were provided, the `type: "object"` was not implicitly added, leading to Gemini function declaration validation errors.
Install
-
npm install ai-sdk-provider-gemini-cli -
yarn add ai-sdk-provider-gemini-cli -
pnpm add ai-sdk-provider-gemini-cli
Imports
- createGeminiProvider
const createGeminiProvider = require('ai-sdk-provider-gemini-cli').createGeminiProvider;import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli'; - generateText
const { generateText } = require('ai');import { generateText } from 'ai'; - ThinkingLevel
import { ThinkingLevel } from 'ai-sdk-provider-gemini-cli';
Quickstart
import { generateText } from 'ai';
import { createGeminiProvider } from 'ai-sdk-provider-gemini-cli';
async function main() {
// Ensure @google/gemini-cli is installed globally and authenticated
// npm install -g @google/gemini-cli
// gemini (then follow interactive setup)
const gemini = createGeminiProvider({
authType: 'oauth-personal', // Uses credentials from ~/.gemini/oauth_creds.json
// apiKey: process.env.GEMINI_API_KEY ?? '', // Alternative API key auth
});
try {
const result = await generateText({
model: gemini('gemini-3-pro-preview', {
temperature: 0.7,
maxOutputTokens: 50,
}),
prompt: 'Write a haiku about a majestic cat watching the sunrise.',
});
console.log('Generated Haiku:\n', result.text);
} catch (error) {
console.error('Error generating text:', error);
}
}
main();