Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tracer] add project name to run from tracer #26736

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading