Dereference run tree (#30377)

This commit is contained in:
William FH 2025-03-19 12:05:06 -07:00 committed by GitHub
parent 8265be4d3e
commit ce84f8ba7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View File

@ -173,7 +173,13 @@ class LangChainTracer(BaseTracer):
return chat_model_run
def _persist_run(self, run: Run) -> None:
self.latest_run = run
# We want to free up more memory by avoiding keeping a reference to the
# whole nested run tree.
self.latest_run = Run.construct(
**run.dict(exclude={"child_runs", "inputs", "outputs"}),
inputs=run.inputs,
outputs=run.outputs,
)
def get_run_url(self) -> str:
"""Get the LangSmith root run URL.

View File

@ -445,12 +445,13 @@ def test_tree_is_constructed(parent_type: Literal["ls", "lc"]) -> None:
metadata={"some_foo": "some_bar"},
tags=["afoo"],
):
if parent_type == "ls":
collected: dict[str, RunTree] = {} # noqa
def collect_run(run: RunTree) -> None:
collected[str(run.id)] = run
if parent_type == "ls":
@traceable
def parent() -> str:
return child.invoke("foo")
@ -459,7 +460,6 @@ def test_tree_is_constructed(parent_type: Literal["ls", "lc"]) -> None:
parent(langsmith_extra={"on_end": collect_run, "run_id": rid}) == "foo"
)
assert collected
run = collected.get(str(rid))
else:
@ -468,8 +468,10 @@ def test_tree_is_constructed(parent_type: Literal["ls", "lc"]) -> None:
return child.invoke("foo")
tracer = LangChainTracer()
tracer._persist_run = collect_run # type: ignore
assert parent.invoke(..., {"run_id": rid, "callbacks": [tracer]}) == "foo" # type: ignore
run = tracer.latest_run
run = collected.get(str(rid))
assert run is not None
assert run.name == "parent"