Patch the Google Gen AI client to make it traceable.
BETA: This wrapper is in beta.
wrap_gemini(
client: C,
*,
tracing_extra: Optional[TracingExtra] = None,
chat_name: str = 'ChatGoogleGenerativeAI'
) -> CSupports:
generate_content and generate_content_stream methodsinline_data supportExample:
from google import genai
from google.genai import types
from langsmith import wrappers
# Use Google Gen AI client same as you normally would.
client = wrappers.wrap_gemini(genai.Client(api_key="your-api-key"))
# Basic text generation:
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Why is the sky blue?",
)
print(response.text)
# Streaming:
for chunk in client.models.generate_content_stream(
model="gemini-2.5-flash",
contents="Tell me a story",
):
print(chunk.text, end="")
# Tool/Function calling:
schedule_meeting_function = {
"name": "schedule_meeting",
"description": "Schedules a meeting with specified attendees.",
"parameters": {
"type": "object",
"properties": {
"attendees": {"type": "array", "items": {"type": "string"}},
"date": {"type": "string"},
"time": {"type": "string"},
"topic": {"type": "string"},
},
"required": ["attendees", "date", "time", "topic"],
},
}
tools = types.Tool(function_declarations=[schedule_meeting_function])
config = types.GenerateContentConfig(tools=[tools])
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Schedule a meeting with Bob and Alice tomorrow at 2 PM.",
config=config,
)
# Image generation:
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=["Create a picture of a futuristic city"],
)
# Save generated image
from io import BytesIO
from PIL import Image
for part in response.candidates[0].content.parts:
if part.inline_data is not None:
image = Image.open(BytesIO(part.inline_data.data))
image.save("generated_image.png")
Initial beta release of Google Gemini wrapper.
| Name | Type | Description |
|---|---|---|
client* | C | The Google Gen AI client to patch. |
tracing_extra | Optional[TracingExtra] | Default: NoneExtra tracing information. |
chat_name | str | Default: 'ChatGoogleGenerativeAI'The run name for the chat endpoint. |