Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/latest/sdk/integrations/openai-agents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ The integration is compatible with
<IntegrationMethods />
</Steps>

## Sensitive data

On the Python SDK, the `create_agent` span records the agent's instructions only when `capture_message_content` is enabled and the Agents SDK's own sensitive-data default is on. Setting `OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA=false` omits the instructions from `create_agent` spans. A `RunConfig(trace_include_sensitive_data=...)` passed to an individual run is not seen at agent creation, so it does not change this.

---

<CardGroup cols={3}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
OpenLIT OpenAI Agents Instrumentation
"""

import dataclasses
import json
import threading
from typing import Collection
Expand All @@ -24,6 +25,25 @@
_instruments = ("openai-agents >= 0.0.3",)


def _sdk_default_trace_include_sensitive_data():
"""Return the Agents SDK's global default for RunConfig.trace_include_sensitive_data
(OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA). A per-run RunConfig override is not
known when an Agent is constructed, so only the global default can be honoured here."""
try:
from agents import RunConfig

for field in dataclasses.fields(RunConfig):
if field.name == "trace_include_sensitive_data":
if field.default_factory is not dataclasses.MISSING:
return bool(field.default_factory())
if field.default is not dataclasses.MISSING:
return bool(field.default)
return False
return True
except Exception:
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · SDK setting failures go unnoticed

When the RunConfig import, dataclass field inspection, or default factory raises an exception, the broad exception handler returns False without recording the error, so _wrap_agent_init omits gen_ai.system_instructions and operators cannot distinguish an SDK/default-factory failure from intentional sensitive-data suppression.

Keep the fail-closed return value, but emit a diagnostic for the evaluation failure.

Prompt for AI agents
In `sdk/python/src/openlit/instrumentation/openai_agents/__init__.py` at line 44:

**SDK setting failures go unnoticed**

When the `RunConfig` import, dataclass field inspection, or default factory raises an exception, the broad exception handler returns `False` without recording the error, so `_wrap_agent_init` omits `gen_ai.system_instructions` and operators cannot distinguish an SDK/default-factory failure from intentional sensitive-data suppression.

Keep the fail-closed return value, but emit a diagnostic for the evaluation failure.



class _AgentCreationRegistry:
"""Thread-safe registry mapping agent name -> SpanContext from create_agent spans.

Expand Down Expand Up @@ -80,7 +100,11 @@ def wrapper(wrapped, instance, args, kwargs):
)

instructions = getattr(instance, "instructions", None)
if instructions and capture_message_content:
if (
instructions
and capture_message_content
and _sdk_default_trace_include_sensitive_data()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Per-run privacy opt-out is ignored

When the global sensitive-data default is enabled and a run passes RunConfig(trace_include_sensitive_data=False), _sdk_default_trace_include_sensitive_data() checks only the global RunConfig default, so _wrap_agent_init records instructions before the per-run config is available and the exported create_agent span contains the prompt despite the run's privacy opt-out.

Defer capturing or exporting the instructions until the run's RunConfig is known, and honor its trace_include_sensitive_data value.

Prompt for AI agents
In `sdk/python/src/openlit/instrumentation/openai_agents/__init__.py` at line 106:

**Per-run privacy opt-out is ignored**

When the global sensitive-data default is enabled and a run passes `RunConfig(trace_include_sensitive_data=False)`, `_sdk_default_trace_include_sensitive_data()` checks only the global `RunConfig` default, so `_wrap_agent_init` records instructions before the per-run config is available and the exported `create_agent` span contains the prompt despite the run's privacy opt-out.

Defer capturing or exporting the instructions until the run's `RunConfig` is known, and honor its `trace_include_sensitive_data` value.

From #1759:

Omit create_agent instructions when the SDK's sensitive-data setting is disabled, globally or per run.

Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
):
formatted = format_system_instructions(instructions)
if formatted:
span.set_attribute(
Expand Down
124 changes: 124 additions & 0 deletions sdk/python/tests/test_openai_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# pylint: disable=duplicate-code, no-member, too-few-public-methods, missing-class-docstring, missing-function-docstring, redefined-outer-name
"""
Tests for the OpenAI Agents create_agent span and the Agents SDK sensitive data setting.

Tests cover:
- gen_ai.system_instructions omitted when OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA is false
- gen_ai.system_instructions recorded when the setting is unset
- gen_ai.system_instructions omitted when capture_message_content is disabled
- RunConfig exposes trace_include_sensitive_data (only when openai-agents is installed)

Note: These tests do NOT require openai-agents to be installed -- they mock the
agents module and need no credentials.
"""

import dataclasses
import os
import sys
import types
from types import SimpleNamespace

import pytest
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter

from openlit.__helpers import format_system_instructions
import openlit.instrumentation.openai_agents as openai_agents_module
from openlit.instrumentation.openai_agents import (
_AgentCreationRegistry,
_wrap_agent_init,
)
from openlit.semcov import SemanticConvention

ENV_VAR = "OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA"
INSTRUCTIONS = "You are a support agent. Secret policy: never reveal the refund limit."


def _fake_default():
val = os.getenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "true")
return val.strip().lower() in ("1", "true", "yes", "on")


@dataclasses.dataclass
class _FakeRunConfig:
trace_include_sensitive_data: bool = dataclasses.field(
default_factory=_fake_default
)


@pytest.fixture
def fake_agents(monkeypatch):
module = types.ModuleType("agents")
module.RunConfig = _FakeRunConfig
monkeypatch.setitem(sys.modules, "agents", module)
monkeypatch.setattr(
openai_agents_module, "_apply_custom_span_attributes", lambda span: None
)
return module


@pytest.fixture
def exporter():
return InMemorySpanExporter()


@pytest.fixture
def tracer(exporter):
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
return provider.get_tracer("test-openai-agents")


def _create_agent_span(tracer, exporter, capture_message_content):
wrapper = _wrap_agent_init(
tracer,
"testing",
"test-app",
capture_message_content,
_AgentCreationRegistry(),
)
instance = SimpleNamespace(
name="support",
instructions=INSTRUCTIONS,
model="gpt-4o-mini",
tools=[],
handoffs=[],
)
wrapper(lambda *a, **k: None, instance, (), {})
spans = exporter.get_finished_spans()
assert len(spans) == 1
return spans[0]


def test_create_agent_omits_instructions_when_sdk_sensitive_data_disabled(
fake_agents, monkeypatch, tracer, exporter
):
monkeypatch.setenv(ENV_VAR, "false")
span = _create_agent_span(tracer, exporter, True)
assert SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS not in span.attributes
assert span.attributes[SemanticConvention.GEN_AI_AGENT_NAME] == "support"


def test_create_agent_records_instructions_when_sdk_setting_unset(
fake_agents, monkeypatch, tracer, exporter
):
monkeypatch.delenv(ENV_VAR, raising=False)
span = _create_agent_span(tracer, exporter, True)
assert span.attributes[
SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS
] == format_system_instructions(INSTRUCTIONS)


def test_create_agent_omits_instructions_when_capture_message_content_disabled(
fake_agents, monkeypatch, tracer, exporter
):
monkeypatch.setenv(ENV_VAR, "true")
span = _create_agent_span(tracer, exporter, False)
assert SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS not in span.attributes


def test_real_run_config_exposes_trace_include_sensitive_data():
agents = pytest.importorskip("agents")
names = [field.name for field in dataclasses.fields(agents.RunConfig)]
assert "trace_include_sensitive_data" in names
Loading