Skip to content

Commit 86e26e9

Browse files
Feature: Make trace_include_sensitive_data configurable via environment variable (#1192)
Co-authored-by: SyedMohamedHyder <[email protected]>
1 parent e8d311b commit 86e26e9

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/agents/run.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import inspect
5+
import os
56
from dataclasses import dataclass, field
67
from typing import Any, Callable, Generic, cast, get_args
78

@@ -91,6 +92,12 @@ def get_default_agent_runner() -> AgentRunner:
9192
return DEFAULT_AGENT_RUNNER
9293

9394

95+
def _default_trace_include_sensitive_data() -> bool:
96+
"""Returns the default value for trace_include_sensitive_data based on environment variable."""
97+
val = os.getenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "true")
98+
return val.strip().lower() in ("1", "true", "yes", "on")
99+
100+
94101
@dataclass
95102
class ModelInputData:
96103
"""Container for the data that will be sent to the model."""
@@ -145,7 +152,9 @@ class RunConfig:
145152
"""Whether tracing is disabled for the agent run. If disabled, we will not trace the agent run.
146153
"""
147154

148-
trace_include_sensitive_data: bool = True
155+
trace_include_sensitive_data: bool = field(
156+
default_factory=_default_trace_include_sensitive_data
157+
)
149158
"""Whether we include potentially sensitive data (for example: inputs/outputs of tool calls or
150159
LLM generations) in traces. If False, we'll still create spans for these events, but the
151160
sensitive data will not be included.

tests/test_run_config.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,55 @@ async def test_agent_model_object_is_used_when_present() -> None:
8686
# the FakeModel on the agent.
8787
assert provider.last_requested is None
8888
assert result.final_output == "from-agent-object"
89+
90+
91+
def test_trace_include_sensitive_data_defaults_to_true_when_env_not_set(monkeypatch):
92+
"""By default, trace_include_sensitive_data should be True when the env is not set."""
93+
monkeypatch.delenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", raising=False)
94+
config = RunConfig()
95+
assert config.trace_include_sensitive_data is True
96+
97+
98+
@pytest.mark.parametrize(
99+
"env_value,expected",
100+
[
101+
("true", True),
102+
("True", True),
103+
("1", True),
104+
("yes", True),
105+
("on", True),
106+
("false", False),
107+
("False", False),
108+
("0", False),
109+
("no", False),
110+
("off", False),
111+
],
112+
ids=[
113+
"lowercase-true",
114+
"capital-True",
115+
"numeric-1",
116+
"text-yes",
117+
"text-on",
118+
"lowercase-false",
119+
"capital-False",
120+
"numeric-0",
121+
"text-no",
122+
"text-off",
123+
],
124+
)
125+
def test_trace_include_sensitive_data_follows_env_value(env_value, expected, monkeypatch):
126+
"""trace_include_sensitive_data should follow the environment variable if not explicitly set."""
127+
monkeypatch.setenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", env_value)
128+
config = RunConfig()
129+
assert config.trace_include_sensitive_data is expected
130+
131+
132+
def test_trace_include_sensitive_data_explicit_override_takes_precedence(monkeypatch):
133+
"""Explicit value passed to RunConfig should take precedence over the environment variable."""
134+
monkeypatch.setenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "false")
135+
config = RunConfig(trace_include_sensitive_data=True)
136+
assert config.trace_include_sensitive_data is True
137+
138+
monkeypatch.setenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "true")
139+
config = RunConfig(trace_include_sensitive_data=False)
140+
assert config.trace_include_sensitive_data is False

0 commit comments

Comments
 (0)