OpenAI SDK
Overview
respan-instrumentation-openai is an instrumentation plugin that auto-traces all OpenAI SDK calls (ChatCompletion, Completion, Embedding) using the opentelemetry-instrumentation-openai library. It also applies a sync prompt-capture patch to ensure input messages are reliably captured.
Version: 1.0.0 | Python: >=3.11, <3.14
Dependencies
Quick start
Public API
OpenAIInstrumentor
The main instrumentor class. Implements the Instrumentation protocol.
activate()
When called, activate():
- Imports
opentelemetry.instrumentation.openai.OpenAIInstrumentor(the OTEL community instrumentor). - Calls
instrumentor.instrument()if not already instrumented. - Applies a sync prompt-capture patch (see below).
- Sets
self._instrumented = True.
Calling activate() multiple times is safe (idempotent).
deactivate()
Calls OpenAIInstrumentor.uninstrument() on the OTEL instrumentor and resets the internal state.
What gets captured
The OTEL OpenAI instrumentor automatically creates spans for:
Span attributes
All spans carry standard GenAI semantic conventions:
The EnrichedSpan proxy in the exporter automatically injects llm.request.type="chat" for GenAI spans that carry gen_ai.system but lack llm.request.type. This ensures the Respan backend correctly parses prompt/completion data.
Sync prompt-capture patch
Problem
opentelemetry-instrumentation-openai v0.52+ has an async def _handle_request() for the chat wrapper. In synchronous contexts, this runs through asyncio.run() or spawns a thread, which can silently lose prompt attributes when:
_set_request_attributesraises onresponse_formathandling, killing the entire_handle_requestbefore_set_promptsruns.asyncio.run()/ thread path has environment-specific issues (Lambda, Docker, etc.).
Fix
The plugin replaces the async _handle_request with a synchronous version that has fault isolation between sections:
- Request attributes (model, temperature, etc.) — errors are caught and logged, don’t block prompts.
- Prompt capture — runs synchronously with inline content (no base64 upload).
- Trace propagation — reasoning effort and context propagation.
This ensures prompts always appear on the dashboard, even if other attribute sections fail.
Combined with decorators
With propagated attributes
vs auto-instrumentation
respan-tracing includes built-in auto-instrumentation for OpenAI (via the Instruments.OPENAI enum). The difference: