Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
title: Google Gen AI
description: "Adds instrumentation for Google Gen AI SDK."
supported:
- javascript.node
- javascript.aws-lambda
- javascript.azure-functions
- javascript.connect
- javascript.express
- javascript.fastify
- javascript.gcp-functions
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
- javascript.electron
- javascript.nextjs
- javascript.nuxt
- javascript.solidstart
- javascript.sveltekit
- javascript.react-router
- javascript.remix
- javascript.astro
- javascript.bun
- javascript.tanstackstart-react
- javascript.cloudflare
---

<Alert>

This integration works in the Node.js, Cloudflare Workers, and Vercel Edge Functions runtimes. It requires SDK version `10.14.0` or higher.

</Alert>

_Import name: `Sentry.googleGenAIIntegration`_

The `googleGenAIIntegration` adds instrumentation for the `@google/genai` SDK to capture spans by automatically wrapping Google Gen AI client calls and recording LLM interactions with configurable input/output recording.

<PlatformSection notSupported={["javascript.cloudflare", "javascript.nextjs"]}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this section also be visible for Next.JS? IIUC only the edge runtime needs the other setup?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for edge runtime, we are handling this down in a <PlatformSection supported={['javascript.nextjs']}> section

It is enabled by default and will automatically capture spans for Google Gen AI SDK method calls. You can opt-in to capture inputs and outputs by setting `recordInputs` and `recordOutputs` in the integration config:

```javascript
Sentry.init({
dsn: "____PUBLIC_DSN____",
tracesSampleRate: 1.0,
integrations: [
Sentry.googleGenAIIntegration({
recordInputs: true,
recordOutputs: true,
}),
],
});
```

</PlatformSection>

<PlatformSection supported={["javascript.cloudflare"]}>
For Cloudflare Workers, you need to manually instrument the Google Gen AI client using the `instrumentGoogleGenAIClient` helper:

```javascript
import * as Sentry from "@sentry/cloudflare";
import { GoogleGenAI } from "@google/genai";

const genAI = new GoogleGenAI(process.env.API_KEY);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Incorrect Environment Variable Access in Code Examples

The code examples for Cloudflare Workers and Next.js Edge runtime incorrectly use process.env.API_KEY. These environments have restricted or different mechanisms for accessing environment variables, so process.env isn't the correct approach here.

Additional Locations (1)

Fix in Cursor Fix in Web

const client = Sentry.instrumentGoogleGenAIClient(genAI, {
Comment on lines +64 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-l: General question (not just this snippet) Is client also mentioned by the regular google documentation for how to use the library? if they use a different common name, I'd rather go with that, just to keep things familiar for users.

Maybe this

Suggested change
const genAI = new GoogleGenAI(process.env.API_KEY);
const client = Sentry.instrumentGoogleGenAIClient(genAI, {
const originialGenAI = new GoogleGenAI(process.env.API_KEY);
const genAI = Sentry.instrumentGoogleGenAIClient(originialGenAI, {

or

Suggested change
const genAI = new GoogleGenAI(process.env.API_KEY);
const client = Sentry.instrumentGoogleGenAIClient(genAI, {
const genAI = new GoogleGenAI(process.env.API_KEY);
const instrumentedGenAi = Sentry.instrumentGoogleGenAIClient(genAI, {

happy to go with whatever makes most sense. If client is that, then no need to change anything 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They use ai https://www.npmjs.com/package/@google/genai (which i'd rather not use because Vercel's package is ai) andclient https://googleapis.github.io/python-genai/ interchangeably, but mos actually refer to ai as client!

recordInputs: true,
recordOutputs: true,
});

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

</PlatformSection>

<PlatformSection supported={['javascript.nextjs']}>

This integration is automatically instrumented in the Node.js runtime. For Next.js applications using the Edge runtime, you need to manually instrument the Google Gen AI client:

```javascript
import * as Sentry from "@sentry/nextjs";
import { GoogleGenAI } from "@google/genai";

const genAI = new GoogleGenAI(process.env.API_KEY);
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!");
```

</PlatformSection>

## Options

### `recordInputs`

_Type: `boolean`_

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

Defaults to `true` if `sendDefaultPii` is `true`.

```javascript
Sentry.init({
integrations: [Sentry.googleGenAIIntegration({ recordInputs: true })],
});
```

### `recordOutputs`

_Type: `boolean`_

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

Defaults to `true` if `sendDefaultPii` is `true`.

```javascript
Sentry.init({
integrations: [Sentry.googleGenAIIntegration({ recordOutputs: true })],
});
```

## Configuration

By default this integration adds tracing support to Google Gen AI SDK method calls including:

- `models.generateContent()` - Make an API request to generate content with a given model.
- `models.generateContentStream()` - Make an API request to generate content with a given model and yields the response in chunks.
- `chats.create()` - Create chat sessions.
- `sendMessage()` - Send messages in chat sessions.
- `sendMessageStream()` - Stream messages in chat sessions.

The integration will automatically detect streaming vs non-streaming requests and handle them appropriately.

## Supported Versions

- `@google/genai`: `>=0.10.0 <2`
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The JavaScript SDK supports automatic instrumentation for some AI libraries. We
</PlatformLink>
- <PlatformLink to="/configuration/integrations/openai/">OpenAI</PlatformLink>
- <PlatformLink to="/configuration/integrations/anthropic/">Anthropic</PlatformLink>
- <PlatformLink to="/configuration/integrations/google-genai/">Google Gen AI SDK</PlatformLink>

## Manual Instrumentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ Depending on whether an integration enhances the functionality of a particular r
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`amqplibIntegration`](./amqplib) | | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`dataloaderIntegration`](./dataloader) | | | ✓ | |
| [`extraErrorDataIntegration`](./extraerrordata) | | | | ✓ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`eventLoopBlockIntegration`](./event-loop-block) | | ✓ | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`eventLoopBlockIntegration`](./event-loop-block) | | ✓ | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`amqplibIntegration`](./amqplib) | | | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`anrIntegration`](./anr) | | ✓ | | |
| [`captureConsoleIntegration`](./captureconsole) | | | | ✓ |
| [`eventLoopBlockIntegration`](./event-loop-block) | | ✓ | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
| [`unleashIntegration`](./unleash) | | | | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Depending on whether an integration enhances the functionality of a particular r
| [`vercelAiIntegration`](./vercelai) | ✓ | | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |

### Edge Integrations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ Depending on whether an integration enhances the functionality of a particular r
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ Depending on whether an integration enhances the functionality of a particular r
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ Depending on whether an integration enhances the functionality of a particular r
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ Depending on whether an integration enhances the functionality of a particular r
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | ✓ |
| [`openAIIntegration`](./openai) | ✓ | | ✓ | |
| [`anthropicAIIntegration`](./anthropic) | ✓ | ✓ | ✓ | |
| [`googleGenAIIntegration`](./google-genai) | ✓ | ✓ | ✓ | |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Loading