Skip to content

[Feat] Add Support for calling Gemini/Vertex models in their native format #12046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ef48f06
init litellm google gen ai methods
ishaan-jaff Jun 25, 2025
d321cfa
feat init structure of functions for generate content
ishaan-jaff Jun 25, 2025
7aca67e
add init
ishaan-jaff Jun 25, 2025
663848c
add BaseGoogleGenAIGenerateContentConfig
ishaan-jaff Jun 25, 2025
b9b65a9
add generate_content_handler
ishaan-jaff Jun 25, 2025
69b62c7
add get_provider_google_genai_generate_content_config
ishaan-jaff Jun 25, 2025
b699ac6
fixes for generate content
ishaan-jaff Jun 25, 2025
a431868
add get_vertex_ai_project etc to base
ishaan-jaff Jun 25, 2025
a97be7e
use VertexBase
ishaan-jaff Jun 25, 2025
3f3992d
fixes for BaseGoogleGenAIGenerateContentConfig
ishaan-jaff Jun 25, 2025
a257c10
working validate env for google gemini
ishaan-jaff Jun 25, 2025
720cbef
feat - add transform google response
ishaan-jaff Jun 25, 2025
7abf2d9
fixes for transform_generate_content_request
ishaan-jaff Jun 25, 2025
c95b1e9
fix get_supported_generate_content_optional_params
ishaan-jaff Jun 25, 2025
64cf01d
add BaseGoogleGenAITest
ishaan-jaff Jun 25, 2025
495764e
working e2e test
ishaan-jaff Jun 25, 2025
9c0cda1
fixes init config
ishaan-jaff Jun 25, 2025
44fe773
use correct types
ishaan-jaff Jun 25, 2025
17f51ca
fix test for google gen ai
ishaan-jaff Jun 25, 2025
b3ce5d8
fix types
ishaan-jaff Jun 25, 2025
274bcef
add sync_get_auth_token_and_url
ishaan-jaff Jun 25, 2025
253d726
fixes for transform
ishaan-jaff Jun 25, 2025
87c494e
add llm http handler for google
ishaan-jaff Jun 25, 2025
7bf53a6
working non-streaming google endpoints
ishaan-jaff Jun 25, 2025
a55b8aa
add BaseGoogleGenAIGenerateContentStreamingIterator
ishaan-jaff Jun 25, 2025
59527d1
add GoogleGenAIGenerateContentStreamingIterator
ishaan-jaff Jun 25, 2025
c3e841d
fix working sync stream
ishaan-jaff Jun 25, 2025
33b7b69
fixes for litellm logging obj
ishaan-jaff Jun 25, 2025
4d8483f
working async streaming
ishaan-jaff Jun 25, 2025
bf2649d
add google gen ai types
ishaan-jaff Jun 25, 2025
161e800
fix - required imports
ishaan-jaff Jun 25, 2025
1f882ad
fix readme
ishaan-jaff Jun 25, 2025
1ca9c41
fix deps
ishaan-jaff Jun 25, 2025
d96ca77
fix deps
ishaan-jaff Jun 25, 2025
70744ca
fix ruff code QA checks
ishaan-jaff Jun 25, 2025
86739ca
fix linting
ishaan-jaff Jun 25, 2025
0a68985
fixes TYPE_CHECKING
ishaan-jaff Jun 25, 2025
63fb50a
fixes for typing
ishaan-jaff Jun 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions litellm/google_genai/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# 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

```python
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

```python
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

```python
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

```python
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.
19 changes: 19 additions & 0 deletions litellm/google_genai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
This allows using Google GenAI model in their native interface.

This module provides generate_content functionality for Google GenAI models.
"""

from .main import (
agenerate_content,
agenerate_content_stream,
generate_content,
generate_content_stream,
)

__all__ = [
"generate_content",
"agenerate_content",
"generate_content_stream",
"agenerate_content_stream",
]
Loading
Loading