mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-25 22:20:43 +00:00
* fix(logging): retain streaming success logging tasks until they finish Streaming chat, A2A, Gemini/Vertex and the proxy's deferred chat stream logging scheduled their success dispatch with a bare asyncio.create_task. The loop only holds weak references to tasks, so a dispatch suspended on an awaitable nothing else reaches could be garbage collected mid-flight and its spend log never written. Route them through spawn_logging_task, which keeps a strong reference until completion, logs failures and is drained at proxy shutdown. * test(logging): release parked logging callbacks on every exit path Wrap the garbage collection regressions in a context manager that releases the parked callback, waits for its task and drains the logging registry even when the test fails, assert exactly one parked callback, and leave the orphaned closure dispatch on its original schedule. * test(logging): cancel a parked logging callback that never finishes Cancel and await the parked task if it is still pending after release, fail the test in that case, and leave the streaming cache write on its original schedule since it hands off to an already tracked write without suspending. --------- Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com> |
||
|---|---|---|
| .. | ||
| adapters | ||
| __init__.py | ||
| main.py | ||
| Readme.md | ||
| streaming_iterator.py | ||
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 generationagenerate_content()- Asynchronous content generation
Streaming Functions
generate_content_stream()- Synchronous streaming content generationagenerate_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.