OpenAI Tracing

Instrument OpenAI SDK calls (Python & JS/TS) with OpenInference and view traces in Arize.

Checkout an end to end example for tracing OpenAI
See the folder for more OpenAI tutorials

OpenInference provides auto-instrumentation for the OpenAI Python Library and also supports the OpenAI JS/TS Library. Traces from both can be sent to Arize.

Note: This instrumentation also works with Azure OpenAI.

Launch Arize

To get started, sign up for a free Arize account and get your Space ID and API Key.

Install

pip install openinference-instrumentation-openai openai arize-otel

API Key Setup

Add your OpenAI API key as an environment variable:

export OPENAI_API_KEY='your_openai_api_key'

For Azure OpenAI, you would set AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, etc., as per Azure's requirements.

Use the register function to connect your application to Arize and instrument the OpenAI client:

from arize.otel import register

tracer_provider = register(
    space_id = "your-space-id", # in app space settings page
    api_key = "your-api-key", # in app space settings page
    project_name = "your-project-name", 
)

from openinference.instrumentation.openai import OpenAIInstrumentor
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)

Run OpenAI

The OpenAI client will automatically use the OPENAI_API_KEY (or Azure-specific variables) from your environment.

import openai

client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4o", # Or your chosen model, e.g., a gpt-3.5-turbo variant
    messages=[{"role": "user", "content": "Write a haiku about observability."}],
)
print(response.choices[0].message.content)

Observe

Now that you have tracing setup, all invocations of OpenAI (completions, chat completions, embeddings) using the instrumented client will be streamed to your Arize account for observability and evaluation.

JavaScript/TypeScript Support

OpenInference also provides instrumentation for the OpenAI JS/TS SDK. For setup and examples, please refer to the OpenInference JS examples for OpenAI.

Resources

Last updated

Was this helpful?