Skip to content

The agent engineering platform.

fix(core): honor `enabled=false` in nested tracing (#31986)

William FHf1d44d0f9dea
diff --git a/libs/core/langchain_core/tracers/langchain.py b/libs/core/langchain_core/tracers/langchain.py
index 07e6618573..431c4564a8 100644
--- a/libs/core/langchain_core/tracers/langchain.py
+++ b/libs/core/langchain_core/tracers/langchain.py
@@ -8,7 +8,7 @@ from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, Optional, Union
from uuid import UUID
-from langsmith import Client
+from langsmith import Client, get_tracing_context
from langsmith import run_trees as rt
from langsmith import utils as ls_utils
from tenacity import (
@@ -113,6 +113,8 @@ class LangChainTracer(BaseTracer):
super()._start_trace(run)
if run.ls_client is None:
run.ls_client = self.client
+ if get_tracing_context().get("enabled") is False:
+ run.extra["__disabled"] = True
def on_chat_model_start(
self,
@@ -205,6 +207,8 @@ class LangChainTracer(BaseTracer):
def _persist_run_single(self, run: Run) -> None:
"""Persist a run."""
+ if run.extra.get("__disabled"):
+ return
try:
run.extra["runtime"] = get_runtime_environment()
run.tags = self._get_tags(run)
@@ -218,6 +222,8 @@ class LangChainTracer(BaseTracer):
def _update_run_single(self, run: Run) -> None:
"""Update a run."""
+ if run.extra.get("__disabled"):
+ return
try:
run.patch(exclude_inputs=run.extra.get("inputs_is_truthy", False))
except Exception as e:
diff --git a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py
index 2db895355e..639ff799f5 100644
--- a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py
+++ b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py
@@ -57,15 +57,20 @@ def test_tracing_context() -> None:
)
@RunnableLambda
- def my_function(a: int) -> int:
+ def my_lambda(a: int) -> int:
return a + 1
+ @RunnableLambda
+ def my_function(a: int) -> int:
+ with tracing_context(enabled=False):
+ return my_lambda.invoke(a)
+
name = uuid.uuid4().hex
project_name = f"Some project {name}"
with tracing_context(project_name=project_name, client=mock_client_, enabled=True):
assert my_function.invoke(1) == 2
posts = _get_posts(mock_client_)
- assert posts
+ assert len(posts) == 1
assert all(post["session_name"] == project_name for post in posts)