Skip to content

feat(sdk): expose trace_content via Traceloop.init()#4195

Open
yoheidemachi wants to merge 3 commits into
traceloop:mainfrom
yoheidemachi:feat/trace-content-init-arg
Open

feat(sdk): expose trace_content via Traceloop.init()#4195
yoheidemachi wants to merge 3 commits into
traceloop:mainfrom
yoheidemachi:feat/trace-content-init-arg

Conversation

@yoheidemachi
Copy link
Copy Markdown

@yoheidemachi yoheidemachi commented May 27, 2026

Summary

Adds a trace_content: Optional[bool] = None argument to Traceloop.init() that mirrors the existing TRACELOOP_TRACE_CONTENT environment variable, addressing #137 (open since 2023).

  • trace_content=None (default): the TRACELOOP_TRACE_CONTENT env var stays authoritative — no behavior change for existing deployments.
  • trace_content=True / False: the argument explicitly overrides the env var so applications can toggle prompt/completion capture without relying on process-level configuration.

The implementation sets os.environ["TRACELOOP_TRACE_CONTENT"] before any instrumentor initializes, so every bundled instrumentation that reads the env var (bedrock, watsonx, alephalpha, writer, …) picks up the value transparently without further changes.

Design notes

  • An explicit arg wins over the env var (consistent with use_attributes / use_legacy_attributes, which also use Optional[bool] = None to signal "not explicitly set").

Behavior to be aware of

  • Process-wide side effect. Because the implementation writes os.environ["TRACELOOP_TRACE_CONTENT"], child processes that inherit the env and any other code reading that variable will see the override too. This is the same surface area the existing env var already has — the arg just adds a programmatic entry point.
  • The override is sticky. Once trace_content=True/False has been passed, a later Traceloop.init(trace_content=None) will not restore the previous env-driven value — the most recent explicit setting keeps winning until the env var is changed externally. Fine for the typical "init once at startup" flow; documented in the docstring.
  • Per-span override still works. Setting trace_content=False does not block the existing override_enable_content_tracing OTel context value — instrumentations treat env and context as OR, so individual spans can opt back in while the global default stays off. This preserves the current semantics intentionally.

Test plan

  • test_trace_content_false_disables_content_tracing — explicit False overrides env=true
  • test_trace_content_true_overrides_env — explicit True overrides env=false
  • test_trace_content_none_honors_env — omitting the arg preserves env-driven behavior
  • Existing test_sdk_initialization.py + test_privacy_no_prompts.py (32 tests) still pass
  • nx run traceloop-sdk:lint passes

Closes #137

Summary by CodeRabbit

Release Notes

  • New Features
    • Introduced trace_content parameter to SDK initialization for controlling sensitive data capture in distributed traces. Once configured, the setting persists across subsequent initialization calls, ensuring consistent tracing behavior throughout the application lifecycle. Individual spans can still override the global configuration as needed.

Review Change Stack

Adds a `trace_content: Optional[bool] = None` argument to
`Traceloop.init()` that mirrors the `TRACELOOP_TRACE_CONTENT`
environment variable. When omitted, the env var stays authoritative;
when set to True/False, the argument overrides the env var so that
applications can disable prompt/completion capture without relying on
process-level environment configuration.

Closes traceloop#137
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 27, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 70fd9d8a-16a1-4c5c-9b96-15f821c699a6

📥 Commits

Reviewing files that changed from the base of the PR and between b39151b and 376f59e.

📒 Files selected for processing (2)
  • packages/traceloop-sdk/tests/test_sdk_initialization.py
  • packages/traceloop-sdk/traceloop/sdk/__init__.py

📝 Walkthrough

Walkthrough

This PR introduces a new trace_content optional parameter to Traceloop.init() that allows callers to control content tracing (prompts, completions) without relying solely on the TRACELOOP_TRACE_CONTENT environment variable. When provided, the parameter sets the env var for the process lifetime with sticky behavior across subsequent init() calls.

Changes

trace_content Feature

Layer / File(s) Summary
API parameter and documentation
packages/traceloop-sdk/traceloop/sdk/__init__.py
Adds trace_content: Optional[bool] = None to Traceloop.init() and documents how it overrides TRACELOOP_TRACE_CONTENT, its sticky behavior, per-span context interaction, and that it does not apply in early no-op paths.
Environment variable override logic
packages/traceloop-sdk/traceloop/sdk/__init__.py
Sets os.environ["TRACELOOP_TRACE_CONTENT"] to "true" or "false" when trace_content is explicitly provided, before computing enable_content_tracing.
Test fixture and validation cases
packages/traceloop-sdk/tests/test_sdk_initialization.py
Adds os import, isolated_trace_content_env fixture for safe env var manipulation, and four tests validating: explicit False disables content tracing, explicit True overrides disabled env values, omitted parameter honors env state, and sticky override behavior across init() calls.

🎯 2 (Simple) | ⏱️ ~12 minutes

🐰 A trace_content flag hops in with style,
No more prompts hiding all the while!
Environment vars now bow and bend,
To parameters users send—control to the end! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: exposing trace_content parameter via Traceloop.init() method.
Linked Issues check ✅ Passed The pull request fully implements the feature request from #137: adds trace_content parameter to Traceloop.init() with proper override behavior for TRACELOOP_TRACE_CONTENT env var.
Out of Scope Changes check ✅ Passed All changes are within scope: the new parameter in Traceloop.init(), supporting tests, and documentation align directly with the objectives of issue #137.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Document the three non-obvious aspects of trace_content surfaced in
review: (1) writing TRACELOOP_TRACE_CONTENT is process-wide and
inherited by subprocesses, (2) the override is sticky across subsequent
init() calls with trace_content=None, (3) per-span override via
override_enable_content_tracing still works because instrumentations
treat env and context as OR. Adds a test pinning (2) so a future
save/restore refactor would force a docs update.

Assisted-by: Claude Opus 4.7
…test fixture

Address self-review nits on the trace_content init arg:

* Docstring: clarify that the env override is not applied when
  enabled=False or tracing is disabled via TRACELOOP_TRACING_ENABLED,
  since init() returns early before the write would happen. Pins the
  documented surface to the actual code path.
* Test fixture: clear TRACELOOP_TRACE_CONTENT before yielding (not just
  on teardown) so trace_content tests start from a known-unset state. A
  CI env that pre-sets the var would otherwise mask bugs in tests that
  exercise the "env not set" default path.

No behavior change.

Assisted-by: Claude Opus 4.7
@yoheidemachi yoheidemachi marked this pull request as ready for review May 27, 2026 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀 Feature: allow disabling prompt sending as an argument to Traceloop.init()

2 participants