Skip to content

Commit

Permalink
[Core] Add more interops tests (#26841)
Browse files Browse the repository at this point in the history
To test that the client propagates both ways
  • Loading branch information
hinthornw authored Sep 25, 2024
1 parent 9b6ac41 commit 82b5b77
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
6 changes: 5 additions & 1 deletion libs/core/langchain_core/callbacks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,11 @@ def _configure(
try:
handler = LangChainTracer(
project_name=tracer_project,
client=run_tree.client if run_tree is not None else None,
client=(
run_tree.client
if run_tree is not None
else tracing_context["client"]
),
)
callback_manager.add_handler(handler, True)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion libs/core/langchain_core/tracers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _start_trace(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # t
self._add_child_run(parent_run, run)
else:
if self.log_missing_parent:
logger.warning(
logger.debug(
f"Parent run {run.parent_run_id} not found for run {run.id}."
" Treating as a root run."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/core/langchain_core/tracers/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _start_trace(self, run: Run) -> None:

super()._start_trace(run)
if run._client is None:
run._client = self.client
run._client = self.client # type: ignore

def on_chat_model_start(
self,
Expand Down
42 changes: 28 additions & 14 deletions libs/core/tests/unit_tests/runnables/test_tracing_interops.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from langsmith.run_helpers import tracing_context
from langsmith.run_trees import RunTree
from langsmith.utils import get_env_var
from typing_extensions import Literal

from langchain_core.runnables.base import RunnableLambda, RunnableParallel
from langchain_core.tracers.langchain import LangChainTracer
Expand Down Expand Up @@ -361,7 +362,8 @@ def parent(a: int) -> int:
assert dotted_order.split(".")[0] == dotted_order


def test_tree_is_constructed() -> None:
@pytest.mark.parametrize("parent_type", ("ls", "lc"))
def test_tree_is_constructed(parent_type: Literal["ls", "lc"]) -> None:
mock_session = MagicMock()
mock_client_ = Client(
session=mock_session, api_key="test", auto_batch_tracing=False
Expand All @@ -379,27 +381,39 @@ def grandchild(x: str) -> str:
def child(x: str) -> str:
return grandchild.invoke(x)

@traceable
def parent() -> str:
return child.invoke("foo")

collected: dict[str, RunTree] = {} # noqa

def collect_run(run: RunTree) -> None:
collected[str(run.id)] = run

rid = uuid.uuid4()

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"
if parent_type == "ls":
collected: dict[str, RunTree] = {} # noqa

def collect_run(run: RunTree) -> None:
collected[str(run.id)] = run

@traceable
def parent() -> str:
return child.invoke("foo")

assert (
parent(langsmith_extra={"on_end": collect_run, "run_id": rid}) == "foo"
)
assert collected
run = collected.get(str(rid))

else:

@RunnableLambda
def parent(_) -> str: # type: ignore
return child.invoke("foo")

tracer = LangChainTracer()
assert parent.invoke(..., {"run_id": rid, "callbacks": [tracer]}) == "foo" # type: ignore
run = tracer.latest_run

assert collected
run = collected.get(str(rid))
assert run is not None
assert run.name == "parent"
assert run.child_runs
Expand Down
2 changes: 1 addition & 1 deletion libs/core/tests/unit_tests/tracers/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def new_persist_run_single(run: Run) -> None:
def test_tracer_with_run_tree_parent() -> None:
mock_session = unittest.mock.MagicMock()
client = Client(session=mock_session, api_key="test")
parent = RunTree(name="parent", inputs={"input": "foo"}, _client=client)
parent = RunTree(name="parent", inputs={"input": "foo"}, _client=client) # type: ignore
run_id = uuid.uuid4()
tracer = LangChainTracer(client=client)
tracer.order_map[parent.id] = (parent.trace_id, parent.dotted_order)
Expand Down

0 comments on commit 82b5b77

Please sign in to comment.