OpenAI Integration (Python)
Themutagent-openai package provides a drop-in wrapper around the official OpenAI Python client that automatically traces all chat completion calls.
Installation
Once published to PyPI:mutagent-openai along with its dependencies: mutagent-tracing (core SDK) and openai (>= 1.0.0).
Quick Start
Every call toclient.chat.completions.create() is automatically traced and sent to MutagenT. No additional code changes required.
Full Example
Streaming
Streaming responses are fully supported. The wrapper accumulates streamed content and records the complete response in the span when the stream finishes.Streaming spans capture the accumulated output text after the stream completes. Token usage metrics are recorded when available from the model response.
What Gets Traced
Each call tochat.completions.create() generates a span with:
Input Messages
All messages sent to the model (system, user, assistant)
Output Messages
The model’s response content
Token Usage
Input tokens, output tokens, and total tokens
Model Info
Model name and provider (
openai)Latency
Request duration in milliseconds
Errors
Error messages with stack traces on failure
Span Details
| Field | Description | Example |
|---|---|---|
kind | Span type | llm.chat |
name | Model name | gpt-4 |
input.messages | Input chat messages | System + user messages |
output.messages | Response messages | Assistant response |
metrics.model | Model identifier | gpt-4-0613 |
metrics.provider | Provider name | openai |
metrics.input_tokens | Prompt tokens | 150 |
metrics.output_tokens | Completion tokens | 50 |
metrics.total_tokens | Total tokens | 200 |
Constructor Options
MutagentOpenAI accepts the same parameters as the official openai.OpenAI client:
| Parameter | Type | Description |
|---|---|---|
api_key | str | None | OpenAI API key (falls back to OPENAI_API_KEY env var) |
organization | str | None | OpenAI organization ID |
base_url | str | None | Custom API base URL |
timeout | float | None | Request timeout in seconds |
max_retries | int | None | Maximum retry count for failed requests |
Error Handling
When an API call fails, the span is automatically recorded with anERROR status and the error message is captured:
Nested Spans
MutagentOpenAI works with the core tracing SDK’s context propagation. If you create a parent span, OpenAI calls will automatically nest under it: