Facet
All docs

OpenAI Agents SDK — Integration Guide

Step-by-step developer guide for instrumenting an OpenAI Agents SDK application.

OpenAI Agents SDK Integration Guide (Developer)

This is the complete, step-by-step guide for a developer instrumenting a real OpenAI Agents SDK application with ADG. It reflects the adapter as actually validated against a real, independently-authored open-source application (openai/openai-cs-agents-demo) — not just this project's own hand-built demos — see "Real-world validation" below for what that means in practice and why it matters to you.

For the conceptual question "which of my own objects/calls should become a Trace vs. an Activity vs. an Artifact vs. a Resource?" — see the Ontology Mapping Decision Guide first. This guide assumes you already know the answer and shows the OpenAI Agents SDK-specific mechanics of acting on it.


1. The one-line integration

If your code already calls agents.Runner.run()/run_sync()/ run_streamed() on an agents.Agent, the entire integration is:

from agents import set_trace_processors
from adg.adapters.openai_agents import instrument_tracing

set_trace_processors([instrument_tracing()])

result = await Runner.run(my_agent, user_input)

instrument_tracing() returns an ADGTracingProcessor — a real agents.tracing.processor_interface.TracingProcessor subclass — wired with ADG's default transport (console by default; your Facet platform contact can help configure REST/production transport). No wrapping of your Agent/Runner calls, no subclassing, no changes to your agent's own tool/handoff/guardrail definitions.

