mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-07 12:06:43 +00:00
[Core] Add more interops tests (#26841)
To test that the client propagates both ways
This commit is contained in:
parent
9b6ac41442
commit
82b5b77940
@ -2333,7 +2333,11 @@ def _configure(
|
|||||||
try:
|
try:
|
||||||
handler = LangChainTracer(
|
handler = LangChainTracer(
|
||||||
project_name=tracer_project,
|
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)
|
callback_manager.add_handler(handler, True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -118,7 +118,7 @@ class _TracerCore(ABC):
|
|||||||
self._add_child_run(parent_run, run)
|
self._add_child_run(parent_run, run)
|
||||||
else:
|
else:
|
||||||
if self.log_missing_parent:
|
if self.log_missing_parent:
|
||||||
logger.warning(
|
logger.debug(
|
||||||
f"Parent run {run.parent_run_id} not found for run {run.id}."
|
f"Parent run {run.parent_run_id} not found for run {run.id}."
|
||||||
" Treating as a root run."
|
" Treating as a root run."
|
||||||
)
|
)
|
||||||
|
@ -123,7 +123,7 @@ class LangChainTracer(BaseTracer):
|
|||||||
|
|
||||||
super()._start_trace(run)
|
super()._start_trace(run)
|
||||||
if run._client is None:
|
if run._client is None:
|
||||||
run._client = self.client
|
run._client = self.client # type: ignore
|
||||||
|
|
||||||
def on_chat_model_start(
|
def on_chat_model_start(
|
||||||
self,
|
self,
|
||||||
|
@ -10,6 +10,7 @@ from langsmith import Client, get_current_run_tree, traceable
|
|||||||
from langsmith.run_helpers import tracing_context
|
from langsmith.run_helpers import tracing_context
|
||||||
from langsmith.run_trees import RunTree
|
from langsmith.run_trees import RunTree
|
||||||
from langsmith.utils import get_env_var
|
from langsmith.utils import get_env_var
|
||||||
|
from typing_extensions import Literal
|
||||||
|
|
||||||
from langchain_core.runnables.base import RunnableLambda, RunnableParallel
|
from langchain_core.runnables.base import RunnableLambda, RunnableParallel
|
||||||
from langchain_core.tracers.langchain import LangChainTracer
|
from langchain_core.tracers.langchain import LangChainTracer
|
||||||
@ -361,7 +362,8 @@ async def test_runnable_sequence_parallel_trace_nesting(method: str) -> None:
|
|||||||
assert dotted_order.split(".")[0] == dotted_order
|
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_session = MagicMock()
|
||||||
mock_client_ = Client(
|
mock_client_ = Client(
|
||||||
session=mock_session, api_key="test", auto_batch_tracing=False
|
session=mock_session, api_key="test", auto_batch_tracing=False
|
||||||
@ -379,27 +381,39 @@ def test_tree_is_constructed() -> None:
|
|||||||
def child(x: str) -> str:
|
def child(x: str) -> str:
|
||||||
return grandchild.invoke(x)
|
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()
|
rid = uuid.uuid4()
|
||||||
|
|
||||||
with tracing_context(
|
with tracing_context(
|
||||||
client=mock_client_,
|
client=mock_client_,
|
||||||
enabled=True,
|
enabled=True,
|
||||||
metadata={"some_foo": "some_bar"},
|
metadata={"some_foo": "some_bar"},
|
||||||
tags=["afoo"],
|
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 is not None
|
||||||
assert run.name == "parent"
|
assert run.name == "parent"
|
||||||
assert run.child_runs
|
assert run.child_runs
|
||||||
|
@ -65,7 +65,7 @@ def test_example_id_assignment_threadsafe() -> None:
|
|||||||
def test_tracer_with_run_tree_parent() -> None:
|
def test_tracer_with_run_tree_parent() -> None:
|
||||||
mock_session = unittest.mock.MagicMock()
|
mock_session = unittest.mock.MagicMock()
|
||||||
client = Client(session=mock_session, api_key="test")
|
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()
|
run_id = uuid.uuid4()
|
||||||
tracer = LangChainTracer(client=client)
|
tracer = LangChainTracer(client=client)
|
||||||
tracer.order_map[parent.id] = (parent.trace_id, parent.dotted_order)
|
tracer.order_map[parent.id] = (parent.trace_id, parent.dotted_order)
|
||||||
|
Loading…
Reference in New Issue
Block a user