> ## Documentation Index
> Fetch the complete documentation index at: https://langwatch.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# Google Gemini Integration

> Instrument Google Gemini and Vertex AI calls in Go using the LangWatch SDK's native genai instrumentation.

LangWatch provides **native** instrumentation for Google's unified Gen AI Go SDK (`google.golang.org/genai`), which covers both the **Gemini Developer API** and **Vertex AI** behind one client. It traces at the HTTP layer the `genai` client uses, recording request and response attributes, including all token kinds Gemini reports (prompt, candidates, cached content, and `thoughts` as reasoning tokens), with full streaming support.

## Installation

```bash theme={null}
go get github.com/langwatch/langwatch/sdks/go
go get github.com/langwatch/langwatch/sdks/go/instrumentation/googlegenai
go get google.golang.org/genai
```

## Usage

> [!NOTE]
>
> Set `LANGWATCH_API_KEY` and `GEMINI_API_KEY` environment variables before running.

`WrapClientConfig` sets the client's HTTP transport to a traced one in place, preserving any transport you already configured. Register your LangWatch tracer provider first, then wrap the `genai.ClientConfig`:

```go theme={null}
package main

import (
	"context"
	"log"
	"os"

	langwatch "github.com/langwatch/langwatch/sdks/go"
	"github.com/langwatch/langwatch/sdks/go/instrumentation/googlegenai"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	"google.golang.org/genai"
)

func main() {
	ctx := context.Background()

	// Set up the LangWatch exporter (reads LANGWATCH_API_KEY from env).
	exporter, err := langwatch.NewExporter(ctx)
	if err != nil {
		log.Fatalf("failed to create exporter: %v", err)
	}
	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	otel.SetTracerProvider(tp)
	defer tp.Shutdown(ctx) // Critical: ensures traces are flushed.

	cc := &genai.ClientConfig{
		APIKey:  os.Getenv("GEMINI_API_KEY"),
		Backend: genai.BackendGeminiAPI,
	}
	googlegenai.WrapClientConfig(cc) // uses the global TracerProvider by default

	client, err := genai.NewClient(ctx, cc)
	if err != nil {
		log.Fatal(err)
	}

	resp, err := client.Models.GenerateContent(ctx, "gemini-2.5-flash", genai.Text("Hello, Gemini!"), nil)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Response: %s", resp.Text())
}
```

> [!NOTE]
>
> For the **Vertex AI** backend set `Backend: genai.BackendVertexAI`. If you build the `*http.Client` yourself, use `googlegenai.NewTransport()` (or `NewTransportWithBase(base, opts...)` to chain an authenticated transport for Vertex). The instrumentation captures input and output **by default**; pass `googlegenai.WithDataCapture(langwatch.DataCaptureNone)` to opt out.

> [!WARNING]
>
> The `defer tp.Shutdown(ctx)` call is essential. Without it, traces buffered in memory will be lost
> when your application exits.

## Check it worked

Run the program with `go run .`. The deferred `tp.Shutdown(ctx)` flushes the spans when `main` returns. Then open your project in LangWatch: a new trace appears in the traces list within a few seconds, with the Gemini call as an LLM span carrying its input, output and token counts.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the program runs in.

## Related

* [Go Integration Guide](https://langwatch.ai/docs/integration/go/guide.md) - Setup, manual spans, data capture and the REST client
* [Go SDK API Reference](https://langwatch.ai/docs/integration/go/reference.md#automatic-instrumentation) - Instrumentation options and collected attributes
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your Google Gemini applications


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.