AI-183: Respect LANGSMITH_TRACING env var in LangSmith plugin#1509
Open
AI-183: Respect LANGSMITH_TRACING env var in LangSmith plugin#1509
Conversation
LangSmithInterceptor previously hard-coded `enabled=True` in every `tracing_context(...)` call (4 sites: workflow, activity, nexus start, nexus cancel) and `_maybe_run` gated only on `add_temporal_runs`. This meant `LANGSMITH_TRACING=false` was a no-op when the plugin was installed. Defer to langsmith's native polarity: drop `enabled` from all four `tracing_context(...)` calls so langsmith's own `tracing_is_enabled()` resolution takes over, and short-circuit `_maybe_run` on `tracing_is_enabled()` in addition to the existing `add_temporal_runs` gate. Behavior change: the plugin is no longer "on by default" once installed. Users must now explicitly opt in via `LANGSMITH_TRACING=true`, matching langsmith's standard mechanism. Cross-process: `tracing_is_enabled()` checks for an active parent run tree before consulting env vars, so an inbound parent trace propagated via headers will continue locally even when `LANGSMITH_TRACING=false` (documented in `_maybe_run`'s docstring). Adds 5 tests in tests/contrib/langsmith/test_tracing_env_override.py covering both `add_temporal_runs` modes, the legacy `LANGCHAIN_TRACING_V2` env var, the Nexus start path, and a positive control. Adds two autouse fixtures in tests/contrib/langsmith/conftest.py: one to clear langsmith's env-var lru_cache between tests, and one to set `LANGSMITH_TRACING=true` by default for the suite (preserving the prior "tracing on" assumption that the existing 87 tests rely on; individual tests override). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The Note paragraph was sandwiched between two bullets, breaking RST definition list parsing. Pydoctor reported "Definition list ends without a blank line; unexpected unindent." Move the Note after all bullets, as a standalone paragraph before ``Args:``. No content change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
brianstrauch
approved these changes
May 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix AI-183:
LangSmithPluginwas force-enabling LangSmith tracing inside every workflow execution, overriding the user'sLANGSMITH_TRACING=falseenv var.Two bug surfaces in
temporalio/contrib/langsmith/_interceptor.py:tracing_context(...)call sites (workflow, activity, nexus start, nexus cancel) hard-codedenabled=True, short-circuitingtracing_is_enabled()because the langsmith library checks the context dict before env vars._maybe_rungated Temporal-level runs only onadd_temporal_runs. Whenadd_temporal_runs=True, runs were created regardless of any disable mechanism.The fix defers to langsmith's native polarity: drop the
enabledkwarg entirely so langsmith's own resolution takes over, and gate_maybe_runonlangsmith.utils.tracing_is_enabled().The plugin no longer treats "installed = on by default." Users must now opt in via
LANGSMITH_TRACING=true(or set the context appropriately), matching langsmith's standard mechanism. Users who previously relied on installing the plugin without setting any env var will need to setLANGSMITH_TRACING=trueto keep getting traces.Cross-process trace continuation
tracing_is_enabled()checks for an active parent run tree before consulting the env var. So a worker withLANGSMITH_TRACING=falsewill still continue an inbound parent trace propagated via headers from an upstream worker that had tracing enabled. This matches langsmith's "continue mid-trace" model —LANGSMITH_TRACING=falsesuppresses new traces locally but doesn't break a parent trace flowing through. Documented in_maybe_run's docstring.Tests
5 new tests in
tests/contrib/langsmith/test_tracing_env_override.py:add_temporal_runs=True+LANGSMITH_TRACING=false→ no runsadd_temporal_runs=False+LANGSMITH_TRACING=false→ no runsLANGCHAIN_TRACING_V2=false→ no runsLANGSMITH_TRACING=false→ no runsLANGSMITH_TRACING=true→ runs ARE emittedTwo new autouse fixtures in
tests/contrib/langsmith/conftest.py:_clear_langsmith_env_cache— clears langsmith'sget_env_varlru_cache between tests so env-var changes don't leak across tests._enable_langsmith_tracing— setsLANGSMITH_TRACING=trueby default for all langsmith tests, preserving the prior "tracing on" assumption that 87 existing tests rely on. Individual tests override withmonkeypatch.setenv(..., "false")as needed.The Nexus cancel path is not directly tested — it would require ~50+ lines of new scaffolding for in-flight cancel orchestration. Code path is identical to Nexus start; worth a follow-up ticket.
Test plan
uv run pytest tests/contrib/langsmith/test_tracing_env_override.py -v— 5/5 passuv run pytest tests/contrib/langsmith/ -v— full suite, 87/87 passuv run pytest tests/contrib/ -v— broader contrib, 306 pass / 16 skip / 0 fail (3 unrelated collection errors from missing optional deps)