track_openai

opik.integrations.openai.track_openai(openai_client: OpenAIClient, project_name: str | None = None, provider: str | LLMProvider | None = None) OpenAIClient

Adds Opik tracking wrappers to an OpenAI client.

The client is always patched; however every wrapped call checks opik.is_tracing_active() before emitting any telemetry. If tracing is disabled at call time, the wrapped function executes normally but no span/trace is sent.

Tracks calls to:

  • openai_client.chat.completions.create(), including support for stream=True mode.

  • openai_client.beta.chat.completions.parse()

  • openai_client.beta.chat.completions.stream()

  • openai_client.responses.create()

  • openai_client.videos.create(), videos.create_and_poll(), videos.poll(), videos.list(), videos.delete(), videos.remix(), videos.download_content(), and write_to_file() on downloaded content

  • openai_client.audio.speech.create() and openai_client.audio.speech.with_streaming_response.create()

Can be used within other Opik-tracked functions.

Parameters:
  • openai_client – An instance of OpenAI or AsyncOpenAI client.

  • project_name – The name of the project to log data.

  • provider – The provider name to record on every LLM span created by the integration. The OpenAI SDK is commonly used as a client for other OpenAI-compatible APIs (Together, OpenRouter, vLLM, DeepSeek, etc.), so this lets you record the actual model provider instead of the base URL host. Accepts any string, or one of the providers Opik recognizes for cost tracking via the opik.LLMProvider enum: “openai”, “anthropic”, “google_vertexai”, “google_ai”, “groq”, “bedrock”, “anthropic_vertexai”. When not provided, the provider is inferred from the client’s base URL (“openai” for api.openai.com, otherwise the base URL host).

Returns:

The modified OpenAI client with Opik tracking enabled.