Skip to content

Commit 35ae5ea

Browse files
authored
core: use run tree post/patch (#31500)
Use run post/patch
1 parent 73655b0 commit 35ae5ea

File tree

7 files changed

+1632
-1655
lines changed

7 files changed

+1632
-1655
lines changed

libs/core/langchain_core/tracers/langchain.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import logging
6-
import warnings
76
from concurrent.futures import ThreadPoolExecutor
87
from datetime import datetime, timezone
98
from typing import TYPE_CHECKING, Any, Optional, Union
@@ -12,7 +11,6 @@
1211
from langsmith import Client
1312
from langsmith import run_trees as rt
1413
from langsmith import utils as ls_utils
15-
from pydantic import PydanticDeprecationWarning
1614
from tenacity import (
1715
Retrying,
1816
retry_if_exception_type,
@@ -67,21 +65,6 @@ def _get_executor() -> ThreadPoolExecutor:
6765
return _EXECUTOR
6866

6967

70-
def _run_to_dict(run: Run, *, exclude_inputs: bool = False) -> dict:
71-
# TODO: Update once langsmith moves to Pydantic V2 and we can swap run.dict for
72-
# run.model_dump
73-
with warnings.catch_warnings():
74-
warnings.simplefilter("ignore", category=PydanticDeprecationWarning)
75-
76-
res = {
77-
**run.dict(exclude={"child_runs", "inputs", "outputs"}),
78-
"outputs": run.outputs,
79-
}
80-
if not exclude_inputs:
81-
res["inputs"] = run.inputs
82-
return res
83-
84-
8568
class LangChainTracer(BaseTracer):
8669
"""Implementation of the SharedTracer that POSTS to the LangChain endpoint."""
8770

@@ -218,18 +201,11 @@ def _get_tags(self, run: Run) -> list[str]:
218201
def _persist_run_single(self, run: Run) -> None:
219202
"""Persist a run."""
220203
try:
221-
run_dict = _run_to_dict(run)
222-
run_dict["tags"] = self._get_tags(run)
223-
extra = run_dict.get("extra", {})
224-
extra["runtime"] = get_runtime_environment()
225-
run_dict["extra"] = extra
226-
inputs_ = run_dict.get("inputs")
227-
if inputs_ and (len(inputs_) > 1 or bool(next(iter(inputs_.values())))):
228-
inputs_is_truthy = True
229-
else:
230-
inputs_is_truthy = False
231-
run.extra["inputs_is_truthy"] = inputs_is_truthy
232-
self.client.create_run(**run_dict, project_name=self.project_name)
204+
run.extra["runtime"] = get_runtime_environment()
205+
run.tags = self._get_tags(run)
206+
if run.ls_client is not self.client:
207+
run.ls_client = self.client
208+
run.post()
233209
except Exception as e:
234210
# Errors are swallowed by the thread executor so we need to log them here
235211
log_error_once("post", e)
@@ -238,10 +214,7 @@ def _persist_run_single(self, run: Run) -> None:
238214
def _update_run_single(self, run: Run) -> None:
239215
"""Update a run."""
240216
try:
241-
exclude_inputs = run.extra.get("inputs_is_truthy", False)
242-
run_dict = _run_to_dict(run, exclude_inputs=exclude_inputs)
243-
run_dict["tags"] = self._get_tags(run)
244-
self.client.update_run(run.id, **run_dict)
217+
run.patch(exclude_inputs=run.extra.get("inputs_is_truthy", False))
245218
except Exception as e:
246219
# Errors are swallowed by the thread executor so we need to log them here
247220
log_error_once("patch", e)

libs/core/langchain_core/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""langchain-core version information and utilities."""
22

3-
VERSION = "0.3.63"
3+
VERSION = "0.3.64"

libs/core/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = []
77
license = {text = "MIT"}
88
requires-python = ">=3.9"
99
dependencies = [
10-
"langsmith<0.4,>=0.1.126",
10+
"langsmith<0.4,>=0.3.45",
1111
"tenacity!=8.4.0,<10.0.0,>=8.1.0",
1212
"jsonpatch<2.0,>=1.33",
1313
"PyYAML>=5.3",
@@ -16,7 +16,7 @@ dependencies = [
1616
"pydantic>=2.7.4",
1717
]
1818
name = "langchain-core"
19-
version = "0.3.63"
19+
version = "0.3.64"
2020
description = "Building applications with LLMs through composability"
2121
readme = "README.md"
2222

libs/core/tests/unit_tests/document_loaders/test_langsmith.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def test_lazy_load() -> None:
5353
}
5454
expected.append(
5555
Document(example.inputs["first"]["second"].upper(), metadata=metadata)
56+
if example.inputs
57+
else None
5658
)
5759
actual = list(loader.lazy_load())
5860
assert expected == actual

libs/core/tests/unit_tests/runnables/test_tracing_interops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def my_parent_function(a: int) -> int:
130130
parent_run_id = None
131131
for name in ordered_names:
132132
id_ = name_to_body[name]["id"]
133-
parent_run_id_ = name_to_body[name]["parent_run_id"]
133+
parent_run_id_ = name_to_body[name].get("parent_run_id")
134134
if parent_run_id_ is not None:
135135
assert parent_run_id == parent_run_id_
136136
assert name in name_to_body
@@ -199,7 +199,7 @@ async def my_parent_function(a: int) -> int:
199199
parent_run_id = None
200200
for name in ordered_names:
201201
id_ = name_to_body[name]["id"]
202-
parent_run_id_ = name_to_body[name]["parent_run_id"]
202+
parent_run_id_ = name_to_body[name].get("parent_run_id")
203203
if parent_run_id_ is not None:
204204
assert parent_run_id == parent_run_id_
205205
assert name in name_to_body

libs/core/tests/unit_tests/tracers/test_langchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_correct_get_tracer_project(self) -> None:
139139
projects = []
140140

141141
def mock_create_run(**kwargs: Any) -> Any:
142-
projects.append(kwargs.get("project_name")) # noqa: B023
142+
projects.append(kwargs.get("session_name")) # noqa: B023
143143
return unittest.mock.MagicMock()
144144

145145
client.create_run = mock_create_run

0 commit comments

Comments
 (0)