Google Gen AI

Adds instrumentation for Google Gen AI SDK.

Import name: Sentry.instrumentGoogleGenAIClient

The instrumentGoogleGenAIClient helper adds instrumentation for the @google/genai SDK to capture spans by wrapping Google Gen AI SDK calls and recording LLM interactions with configurable input/output recording. You need to manually wrap your Google Gen AI client instance with this helper. See example below:

Copied
import { GoogleGenAI } from "@google/genai";

const genAI = new GoogleGenAI({
  apiKey: "your-api-key", // Warning: API key will be exposed in browser!
});

const client = Sentry.instrumentGoogleGenAIClient(genAI, {
  recordInputs: true,
  recordOutputs: true,
});

// Use the wrapped client instead of the original genAI instance
const result = await client.models.generateContent("Hello!");

To customize what data is captured (such as inputs and outputs), see the Options in the Configuration section.

The following options control what data is captured from Google Gen AI SDK calls:

Type: boolean (optional)

Records inputs to Google Gen AI SDK calls (such as prompts and messages).

Defaults to true if sendDefaultPii is true.

Type: boolean (optional)

Records outputs from Google Gen AI SDK calls (such as generated text and responses).

Defaults to true if sendDefaultPii is true.

Usage

Using the instrumentGoogleGenAIClient helper:

Copied
const client = Sentry.instrumentGoogleGenAIClient(genAI, {
  // your options here
});

By default, tracing support is added to the following Google Gen AI SDK calls:

  • models.generateContent() - Generate content with a given model
  • models.generateContentStream() - Stream content generation with a given model
  • chats.create() - Create chat sessions
  • sendMessage() - Send messages in chat sessions
  • sendMessageStream() - Stream messages in chat sessions

Streaming and non-streaming requests are automatically detected and handled appropriately.

  • @google/genai: >=0.10.0 <2
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").