Skip to content

Commit

Permalink
[Core] Inherit tracing metadata & tags (#26838)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Sep 25, 2024
1 parent 3796e14 commit 9b6ac41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 13 additions & 3 deletions libs/core/langchain_core/callbacks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 []
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion libs/core/tests/unit_tests/runnables/test_tracing_interops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 9b6ac41

Please sign in to comment.