Use agents.add_trace_processor(instrument_tracing()) instead of set_trace_processors([...]) if you want ADG's processor to run alongside the SDK's own default OpenAI-backend export processor (traces still visible in OpenAI's own platform, in addition to ADG) — both are the SDK's own public registration API; this adapter never registers itself on your behalf, exactly like every sibling adapter never calls a framework's own "attach callback" API internally either.

Every on_trace_start/on_trace_end and on_span_start/ on_span_end callback the SDK fires becomes a Trace/Activity/ Artifact/Resource/Lineage automatically — see §3 below for exactly how each maps.

Customizing what gets captured

All the same optional parameters every other ADG adapter supports are available on instrument_tracing(...):

from pathlib import Path
from adg.adapters.openai_agents import instrument_tracing
from adg.artifact_types import load_artifact_type_mapping

artifact_type_mapping = load_artifact_type_mapping(
    Path(__file__).parent / "artifact_types.toml"
)

processor = instrument_tracing(artifact_type_mapper=artifact_type_mapping)

See the Artifact Type Mapping guide for the full artifact_types.toml format — this classifies your agent's own outcome Artifacts (e.g. "this tool's return value is a DECISION") without ADG ever guessing at your business semantics itself.

No zero-code mode (yet)

Unlike LangGraph, Semantic Kernel, and the Direct LLM API, the OpenAI Agents SDK has no zero-code sitecustomize patch today — there is no agents-specific entry in adg.zero_code._PATCHERS. Setting ADG_ZERO_CODE_FRAMEWORKS=openai_agents is a no-op (it would log an "unknown framework" warning and do nothing). Code-level instrumentation (§1 above, one call to set_trace_processors/add_trace_processor) is the only supported integration path today. This is on our roadmap if your customer specifically needs zero-code.


2. How the OpenAI Agents SDK's events map onto the ontology

Unlike every other adapter in this codebase (LangGraph, LangChain, Semantic Kernel, Direct LLM), which all default to diffing opaque dict-shaped state (new_keys = after.keys() - before.keys()) to find Artifacts, the OpenAI Agents SDK never exposes dict-shaped customer state at all — every Span carries one fixed, SDK-authored SpanData subclass instead. DefaultArtifactExtractor/DefaultResourceExtractor therefore dispatch on span_data.type (a framework-runtime fact, not a guess about your business semantics) — see the adapter's own extractor logic for the full rationale.

Span.span_data.type Ontology entity Notes
The Runner.run()/run_sync()/run_streamed() call itself (Trace) Trace Opened on on_trace_start, closed on on_trace_end.
Every Span the SDK reports, regardless of type Activity "agent", "function", "generation", "guardrail", "handoff", "custom", "mcp_tools" spans are all Activities — nesting via span.parent_id becomes Lineage CONTAINS.
"function" (tool call) Artifact (the tool's return value, named after the tool) + Resource ("tool"-typed) Proven against a real multi-tool agent in our own validation suite.
"generation" (an LLM call) Artifact (the completion, if populated) + Resource ("llm"-typed, with usage metadata) Empirically, this span is only populated when a real model provider (or ScriptedModel(emit_traces=True), itself still empty as of SDK 0.24.0) actually fills it in — see §5's honest caveat.
"guardrail" (input or output) Artifact (the triggered/not-triggered verdict, named "<guardrail_name>_guardrail_result") Classify this as POLICY_EVALUATION via artifact_types.toml — see both demos.
"handoff" Artifact (the from/to agent transfer record) No special "handoff" ontology entity exists — this is an ordinary Artifact, same as everything else.
"custom" (including the SDK's own internal "task"/"turn" bookkeeping spans, and any custom_span(...) you author yourself) Artifact (the data dict verbatim, never interpreted) See our own internal test applications for a worked example of your own custom_span.
"mcp_tools" Artifact (the tool list reported) + Resource ("mcp_server"-typed)
The last root-level (parent_id is None) span's own .export() at on_trace_end TraceFinalStateCaptured A documented best-effort heuristic — see §4.
Any span recording an error during the trace TraceFailed (instead of TraceCompleted) See §4 — the SDK has no separate trace-error callback.

3. Two documented architectural heuristics (read before you rely on them)

The OpenAI Agents SDK's TracingProcessor interface is structurally different from every other framework's callback interface this project adapts, in two ways that required an explicit, documented policy rather than a direct 1:1 mapping:

  1. No on_trace_error callback exists. A Trace always ends via on_trace_end, win or lose — there is no separate "trace failed" callback the way there is on_chain_error/.on_graph_error in other adapters. ADGTracingProcessor tracks whether any span within a trace recorded an error (span.error is not None) during on_span_end; if so, it emits TraceFailed instead of TraceCompleted at on_trace_end, and skips TraceFinalStateCaptured on that path (mirroring the LangChain adapter's own on_chain_error convention of not capturing final state on failure).
  2. TracingProcessor never sees RunResult.final_output. Only Runner.run()'s own return value carries that; no TracingProcessor method receives it. TraceFinalStateCaptured is therefore populated via a best-effort heuristic: the .export() snapshot of the most recently completed span whose own parent_id is None — empirically, this is always the SDK's own internal "task"-named CustomSpanData span, which conveniently accumulates total usage/request counts across the whole run by the time it ends. If you need the exact RunResult.final_output value itself, read it from your own Runner.run() call's return value — ADGTracingProcessor cannot and does not fabricate it.

4. Guardrails: two supported shapes

The OpenAI Agents SDK supports both input_guardrails (run before the agent's own model call) and output_guardrails (run after the agent's own final answer, but before it reaches the caller) — both map onto the ontology identically (a "guardrail"-typed span, see §2's table), regardless of which kind, or what logic backs the check:

  • A plain function (support_triage's real Guardrails-AI-backed PII check, research_assistant's plain substring rule) — the common case.
  • A full nested agent run (await Runner.run(some_classifier_agent, ...) inside the guardrail function) — confirmed working with no adapter change required during real-world validation (see §5): the nested run's own spans (its own "agent"/"generation"/ "turn" spans) are captured as ordinary nested Activities under the very same Trace, correctly lineage-linked beneath the "guardrail" span, because the SDK's tracing context is ambient (propagated via contextvars, the same mechanism LangChain's own RunnableConfig propagation relies on — see the LangChain developer guide's own Pattern B).

5. Real-world validation: what this adapter was actually proven against

Beyond this project's own two hand-built internal test applications, the adapter was validated against a real, OpenAI-authored reference application:

openai/openai-cs-agents-demo (~6.6k★, MIT) — a 6-agent airline customer-service app (triage + 5 specialists), with 2 nested-agent-run guardrails (relevance_guardrail/jailbreak_guardrail, each a full Runner.run() call over its own classifier agent) and a real chatkit-backed agent context. This required vendoring a small, MIT-licensed, pinned-commit copy of the app's real airline/ business-logic module (not its FastAPI server or Next.js UI) rather than a plain uv pip install, since unlike LangChain's real-world validations, this app publishes no separate PyPI package of its own.

Result: zero changes to the adapter were needed. The nested-agent-run guardrail finding in §4 above was discovered, not assumed, during this validation — confirmed directly in the captured event stream (a guardrail:Relevance Guardrail Activity correctly containing a nested agent:Relevance Guardrail Activity, all within the same Trace).

One honest caveat, consistent with §2's table: this validation's offline-determinism approach (agents.testing.ScriptedModel) means no real "generation"-type span was ever populated with real model/usage data in either demo or this real-world validation — ScriptedModel(emit_traces=True) was directly tested and confirmed to still leave "generation" spans empty as of SDK 0.24.0. If your production app uses a real model provider, "generation" spans will populate with real model/usage/output fields the extractor already handles — this has simply not yet been proven against a live model call in our own test suite.


6. Where to go next

  • The Ontology Mapping Decision Guide — how to decide ontology boundaries for anything the SDK doesn't already structure for you.
  • The Artifact Type Mapping guide — classifying your agent's own outcome Artifacts.
  • The Field Mapping guide — vocabulary normalization (mapping your own field names onto canonical projection fields).
  • Your Facet platform contact — production transport, monitoring, and scaling for OpenAI Agents SDK-sourced traces.