Drop duplicate inputs (#29589)

This commit is contained in:
William FH
2025-02-04 18:06:10 -08:00
committed by GitHub
parent 65f0deb81a
commit 5ae4ed791d

View File

@@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import copy
import logging import logging
import warnings import warnings
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
@@ -68,17 +67,19 @@ def _get_executor() -> ThreadPoolExecutor:
return _EXECUTOR return _EXECUTOR
def _run_to_dict(run: Run) -> dict: def _run_to_dict(run: Run, exclude_inputs: bool = False) -> dict:
# TODO: Update once langsmith moves to Pydantic V2 and we can swap run.dict for # TODO: Update once langsmith moves to Pydantic V2 and we can swap run.dict for
# run.model_dump # run.model_dump
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore", category=PydanticDeprecationWarning) warnings.simplefilter("ignore", category=PydanticDeprecationWarning)
return { res = {
**run.dict(exclude={"child_runs", "inputs", "outputs"}), **run.dict(exclude={"child_runs", "inputs", "outputs"}),
"inputs": run.inputs.copy() if run.inputs is not None else None, "outputs": run.outputs,
"outputs": run.outputs.copy() if run.outputs is not None else None,
} }
if not exclude_inputs:
res["inputs"] = run.inputs
return res
class LangChainTracer(BaseTracer): class LangChainTracer(BaseTracer):
@@ -172,13 +173,7 @@ class LangChainTracer(BaseTracer):
return chat_model_run return chat_model_run
def _persist_run(self, run: Run) -> None: def _persist_run(self, run: Run) -> None:
# TODO: Update once langsmith moves to Pydantic V2 and we can swap run.copy for self.latest_run = run
# run.model_copy
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=PydanticDeprecationWarning)
run_ = copy.copy(run)
run_.reference_example_id = self.example_id
self.latest_run = run_
def get_run_url(self) -> str: def get_run_url(self) -> str:
"""Get the LangSmith root run URL. """Get the LangSmith root run URL.
@@ -216,12 +211,14 @@ class LangChainTracer(BaseTracer):
def _persist_run_single(self, run: Run) -> None: def _persist_run_single(self, run: Run) -> None:
"""Persist a run.""" """Persist a run."""
run_dict = _run_to_dict(run)
run_dict["tags"] = self._get_tags(run)
extra = run_dict.get("extra", {})
extra["runtime"] = get_runtime_environment()
run_dict["extra"] = extra
try: try:
run_dict = _run_to_dict(run)
run_dict["tags"] = self._get_tags(run)
extra = run_dict.get("extra", {})
extra["runtime"] = get_runtime_environment()
run_dict["extra"] = extra
inputs_is_truthy = bool(run_dict.get("inputs"))
run.extra["inputs_is_truthy"] = inputs_is_truthy
self.client.create_run(**run_dict, project_name=self.project_name) self.client.create_run(**run_dict, project_name=self.project_name)
except Exception as e: except Exception as e:
# Errors are swallowed by the thread executor so we need to log them here # Errors are swallowed by the thread executor so we need to log them here
@@ -231,7 +228,8 @@ class LangChainTracer(BaseTracer):
def _update_run_single(self, run: Run) -> None: def _update_run_single(self, run: Run) -> None:
"""Update a run.""" """Update a run."""
try: try:
run_dict = _run_to_dict(run) exclude_inputs = run.extra.get("inputs_is_truthy", False)
run_dict = _run_to_dict(run, exclude_inputs=exclude_inputs)
run_dict["tags"] = self._get_tags(run) run_dict["tags"] = self._get_tags(run)
self.client.update_run(run.id, **run_dict) self.client.update_run(run.id, **run_dict)
except Exception as e: except Exception as e: