From 9b6ac41442ca557c55b10de63fd0673dccb6a249 Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:33:12 -0700 Subject: [PATCH] [Core] Inherit tracing metadata & tags (#26838) --- libs/core/langchain_core/callbacks/manager.py | 16 +++++++++++++--- .../runnables/test_tracing_interops.py | 11 ++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/libs/core/langchain_core/callbacks/manager.py b/libs/core/langchain_core/callbacks/manager.py index d8685e51d0219..1f1c2cd8f44a3 100644 --- a/libs/core/langchain_core/callbacks/manager.py +++ b/libs/core/langchain_core/callbacks/manager.py @@ -20,7 +20,7 @@ ) from uuid import UUID -from langsmith.run_helpers import get_run_tree_context +from langsmith.run_helpers import get_tracing_context from tenacity import RetryCallState from langchain_core.callbacks.base import ( @@ -2232,9 +2232,15 @@ def _configure( tracing_v2_callback_var, ) - run_tree = get_run_tree_context() + tracing_context = get_tracing_context() + tracing_metadata = tracing_context["metadata"] + tracing_tags = tracing_context["tags"] + run_tree: Optional[Run] = tracing_context["parent"] parent_run_id = None if run_tree is None else run_tree.id - callback_manager = callback_manager_cls(handlers=[], parent_run_id=parent_run_id) + callback_manager = callback_manager_cls( + handlers=[], + parent_run_id=parent_run_id, + ) if inheritable_callbacks or local_callbacks: if isinstance(inheritable_callbacks, list) or inheritable_callbacks is None: inheritable_callbacks_ = inheritable_callbacks or [] @@ -2278,6 +2284,10 @@ def _configure( if inheritable_metadata or local_metadata: callback_manager.add_metadata(inheritable_metadata or {}) callback_manager.add_metadata(local_metadata or {}, False) + if tracing_metadata: + callback_manager.add_metadata(tracing_metadata.copy()) + if tracing_tags: + callback_manager.add_tags(tracing_tags.copy()) v1_tracing_enabled_ = env_var_is_set("LANGCHAIN_TRACING") or env_var_is_set( "LANGCHAIN_HANDLER" 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 1dcd85c2fdff7..b6318ef5b3bbb 100644 --- a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py +++ b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py @@ -390,7 +390,12 @@ def collect_run(run: RunTree) -> None: rid = uuid.uuid4() - with tracing_context(client=mock_client_, enabled=True): + with tracing_context( + client=mock_client_, + enabled=True, + metadata={"some_foo": "some_bar"}, + tags=["afoo"], + ): assert parent(langsmith_extra={"on_end": collect_run, "run_id": rid}) == "foo" assert collected @@ -404,6 +409,10 @@ def collect_run(run: RunTree) -> None: grandchild_run = child_run.child_runs[0] assert grandchild_run.name == "grandchild" assert grandchild_run.child_runs + assert grandchild_run.metadata.get("some_foo") == "some_bar" + assert "afoo" in grandchild_run.tags # type: ignore kitten_run = grandchild_run.child_runs[0] assert kitten_run.name == "kitten" assert not kitten_run.child_runs + assert kitten_run.metadata.get("some_foo") == "some_bar" + assert "afoo" in kitten_run.tags # type: ignore