diff --git a/python/langsmith/client.py b/python/langsmith/client.py index 70861458d..2ac947d34 100644 --- a/python/langsmith/client.py +++ b/python/langsmith/client.py @@ -887,7 +887,9 @@ def upload_csv( file_name = file_name.split("/")[-1] raise ValueError(f"Dataset {file_name} already exists") return ls_schemas.Dataset( - **result, _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **result, + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) @staticmethod @@ -1758,18 +1760,29 @@ def update_project( ls_utils.raise_for_status_with_text(response) return ls_schemas.TracerSession(**response.json(), _host_url=self._host_url) - def _get_tenant_id(self) -> uuid.UUID: + def _get_optional_tenant_id(self) -> Optional[uuid.UUID]: if self._tenant_id is not None: return self._tenant_id - response = self._get_with_retries("/sessions", params={"limit": 1}) - result = response.json() - if isinstance(result, list): - tracer_session = ls_schemas.TracerSessionResult( - **result[0], _host_url=self._host_url + try: + response = self._get_with_retries("/sessions", params={"limit": 1}) + result = response.json() + if isinstance(result, list) and len(result) > 0: + tracer_session = ls_schemas.TracerSessionResult( + **result[0], _host_url=self._host_url + ) + self._tenant_id = tracer_session.tenant_id + return self._tenant_id + except Exception as e: + logger.warning( + "Failed to get tenant ID from LangSmith: %s", repr(e), exc_info=True ) - self._tenant_id = tracer_session.tenant_id - return self._tenant_id - raise ls_utils.LangSmithError("No projects found") + return None + + def _get_tenant_id(self) -> uuid.UUID: + tenant_id = self._get_optional_tenant_id() + if tenant_id is None: + raise ls_utils.LangSmithError("No tenant ID found") + return tenant_id @ls_utils.xor_args(("project_id", "project_name")) def read_project( @@ -2026,7 +2039,7 @@ def create_dataset( return ls_schemas.Dataset( **response.json(), _host_url=self._host_url, - _tenant_id=self._get_tenant_id(), + _tenant_id=self._get_optional_tenant_id(), ) def has_dataset( @@ -2092,10 +2105,14 @@ def read_dataset( f"Dataset {dataset_name} not found" ) return ls_schemas.Dataset( - **result[0], _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **result[0], + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) return ls_schemas.Dataset( - **result, _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **result, + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) def read_dataset_openai_finetuning( @@ -2155,7 +2172,9 @@ def list_datasets( yield from ( ls_schemas.Dataset( - **dataset, _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **dataset, + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) for dataset in self._get_paginated_list("/datasets", params=params) ) @@ -2498,7 +2517,9 @@ def create_example( ls_utils.raise_for_status_with_text(response) result = response.json() return ls_schemas.Example( - **result, _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **result, + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) def read_example(self, example_id: ID_TYPE) -> ls_schemas.Example: @@ -2516,7 +2537,7 @@ def read_example(self, example_id: ID_TYPE) -> ls_schemas.Example: return ls_schemas.Example( **response.json(), _host_url=self._host_url, - _tenant_id=self._get_tenant_id(), + _tenant_id=self._get_optional_tenant_id(), ) def list_examples( @@ -2554,7 +2575,9 @@ def list_examples( params["inline_s3_urls"] = inline_s3_urls yield from ( ls_schemas.Example( - **example, _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **example, + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) for example in self._get_paginated_list("/examples", params=params) ) @@ -2667,7 +2690,9 @@ def _resolve_example_id( reference_example_ = example elif isinstance(example, dict): reference_example_ = ls_schemas.Example( - **example, _host_url=self._host_url, _tenant_id=self._get_tenant_id() + **example, + _host_url=self._host_url, + _tenant_id=self._get_optional_tenant_id(), ) elif run.reference_example_id is not None: reference_example_ = self.read_example(run.reference_example_id) diff --git a/python/pyproject.toml b/python/pyproject.toml index 6c7216678..cfbefc9fa 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "langsmith" -version = "0.1.8" +version = "0.1.9" description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform." authors = ["LangChain "] license = "MIT"