The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
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.pyindex 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, timezonefrom typing import TYPE_CHECKING, Any, Optional, Unionfrom uuid import UUID-from langsmith import Client+from langsmith import Client, get_tracing_contextfrom langsmith import run_trees as rtfrom langsmith import utils as ls_utilsfrom 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"] = Truedef 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"):+ returntry: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"):+ returntry: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.pyindex 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().hexproject_name = f"Some project {name}"with tracing_context(project_name=project_name, client=mock_client_, enabled=True):assert my_function.invoke(1) == 2posts = _get_posts(mock_client_)- assert posts+ assert len(posts) == 1assert all(post["session_name"] == project_name for post in posts)