llama-index llms google genai integration
Project Links
Meta
Author: Your Name
Requires Python: <4.0,>=3.9
Classifiers
LlamaIndex Llms Integration: Google GenAI
Installation
-
Install the required Python packages:
%pip install llama-index-llms-google-genai
-
Set the Google API key as an environment variable:
%env GOOGLE_API_KEY=your_api_key_here
Usage
Basic Content Generation
To generate a poem using the Gemini model, use the following code:
from llama_index.llms.google_genai import GoogleGenAI
llm = GoogleGenAI(model="gemini-2.0-flash")
resp = llm.complete("Write a poem about a magic backpack")
print(resp)
Chat with Messages
To simulate a conversation, send a list of messages:
from llama_index.core.llms import ChatMessage
from llama_index.llms.google_genai import GoogleGenAI
messages = [
ChatMessage(role="user", content="Hello friend!"),
ChatMessage(role="assistant", content="Yarr what is shakin' matey?"),
ChatMessage(
role="user", content="Help me decide what to have for dinner."
),
]
llm = GoogleGenAI(model="gemini-2.0-flash")
resp = llm.chat(messages)
print(resp)
Streaming Responses
To stream content responses in real-time:
from llama_index.llms.google_genai import GoogleGenAI
llm = GoogleGenAI(model="gemini-2.0-flash")
resp = llm.stream_complete(
"The story of Sourcrust, the bread creature, is really interesting. It all started when..."
)
for r in resp:
print(r.text, end="")
To stream chat responses:
from llama_index.core.llms import ChatMessage
from llama_index.llms.google_genai import GoogleGenAI
llm = GoogleGenAI(model="gemini-2.0-flash")
messages = [
ChatMessage(role="user", content="Hello friend!"),
ChatMessage(role="assistant", content="Yarr what is shakin' matey?"),
ChatMessage(
role="user", content="Help me decide what to have for dinner."
),
]
resp = llm.stream_chat(messages)
Specific Model Usage
To use a specific model, you can configure it like this:
from llama_index.llms.google_genai import GoogleGenAI
llm = GoogleGenAI(model="models/gemini-pro")
resp = llm.complete("Write a short, but joyous, ode to LlamaIndex")
print(resp)
Asynchronous API
To use the asynchronous completion API:
from llama_index.llms.google_genai import GoogleGenAI
llm = GoogleGenAI(model="models/gemini-pro")
resp = await llm.acomplete("Llamas are famous for ")
print(resp)
For asynchronous streaming of responses:
resp = await llm.astream_complete("Llamas are famous for ")
async for chunk in resp:
print(chunk.text, end="")
0.6.2
Sep 26, 2025
0.6.1
Sep 25, 2025
0.6.0
Sep 24, 2025
0.5.1
Sep 22, 2025
0.5.0
Sep 15, 2025
0.4.0
Sep 15, 2025
0.3.1
Sep 08, 2025
0.3.0
Jul 30, 2025
0.2.6
Jul 28, 2025
0.2.5
Jul 16, 2025
0.2.4
Jul 09, 2025
0.2.3
Jul 02, 2025
0.2.2
Jun 24, 2025
0.2.1
Jun 02, 2025
0.2.0
May 30, 2025
0.1.14
May 28, 2025
0.1.13
May 15, 2025
0.1.12
May 05, 2025
0.1.11
May 03, 2025
0.1.10
Apr 30, 2025
0.1.9
Apr 28, 2025
0.1.8
Apr 24, 2025
0.1.7
Mar 27, 2025
0.1.6
Mar 24, 2025
0.1.5
Mar 24, 2025
0.1.4
Mar 14, 2025
0.1.3
Mar 12, 2025
0.1.2
Mar 12, 2025
0.1.1
Mar 09, 2025
0.1.0.post1
Mar 08, 2025
0.1.0
Mar 08, 2025
Wheel compatibility matrix
Files in release
Extras:
None
Dependencies:
google-genai
(<2,>=1.24.0)
llama-index-core
(<0.15,>=0.14.3)
pillow
(>=10.2.0)