Skip to content

Commit

Permalink
[Tracer] add project name to run from tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Sep 20, 2024
1 parent a8b2413 commit 949a47b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 8 additions & 0 deletions libs/core/langchain_core/tracers/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def __init__(
self.latest_run: Optional[Run] = None

def _start_trace(self, run: Run) -> None:
if self.project_name:
run.session_name = self.project_name
if self.tags is not None:
if run.tags:
run.tags = sorted(set(run.tags + self.tags))
else:
run.tags = self.tags.copy()

super()._start_trace(run)
if run._client is None:
run._client = self.client
Expand Down
19 changes: 17 additions & 2 deletions libs/core/tests/unit_tests/runnables/test_tracing_interops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from unittest.mock import MagicMock, patch

import pytest
from langsmith import Client, traceable
from langsmith import Client, get_current_run_tree, traceable
from langsmith.run_helpers import tracing_context
from langsmith.run_trees import RunTree
from langsmith.utils import get_env_var
Expand Down Expand Up @@ -40,10 +40,15 @@ def test_config_traceable_handoff() -> None:
mock_client_ = Client(
session=mock_session, api_key="test", auto_batch_tracing=False
)
tracer = LangChainTracer(client=mock_client_)
tracer = LangChainTracer(
client=mock_client_, project_name="another-flippin-project", tags=["such-a-tag"]
)

@traceable
def my_great_great_grandchild_function(a: int) -> int:
rt = get_current_run_tree()
assert rt
assert rt.session_name == "another-flippin-project"
return a + 1

@RunnableLambda
Expand All @@ -60,19 +65,28 @@ def my_child_function(a: int) -> int:

@traceable()
def my_function(a: int) -> int:
rt = get_current_run_tree()
assert rt
assert rt.session_name == "another-flippin-project"
assert rt.parent_run and rt.parent_run.name == "my_parent_function"
return my_child_function(a)

def my_parent_function(a: int) -> int:
rt = get_current_run_tree()
assert rt
assert rt.session_name == "another-flippin-project"
return my_function(a)

my_parent_runnable = RunnableLambda(my_parent_function)

assert my_parent_runnable.invoke(1, {"callbacks": [tracer]}) == 6
posts = _get_posts(mock_client_)
assert all(post["session_name"] == "another-flippin-project" for post in posts)
# There should have been 6 runs created,
# one for each function invocation
assert len(posts) == 6
name_to_body = {post["name"]: post for post in posts}

ordered_names = [
"my_parent_function",
"my_function",
Expand Down Expand Up @@ -102,6 +116,7 @@ def my_parent_function(a: int) -> int:
)
last_dotted_order = dotted_order
parent_run_id = id_
assert "such-a-tag" in name_to_body["my_parent_function"]["tags"]


@pytest.mark.skipif(
Expand Down

0 comments on commit 949a47b

Please sign in to comment.