Skip to content

Commit

Permalink
Ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Sep 24, 2024
1 parent fce19e5 commit 8dc87a8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
18 changes: 9 additions & 9 deletions python/langsmith/run_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class RunTree(ls_schemas.RunBase):
events: List[Dict] = Field(default_factory=list)
"""List of events associated with the run, like
start and end events."""
_client: Optional[Client] = Field(default=None, exclude=True)
ls_client: Optional[Any] = Field(default=None, exclude=True)
dotted_order: str = Field(
default="", description="The order of the run in the tree."
)
Expand All @@ -74,7 +74,7 @@ class Config:

arbitrary_types_allowed = True
allow_population_by_field_name = True
extra = "allow"
extra = "ignore"

@root_validator(pre=True)
def infer_defaults(cls, values: dict) -> dict:
Expand All @@ -87,9 +87,9 @@ def infer_defaults(cls, values: dict) -> dict:
if values.get("name") is None:
values["name"] = "Unnamed"
if "client" in values: # Handle user-constructed clients
values["_client"] = values["client"]
if not values.get("_client"):
values["_client"] = None
values["ls_client"] = values.pop("client")
if not values.get("ls_client"):
values["ls_client"] = None
if values.get("parent_run") is not None:
values["parent_run_id"] = values["parent_run"].id
if "id" not in values:
Expand Down Expand Up @@ -130,9 +130,9 @@ def client(self) -> Client:
"""Return the client."""
# Lazily load the client
# If you never use this for API calls, it will never be loaded
if not self._client:
self._client = get_cached_client()
return self._client
if not isinstance(self.ls_client, Client):
self.ls_client = get_cached_client()
return self.ls_client

def add_tags(self, tags: Union[Sequence[str], str]) -> None:
"""Add tags to the run."""
Expand Down Expand Up @@ -250,7 +250,7 @@ def create_child(
extra=extra or {},
parent_run=self,
project_name=self.session_name,
_client=self._client,
ls_client=self.ls_client,
tags=tags,
)
self.child_runs.append(run)
Expand Down
26 changes: 26 additions & 0 deletions python/tests/unit_tests/test_run_trees.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from unittest.mock import MagicMock
Expand All @@ -22,7 +23,32 @@ def test_run_tree_accepts_tpe() -> None:

def test_lazy_rt() -> None:
run_tree = RunTree(name="foo")
assert run_tree.ls_client is None
assert isinstance(run_tree.client, Client)
client = Client()
assert RunTree(name="foo", client=client).client == client
assert RunTree(name="foo", ls_client=client).client == client


def test_json_serializable():
run_tree = RunTree(name="foo")
d = run_tree.dict()
assert not d.get("client") and not d.get("ls_client")
assert isinstance(run_tree.client, Client)
d = run_tree.dict()
assert not d.get("client") and not d.get("ls_client")
d = json.loads(run_tree.json())
assert not d.get("client") and not d.get("ls_client")
run_tree = RunTree(name="foo", ls_client=Client())
d = run_tree.dict()
assert not d.get("client") and not d.get("ls_client")
d = json.loads(run_tree.json())
assert not d.get("client") and not d.get("ls_client")
run_tree = RunTree(name="foo", client=Client())
d = run_tree.dict()
assert not d.get("client") and not d.get("ls_client")
d = json.loads(run_tree.json())
assert not d.get("client") and not d.get("ls_client")


@pytest.mark.parametrize(
Expand Down

0 comments on commit 8dc87a8

Please sign in to comment.