Usage
Callpatch_openai_client() once after creating your OpenAI client. All subsequent client.chat.completions.create() calls are traced automatically. No changes to individual API calls are needed.
Works with both openai.OpenAI and openai.AsyncOpenAI.
What gets traced
By default, each non-streamingchat.completions.create() call produces its own trace.
Calls made with
stream=True are passed through untraced. Safely wrapping a
chunk iterator without disrupting the caller’s own consumption of it needs
different handling than a single request/response call, so streaming isn’t
covered by this integration yet.chat.completions.create() calls, so the patch can’t see them on its own. Use tracer.trace_tool_call() and tracer.trace_retrieval() to record them manually so they show up in the trace’s performance summary - see the Anthropic integration’s tool-use example for the same pattern (identical API, different client).
Multi-call agentic loops
Like the Anthropic integration, wrap a multi-call tool-use loop inwith tracer.trace(...) to collapse every chat.completions.create() call made inside it into one trace instead of one trace per call:
patch_openai_client() checks tracer.current_span on every call: if a span is active on the current thread, the call is attached to it as an "LLM Call N" step; otherwise it sends its own trace as usual.
patch_openai_client() reference
Calling
patch_openai_client() on an already-patched client is a no-op; it is safe to call multiple times.
Call
tracer.flush() before your process exits in scripts or one-shot jobs.
In long-running servers it is not required: traces drain automatically in the
background.
