Skip to main content
Google’s Gemini models include Gemini 2.0 Flash, Gemini 2.5 Pro, and other advanced multimodal language models. Braintrust integrates seamlessly with Gemini through direct API access, wrapper functions for automatic tracing, and proxy support.
This guide covers manual instrumentation. For quicker setup, use auto-instrumentation.

Setup

To use Gemini models, configure your Gemini API key in Braintrust.
  1. Get a Gemini API key from Google AI Studio
  2. Add the Gemini API key as an organization or project AI provider.
  3. Set the Gemini API key and your Braintrust API key as environment variables
.env
API keys are stored as one-way cryptographic hashes, never in plaintext.

Trace with Gemini

Trace your Gemini LLM calls for observability and monitoring using either the native Google GenAI SDK or the Braintrust gateway.

Trace automatically with native Google GenAI SDK

Braintrust provides wrapper functions that automatically log Google GenAI API calls. All subsequent API calls will be automatically traced. Braintrust traces these native Google GenAI SDK operations:
These wrapper functions are convenience functions that integrate the Braintrust logger with the Google GenAI client. For more control, see the manual wrapping section below.
Install the required packages:
Then wrap the Google GenAI client:

Stream responses with native Google GenAI SDK

The native Google GenAI client supports streaming with automatic tracing of token metrics.

Trace Live API and interactions

Braintrust traces the Google GenAI interactions API in both Python and TypeScript. In Python, with google-genai v1.30.0 or later, Braintrust also traces the Live API exposed through client.aio.live.connect() and automatically captures:
  • Async Live API round-trips
  • Async streaming responses inside Live API sessions
  • Tool calls made during Live API sessions
  • interactions.create(...), interactions.get(...), and interactions.delete(...)
In TypeScript, with @google/genai v1.33.0 or later, Braintrust traces foreground client.interactions.create() calls, including streaming responses. Background (fire-and-forget) calls are not traced. No additional setup is required beyond the normal Google GenAI wrapper or auto-instrumentation.

Manual wrapping for more control

If you need more control over when tracing is enabled, you can manually wrap the client.

Use Gemini with Braintrust gateway

The Braintrust gateway allows you to access Gemini models through a unified interface. Use any supported provider’s SDK to call Gemini models. Install the braintrust and openai packages.
Then, initialize the client and make a request to a Gemini model via the Braintrust gateway.

Trace gateway calls

When using the Braintrust gateway, API calls are automatically logged to the specified project.

Stream with proxy

Gemini models support streaming through the proxy.

Generate embeddings

The gateway also supports Gemini’s native embedContent and batchEmbedContents endpoints, including multimodal text and image content. See Generate embeddings for an example.

Evaluate with Gemini

Evaluations distill the non-deterministic outputs of Gemini models into an effective feedback loop that enables you to ship more reliable, higher quality products. Braintrust Eval is a simple function composed of a dataset of user inputs, a task, and a set of scorers. To learn more about evaluations, see the Experiments guide.

Evaluate with native SDK

Evaluate with proxy

Additional features

Reasoning models

Gemini 2.5 models (gemini-2.5-flash, gemini-2.5-pro) have built-in reasoning capabilities enabled by default. You can configure reasoning behavior using thinkingConfig.

Native SDK

Structured outputs

Gemini supports structured JSON outputs using response schemas.

Function calling and tools

Gemini supports function calling for building AI agents with tools.

Multimodal content

Gemini models support multimodal inputs including images, audio, and video.

Streaming with token metrics

Stream responses with automatic token tracking.

Context caching

Gemini supports context caching for efficient reuse of large contexts.

Use with Spring AI

For Java applications using Spring AI, you can integrate Braintrust by wrapping the underlying Google GenAI client and passing it to Spring AI’s GoogleGenAiChatModel.
This pattern works with all Spring AI features including streaming, function calling, and structured outputs. All calls through the ChatModel are automatically traced to Braintrust.

Error handling, attachments, and masking sensitive data

To learn more about these topics, check out the customize traces guide.
To learn more about multimodal support, attachments, error handling, and masking sensitive data with Gemini, visit the customize traces guide.