Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

GoogleAIGeminiGenerator

Generate text using multimodal Gemini models through Google AI Studio.

Deprecation Notice

This integration will be deprecated soon. We recommend using GoogleGenAIChatGenerator instead, which provides unified access to both Gemini Developer API and Vertex AI.

Key Features

  • Text generation using multimodal Gemini models through Google AI Studio
  • Supports both text and image inputs for vision-based tasks
  • Streaming support for real-time token-by-token responses
  • Designed for text generation tasks, not for chat (use GoogleGenAIChatGenerator for chat)

Configuration

  1. Drag the GoogleAIGeminiGenerator component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    1. Enter your Google AI Studio API key. Get your API key from Google AI Studio. For detailed instructions, see Use Google AI Models.
    2. Select a model. For available models, see Google AI models.
  4. Go to the Advanced tab to configure generation settings, safety settings, and streaming.

Connections

GoogleAIGeminiGenerator accepts multimodal inputs through its parts input — a list of strings, ByteStream objects, or Part objects. It outputs generated text as replies (a list of strings).

Connect PromptBuilder's prompt output to this component's parts input. Connect the replies output to AnswerBuilder.

Source Code

To check this component's source code, open gemini.py in the Haystack Core Integrations repository.

Usage Examples

Basic Configuration

  GoogleAIGeminiGenerator:
type: haystack_integrations.components.generators.google_ai.gemini.GoogleAIGeminiGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- GOOGLE_API_KEY
strict: false
model: gemini-2.0-flash

Typically, you connect PromptBuilder to the parts input and AnswerBuilder to the replies output. This component is designed for text generation, not chat. For chat capabilities, use GoogleGenAIChatGenerator instead.

This query pipeline uses GoogleAIGeminiGenerator to generate text responses:

components:
bm25_retriever:
type: haystack_integrations.components.retrievers.opensearch.bm25_retriever.OpenSearchBM25Retriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: 'default'
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
top_k: 10
fuzziness: 0

PromptBuilder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: |
Given the following information, answer the question.

Context:
{% for document in documents %}
{{ document.content }}
{% endfor %}

Question: {{ query }}
required_variables:
variables:

GoogleAIGeminiGenerator:
type: haystack_integrations.components.generators.google_ai.gemini.GoogleAIGeminiGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- GOOGLE_API_KEY
strict: false
model: gemini-2.0-flash
generation_config:
safety_settings:
streaming_callback:

AnswerBuilder:
type: haystack.components.builders.answer_builder.AnswerBuilder
init_parameters:
pattern:
reference_pattern:

connections:
- sender: bm25_retriever.documents
receiver: PromptBuilder.documents
- sender: PromptBuilder.prompt
receiver: GoogleAIGeminiGenerator.parts
- sender: GoogleAIGeminiGenerator.replies
receiver: AnswerBuilder.replies
- sender: bm25_retriever.documents
receiver: AnswerBuilder.documents

inputs:
query:
- bm25_retriever.query
- PromptBuilder.query
- AnswerBuilder.query

outputs:
answers: AnswerBuilder.answers

max_runs_per_component: 100

metadata: {}

Parameters

Inputs

ParameterTypeDefaultDescription
partsVariadic[Union[str, ByteStream, Part]]A heterogeneous list of strings, ByteStream or Part objects.
streaming_callbackOptional[Callable[[StreamingChunk], None]]NoneA callback function that is called when a new token is received from the stream.

Outputs

ParameterTypeDescription
repliesList[str]A list of strings containing the generated responses.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
api_keySecretSecret.from_env_var('GOOGLE_API_KEY')Google AI Studio API key.
modelstrgemini-2.0-flashName of the model to use. For available models, see Google AI models.
generation_configOptional[Union[GenerationConfig, Dict[str, Any]]]NoneThe generation configuration to use. Can be a GenerationConfig object or a dictionary of parameters.
safety_settingsOptional[Dict[HarmCategory, HarmBlockThreshold]]NoneThe safety settings to use. A dictionary with HarmCategory as keys and HarmBlockThreshold as values.
streaming_callbackOptional[Callable[[StreamingChunk], None]]NoneA callback function that is called when a new token is received from the stream.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
partsVariadic[Union[str, ByteStream, Part]]A heterogeneous list of strings, ByteStream or Part objects.
streaming_callbackOptional[Callable[[StreamingChunk], None]]NoneA callback function that is called when a new token is received from the stream.