From 0da96148dd5265a9aae77312c83f015a834b55ca Mon Sep 17 00:00:00 2001 From: s mz <69295615+smz202000@users.noreply.github.com> Date: Fri, 17 Jul 2026 15:28:12 +0800 Subject: [PATCH] feat(traceloop-sdk): add trace_content parameter to Traceloop.init() Add a parameter to that allows users to control whether sensitive content (prompts and completions) is sent in traces. When the parameter is not set (None), the existing environment variable is used (backward compatible). When explicitly set to True or False, it overrides the environment variable. This addresses the feature request in issue #137, giving users more granular control over content tracing directly from the API without relying solely on environment variables. Changes: - Modified in config to accept an optional parameter that overrides the env var - Added parameter to - Added docstring documentation for the new parameter --- packages/traceloop-sdk/traceloop/sdk/__init__.py | 7 ++++++- packages/traceloop-sdk/traceloop/sdk/config/__init__.py | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/traceloop-sdk/traceloop/sdk/__init__.py b/packages/traceloop-sdk/traceloop/sdk/__init__.py index 6663429d99..7f0c1fb0a0 100644 --- a/packages/traceloop-sdk/traceloop/sdk/__init__.py +++ b/packages/traceloop-sdk/traceloop/sdk/__init__.py @@ -73,6 +73,7 @@ def init( endpoint_is_traceloop: Optional[bool] = False, use_attributes: Optional[bool] = None, use_legacy_attributes: Optional[bool] = None, + trace_content: Optional[bool] = None, ) -> Optional[Client]: """Initialize Traceloop tracing, metrics, and instrumentation. @@ -88,6 +89,10 @@ def init( events have nowhere to go and no prompt/completion data will be recorded. use_legacy_attributes: Deprecated alias for ``use_attributes``. Will be removed in a future release. + trace_content: Whether to trace prompt/completion content. If not set, + uses the ``TRACELOOP_TRACE_CONTENT`` environment variable + (defaults to ``True``). Set to ``False`` to disable sending + sensitive content in traces. """ if use_attributes is not None and use_legacy_attributes is not None: raise TypeError( @@ -125,7 +130,7 @@ def init( print(Fore.YELLOW + "Tracing is disabled" + Fore.RESET) return - enable_content_tracing = is_content_tracing_enabled() + enable_content_tracing = is_content_tracing_enabled(trace_content) if exporter and processor: warnings.warn( diff --git a/packages/traceloop-sdk/traceloop/sdk/config/__init__.py b/packages/traceloop-sdk/traceloop/sdk/config/__init__.py index 8b57568ff4..204cbf2a7f 100644 --- a/packages/traceloop-sdk/traceloop/sdk/config/__init__.py +++ b/packages/traceloop-sdk/traceloop/sdk/config/__init__.py @@ -1,11 +1,14 @@ import os +from typing import Optional def is_tracing_enabled() -> bool: return (os.getenv("TRACELOOP_TRACING_ENABLED") or "true").lower() == "true" -def is_content_tracing_enabled() -> bool: +def is_content_tracing_enabled(trace_content: Optional[bool] = None) -> bool: + if trace_content is not None: + return trace_content return (os.getenv("TRACELOOP_TRACE_CONTENT") or "true").lower() == "true"