Repository navigation
fix(openai-agents): keep create_agent instructions out of traces when the SDK excludes sensitive data - #1981
Conversation
… the SDK excludes sensitive data
Reviewer's GuideImplementationRead the SDK-default helper alongside the instruction-capture condition it governs, so the privacy decision and its use can be reviewed together.
DocumentationReview the user-facing explanation of when agent instructions appear on create_agent spans and the limitation of per-run settings.
TestsCheck the tests for SDK-default filtering, existing capture_message_content behavior, and the RunConfig field contract.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoRespect Agents SDK privacy setting on create_agent spans
AI Description
Merge Risk
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can route each severity your way: inline, summary, both, or drop |
Linked issuesChecked at commit db2f428 #1759 Bug: OpenAI Agents `create_agent` span records the agent's instructions although the Agents SDK is set to keep sensitive data out of traces · 1 of 1 met
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/python/src/openlit/instrumentation/openai_agents/__init__.py" line_range="106" />
<code_context>
+ if (
+ instructions
+ and capture_message_content
+ and _sdk_default_trace_include_sensitive_data()
+ ):
formatted = format_system_instructions(instructions)
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 2
<location path="sdk/python/src/openlit/instrumentation/openai_agents/__init__.py" line_range="44" />
<code_context>
+ return bool(field.default_factory())
+ if field.default is not dataclasses.MISSING:
+ return bool(field.default)
+ return False
+ return True
+ except Exception:
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 3
<location path="sdk/python/src/openlit/instrumentation/openai_agents/__init__.py" line_range="106" />
<code_context>
+ if (
+ instructions
+ and capture_message_content
+ and _sdk_default_trace_include_sensitive_data()
+ ):
formatted = format_system_instructions(instructions)
</code_context>
<issue_to_address>
**New privacy behavior stays undocumented**
When the `create_agent` span starts honoring the SDK's sensitive-data setting, the OpenAI Agents integration guide does not explain that `create_agent` instructions are now omitted when the SDK's global sensitive-data setting is disabled, so OpenLIT users cannot learn about this behavior from the documentation.
Update the OpenAI Agents integration guide to document how the SDK's global sensitive-data setting affects captured instructions.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: sdk/python/src/openlit/instrumentation/openai_agents/__init__.py:106, sdk/python/src/openlit/instrumentation/openai_agents/__init__.py:44
| if ( | ||
| instructions | ||
| and capture_message_content | ||
| and _sdk_default_trace_include_sensitive_data() |
There was a problem hiding this comment.
🟠 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.| return False | ||
| return True | ||
| except Exception: | ||
| return False |
There was a problem hiding this comment.
🟡 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.
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
Issue number: Fixes #1759
Change description:
The Agents SDK lets an application keep model inputs and outputs out of traces with
OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA=false. OpenLIT's spans built from SDK span data already follow it, because the SDK leaves those fields empty. Thecreate_agentspan is the exception:_wrap_agent_initreadsinstance.instructionsdirectly and recorded the full system prompt ingen_ai.system_instructions, checking onlycapture_message_content.The fix asks the SDK for its own decision instead of re-implementing it.
_sdk_default_trace_include_sensitive_data()reads the default of the publicagents.RunConfig.trace_include_sensitive_datadataclass field (calling itsdefault_factory, without building aRunConfig), and the instructions are recorded only when that is true.capture_message_content=Falsestill omits the attribute, as before.Limitation: a per-run
RunConfig(trace_include_sensitive_data=False)cannot be honoured here. No run exists when anAgentis constructed, and one agent can later run under several configs. Only the global setting is honoured on this span. Users who need per-run privacy can use the environment variable orcapture_message_content=False.An earlier PR (#1786) took a similar route and was closed by its author. It imported the private
agents.run_config._default_trace_include_sensitive_data, which does not exist before the helper moved there, so on those SDK versions its fallback recorded the instructions even with the variable set to false. This one only uses the public dataclass field.How was this tested
CI does not run pytest for the Python SDK (the step is commented out in
ci-python.yml; only pylint runs), so this is local evidence.New
sdk/python/tests/test_openai_agents.py: three hermetic tests with a fakeagentsmodule, no API key, plus a contract test that skips whenopenai-agentsis not installed. Red on the unmodified source, on the attribute value:With the fix:
4 passed(the contract test ran against openai-agents 0.25.0). Forcing the helper toreturn Truefails the first test, and forcing it toreturn Falsefails the second.Against the real SDK, not the fake: instrumenting the real
agents.Agentwith an in-memory exporter and constructingAgent(name=..., instructions="secret refund limit 500")gavesystem_instructions=Nonewith the variablefalse, and the formatted instructions with it unset ortrue. On the original code the secret is recorded in both cases.The helper matched
RunConfig()for every value tried (true,1,YES,false,0,off, unset) on openai-agents 0.0.3, 0.1.0, 0.2.11, 0.3.3, 0.4.2, 0.6.1 and 0.25.0. Pylint is 10.00/10 on both files.Not covered: only the SDK's global default is honoured (see above), and the TypeScript Agents instrumentation was not touched.
Checklist
If your change doesn't seem to apply, please leave them unchecked.
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.
Summary by Sourcery
Honor the Agents SDK's global sensitive-data setting when capturing instructions in OpenAI Agents creation spans.
Bug Fixes:
create_agentspans from recording agent instructions when the Agents SDK disables sensitive trace data globally.Enhancements:
RunConfigdefault to determine whether system instructions may be captured while preserving existing content-capture behavior.Documentation:
create_agentspans, including the limitation of per-runRunConfigoverrides.Tests: