Repository navigation
fix(openai-agents): keep create_agent instructions out of traces when the SDK excludes sensitive data #1981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| OpenLIT OpenAI Agents Instrumentation | ||
| """ | ||
|
|
||
| import dataclasses | ||
| import json | ||
| import threading | ||
| from typing import Collection | ||
|
|
@@ -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 | ||
|
|
||
|
|
||
| class _AgentCreationRegistry: | ||
| """Thread-safe registry mapping agent name -> SpanContext from create_agent spans. | ||
|
|
||
|
|
@@ -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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Defer capturing or exporting the instructions until the run's Prompt for AI agentsIn `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.
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| ): | ||
| formatted = format_system_instructions(instructions) | ||
| if formatted: | ||
| span.set_attribute( | ||
|
|
||
| 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 |
There was a problem hiding this comment.
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
RunConfigimport, dataclass field inspection, or default factory raises an exception, the broad exception handler returnsFalsewithout recording the error, so_wrap_agent_initomitsgen_ai.system_instructionsand 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