Skip to main content
The SGP Tracing SDK provides an integration for the openai-agents library via the OpenAITracingSGPProcessor. This processor automatically captures detailed traces from your AI agent runs, translating events from the agent’s lifecycle into SGP traces and spans. This provides deep visibility into your agent’s execution, including tool calls and model invocations, without requiring manual instrumentation.
This integration requires the openai-agents package. The processor will raise an ImportError if the package is not installed.

How it Works

The OpenAITracingSGPProcessor hooks into the openai-agents tracing system. You register it once, and it listens for lifecycle events, handling the translation to SGP tracing objects automatically.
  • Trace Management:
    • When an agent run starts, on_trace_start creates a corresponding SGP trace with the type AGENT_WORKFLOW. The root span of this trace is stored internally.
    • When the run ends, on_trace_end finds the stored root span and marks it as complete.
  • Span Management:
    • As the agent executes, on_span_start fires for each new operation. It creates a new SGP span, linking it to the correct parent (either another span or the root trace).
    • It uses the openai_span_type_map helper to convert the agent’s span type (e.g., “function”, “generation”) into an SGP SpanType (e.g., “CODE_EXECUTION”, “COMPLETION”).
    • When the operation finishes, on_span_end populates the span’s input, output, and metadata using helper functions that parse the data from the agent. If the agent reports an error, it’s recorded on the span.

Usage Example

To use the processor, you must initialize SGP tracing and then register an instance of OpenAITracingSGPProcessor with the openai-agents library.