Skip to content

Feature: Make trace_include_sensitive_data configurable via environment variable #1192

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
11 changes: 10 additions & 1 deletion src/agents/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import copy
import inspect
import os
from dataclasses import dataclass, field
from typing import Any, Generic, cast

Expand Down Expand Up @@ -81,6 +82,12 @@ def get_default_agent_runner() -> AgentRunner:
return DEFAULT_AGENT_RUNNER


def _default_trace_include_sensitive_data() -> bool:
"""Returns the default value for trace_include_sensitive_data based on environment variable."""
val = os.getenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "true")
return val.strip().lower() in ("1", "true", "yes", "on")


@dataclass
class RunConfig:
"""Configures settings for the entire agent run."""
Expand Down Expand Up @@ -114,7 +121,9 @@ class RunConfig:
"""Whether tracing is disabled for the agent run. If disabled, we will not trace the agent run.
"""

trace_include_sensitive_data: bool = True
trace_include_sensitive_data: bool = field(
default_factory=_default_trace_include_sensitive_data
)
"""Whether we include potentially sensitive data (for example: inputs/outputs of tool calls or
LLM generations) in traces. If False, we'll still create spans for these events, but the
sensitive data will not be included.
Expand Down
52 changes: 52 additions & 0 deletions tests/test_run_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,55 @@ async def test_agent_model_object_is_used_when_present() -> None:
# the FakeModel on the agent.
assert provider.last_requested is None
assert result.final_output == "from-agent-object"


def test_trace_include_sensitive_data_defaults_to_true_when_env_not_set(monkeypatch):
"""By default, trace_include_sensitive_data should be True when the env is not set."""
monkeypatch.delenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", raising=False)
config = RunConfig()
assert config.trace_include_sensitive_data is True


@pytest.mark.parametrize(
"env_value,expected",
[
("true", True),
("True", True),
("1", True),
("yes", True),
("on", True),
("false", False),
("False", False),
("0", False),
("no", False),
("off", False),
],
ids=[
"lowercase-true",
"capital-True",
"numeric-1",
"text-yes",
"text-on",
"lowercase-false",
"capital-False",
"numeric-0",
"text-no",
"text-off",
],
)
def test_trace_include_sensitive_data_follows_env_value(env_value, expected, monkeypatch):
"""trace_include_sensitive_data should follow the environment variable if not explicitly set."""
monkeypatch.setenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", env_value)
config = RunConfig()
assert config.trace_include_sensitive_data is expected


def test_trace_include_sensitive_data_explicit_override_takes_precedence(monkeypatch):
"""Explicit value passed to RunConfig should take precedence over the environment variable."""
monkeypatch.setenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "false")
config = RunConfig(trace_include_sensitive_data=True)
assert config.trace_include_sensitive_data is True

monkeypatch.setenv("OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA", "true")
config = RunConfig(trace_include_sensitive_data=False)
assert config.trace_include_sensitive_data is False