Google GenAI LLM Integration for LlamaIndex
This library provides the official integration for connecting LlamaIndex applications with Google's Generative AI models, including Gemini and Vertex AI. It allows LlamaIndex to leverage Google's LLMs for tasks such as text completion, chat interactions, function calling, and structured prediction. As of version 0.9.1, it is actively maintained with a regular release cadence, often aligning with updates to the core LlamaIndex framework.
Common errors
-
ImportError: cannot import name 'Gemini' from 'llama_index.llms' (unknown location)
cause Attempting to import the `Gemini` class from the old, deprecated `llama_index.llms.gemini` path instead of the new `llama_index.llms.google_genai` path.fixUpdate your import statement to `from llama_index.llms.google_genai import GoogleGenAI` and ensure `llama-index-llms-google-genai` is installed. -
google.api_core.exceptions.PermissionDenied: 403 Permission denied. Your application has insufficient authentication credentials to access this API. Please check the 'Authentication' section in the API documentation.
cause The `GOOGLE_API_KEY` environment variable is either not set, incorrect, or expired. Or, for Vertex AI, the `project_id` or `location` (region) are not correctly configured.fixSet the `GOOGLE_API_KEY` environment variable with a valid API key obtained from Google AI Studio. For Vertex AI, verify that `GOOGLE_GENAI_USE_VERTEXAI=True` and that `project_id` and `location` are correctly specified. -
google.api_core.exceptions.ResourceExhausted: 429 Resource has been exhausted (e.g. check your plan and billing).
cause You have exceeded the allowed API request rate limit or quota for Google Generative AI models.fixImplement a retry mechanism with exponential backoff in your application to handle transient rate limit errors. Review your Google Cloud project's API quotas and consider requesting an increase if your usage consistently exceeds limits.
Warnings
- breaking The `llama-index-llms-gemini` package has been discontinued since version 0.6.2 and replaced by `llama-index-llms-google-genai`. Direct imports from `llama_index.llms.gemini` will fail.
- gotcha Authentication requires setting the `GOOGLE_API_KEY` environment variable. For Vertex AI, additional configurations like `GOOGLE_GENAI_USE_VERTEXAI`, `region`, and `project_id` (either as environment variables or `vertexai_config` parameters) are necessary. Incorrect setup will lead to authentication errors.
- gotcha API rate limiting from Google GenAI can lead to `google.api_core.exceptions.ResourceExhausted` (HTTP 429) errors.
- gotcha Token usage metrics (promptTokens, completionTokens) might not be accurately populated or reported in some tracing and observability tools (e.g., Langfuse) for synchronous calls due to how the underlying Google GenAI SDK reports usage metadata.
Install
-
pip install llama-index-llms-google-genai -
pip install llama-index google-generativeai llama-index-llms-google-genai
Imports
- GoogleGenAI
from llama_index.llms.gemini import Gemini
from llama_index.llms.google_genai import GoogleGenAI
Quickstart
import os
from llama_index.llms.google_genai import GoogleGenAI
# Ensure your Google API key is set as an environment variable
# Or pass it directly: api_key="YOUR_API_KEY"
os.environ["GOOGLE_API_KEY"] = os.environ.get('GOOGLE_API_KEY', 'YOUR_GOOGLE_API_KEY_HERE')
llm = GoogleGenAI(model="gemini-pro") # Or use 'gemini-1.5-flash', 'gemini-1.5-pro', etc.
response = llm.complete("What is the capital of France?")
print(response)