Skip to main content

Overview

The GeminiClient provides integration with Google’s Gemini language models, including Gemini 3.0 Flash/Pro and Gemini 2.5 series with support for thinking configurations.

Installation

Basic Usage

Constructor

LLMConfig | None
default:"None"
Configuration object. If None, creates default config.
bool
default:"False"
Enable response caching (stored in ./llm_cache)
int | None
default:"None"
Maximum output tokens. If not set, uses model-specific defaults (see table below).
types.ThinkingConfig | None
default:"None"
Optional thinking configuration for Gemini 2.5+ models that support enhanced reasoning.
genai.Client | None
default:"None"
Optional pre-configured genai.Client instance. If not provided, creates one from config.

Supported Models

The client supports all Gemini models with model-specific max token limits:

Gemini 3 (Preview) - 64K output

  • gemini-3-pro-preview
  • gemini-3-flash-preview (default)

Gemini 2.5 - 64K output

  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite (64K)

Gemini 2.0 - 8K output

  • gemini-2.0-flash
  • gemini-2.0-flash-lite

Gemini 1.5 - 8K output

  • gemini-1.5-pro
  • gemini-1.5-flash
  • gemini-1.5-flash-8b

Max Tokens Resolution

Similar to AnthropicClient, max tokens are resolved with the following precedence:
  1. Explicit parameter to generate_response()
  2. Instance max_tokens set during initialization
  3. Model-specific maximum from the mapping above
  4. Default fallback: 8192 tokens

Thinking Configuration

Gemini 2.5+ models support enhanced reasoning modes:
Only use thinking_config with Gemini 2.5+ models. Earlier models do not support this feature.

Structured Output via Schema

Gemini uses JSON schema for structured outputs:
Generation configuration:

Model Size Selection

Use model_size parameter to automatically select between models:

Error Handling

Safety Blocks

Gemini may block content for safety reasons:
Safety information is extracted from the response:

Rate Limits

Rate limit detection checks for:
  • “rate limit” in error message
  • “quota” in error message
  • “resource_exhausted” in error message
  • HTTP 429 status code

Automatic Retries

The client retries up to 2 times for:
  • JSON parsing errors
  • Validation errors
  • Transient API failures
Retry with error context:

JSON Salvage

If output is truncated or malformed, the client attempts to salvage partial JSON:
This is useful when responses are cut off due to max_tokens limits.

Token Usage Tracking

The client extracts token counts from Gemini’s response:

System Instructions

System messages and schema instructions are combined:

Example: Batch Processing

Performance Tips

  1. Use Flash variants for speed: gemini-3-flash-preview is much faster than Pro
  2. Set appropriate max_tokens: Don’t request 64K if you only need 2K
  3. Use model_size=ModelSize.small for simple tasks
  4. Enable caching for repeated queries
  5. Monitor safety blocks: Adjust prompts if frequently blocked

Prompt Feedback

Check if your prompt was blocked:
Common block reasons:
  • SAFETY: Content policy violation
  • OTHER: Other blocking reason