litellm/litellm/google_genai
mika-lt 6058bb03d2
fix(google_genai): honor snake_case generation_config on generateContent (#51139)
* fix(google_genai): honor snake_case generation_config on generateContent

Read the request config from generationConfig or generation_config in the shared setup step, and map maxOutputTokens, topP and stopSequences from either spelling on the non-Gemini adapter path, so a snake_case output cap is no longer dropped.

* fix(google_genai): read snake_case generation_config in the rate limiter

* test(google_genai): cover the limiter's Google config precedence

Covers an empty config next to a filled generationConfig and both config spellings in one request, for the cap check and the injected cap, and types the new test params as Mapping.

* fix(google_genai): give a custom generateContent hook an empty config as {} again

* fix(google_genai): read quoted generationConfig numbers and answer 400 for an invalid config

The adapter now reads temperature, the output cap, topP and the stop
sequences as Google types them, so a quoted number like "16" reaches
non-Gemini models as a number. A config that is not an object, or a
field value Google refuses, answers 400 in either spelling before any
provider call, where camelCase answered 500 and snake_case was dropped

* fix(google_genai): read numbers the way the Gemini API types them and accept a typed GenerateContentConfig

* fix(google_genai): read signed and leading-dot number strings and refuse non-finite floats as the Gemini API does

* fix(google_genai): match number strings without backtracking so a long malformed value cannot stall the event loop

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-18 03:47:08 +00:00
..
adapters fix(google_genai): honor snake_case generation_config on generateContent (#51139) 2026-11-18 03:47:08 +00:00
__init__.py refactor(lint): apply every safe ruff autofix and zero 28 strict-rule budgets 2026-08-01 15:43:29 -07:00
main.py fix(google_genai): honor snake_case generation_config on generateContent (#51139) 2026-11-18 03:47:08 +00:00
Readme.md [Feat] Add Support for calling Gemini/Vertex models in their native format (#12046) 2025-06-25 18:37:03 -07:00
streaming_iterator.py fix(logging): retain streaming success logging tasks until they finish (#52067) 2026-11-16 11:21:05 -08:00

LiteLLM Google GenAI Interface

Interface to interact with Google GenAI Functions in the native Google interface format.

Overview

This module provides a native interface to Google's Generative AI API, allowing you to use Google's content generation capabilities with both streaming and non-streaming modes, in both synchronous and asynchronous contexts.

Available Functions

Non-Streaming Functions

  • generate_content() - Synchronous content generation
  • agenerate_content() - Asynchronous content generation

Streaming Functions

  • generate_content_stream() - Synchronous streaming content generation
  • agenerate_content_stream() - Asynchronous streaming content generation

Usage Examples

Basic Non-Streaming Usage

from litellm.google_genai import generate_content, agenerate_content
from google.genai.types import ContentDict, PartDict

# Synchronous usage
contents = ContentDict(
    parts=[
        PartDict(text="Hello, can you tell me a short joke?")
    ],
)

response = generate_content(
    contents=contents,
    model="gemini-pro",  # or your preferred model
    # Add other model-specific parameters as needed
)

print(response)

Async Non-Streaming Usage

import asyncio
from litellm.google_genai import agenerate_content
from google.genai.types import ContentDict, PartDict

async def main():
    contents = ContentDict(
        parts=[
            PartDict(text="Hello, can you tell me a short joke?")
        ],
    )
    
    response = await agenerate_content(
        contents=contents,
        model="gemini-pro",
        # Add other model-specific parameters as needed
    )
    
    print(response)

# Run the async function
asyncio.run(main())

Streaming Usage

from litellm.google_genai import generate_content_stream
from google.genai.types import ContentDict, PartDict

# Synchronous streaming
contents = ContentDict(
    parts=[
        PartDict(text="Tell me a story about space exploration")
    ],
)

for chunk in generate_content_stream(
    contents=contents,
    model="gemini-pro",
):
    print(f"Chunk: {chunk}")

Async Streaming Usage

import asyncio
from litellm.google_genai import agenerate_content_stream
from google.genai.types import ContentDict, PartDict

async def main():
    contents = ContentDict(
        parts=[
            PartDict(text="Tell me a story about space exploration")
        ],
    )
    
    async for chunk in agenerate_content_stream(
        contents=contents,
        model="gemini-pro",
    ):
        print(f"Async chunk: {chunk}")

asyncio.run(main())

Testing

This module includes comprehensive tests covering:

  • Sync and async non-streaming requests
  • Sync and async streaming requests
  • Response validation
  • Error handling scenarios

See tests/unified_google_tests/base_google_test.py for test implementation examples.