OpenAI

Manually instrument the OpenAI SDK in React Native to capture spans and LLM interactions.

Import name: Sentry.instrumentOpenAiClient

The instrumentOpenAiClient helper adds instrumentation for the openai SDK by wrapping an OpenAI client instance and recording LLM interactions with configurable input/output capture. The OpenTelemetry-based automatic integration available for Node.js does not work in React Native, so wrapping the client manually is the only supported path.

Copied
import * as Sentry from "@sentry/react-native";
import OpenAI from "openai";

const openai = new OpenAI({
  // Warning: API keys included in your app bundle will be visible to anyone who
  // inspects the bundle. Proxy LLM calls through your own backend whenever possible.
  apiKey: "your-api-key",
});

const client = Sentry.instrumentOpenAiClient(openai, {
  recordInputs: true,
  recordOutputs: true,
});

// Use the wrapped client instead of the original openai instance
const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Hello!" }],
});

Make sure tracing is enabled for the spans produced by this integration to be captured.

The following options control what data is captured from OpenAI SDK calls:

Type: boolean (optional)

Records inputs to OpenAI SDK calls (such as prompts and messages).

Defaults to true if sendDefaultPii is true.

Type: boolean (optional)

Records outputs from OpenAI SDK calls (such as generated text and responses).

Defaults to true if sendDefaultPii is true.

By default, tracing support is added to the following OpenAI SDK calls:

  • chat.completions.create() - Chat completion requests
  • responses.create() - Response SDK requests

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

  • openai: >=4.0.0 <7
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").