Skip to content

Commit

Permalink
add flag for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 committed Dec 18, 2024
1 parent 7d09a00 commit 78c93e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
37 changes: 23 additions & 14 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,7 @@ def _prepare_multipart_data(
| List[ls_schemas.ExampleUpdateWithAttachments],
],
include_dataset_id: bool = False,
dangerously_allow_filesystem: Optional[bool] = False,
) -> Tuple[Any, bytes]:
parts: List[MultipartPart] = []
if include_dataset_id:
Expand Down Expand Up @@ -3566,19 +3567,24 @@ def _prepare_multipart_data(
for name, attachment in example.attachments.items():
if isinstance(attachment, tuple):
if isinstance(attachment[1], Path):
mime_type, file_path = attachment
file_size = os.path.getsize(file_path)
parts.append(
(
f"{example_id}.attachment.{name}",
if dangerously_allow_filesystem == True:
mime_type, file_path = attachment
file_size = os.path.getsize(file_path)
parts.append(
(
None,
open(file_path, "rb"), # type: ignore[arg-type]
f"{mime_type}; length={file_size}",
{},
),
f"{example_id}.attachment.{name}",
(
None,
open(file_path, "rb"), # type: ignore[arg-type]
f"{mime_type}; length={file_size}",
{},
),
)
)
else:
raise ValueError(
"dangerously_allow_filesystem must be True to upload files from the filesystem"
)
)
else:
mime_type, data = attachment
parts.append(
Expand Down Expand Up @@ -3635,6 +3641,7 @@ def update_examples_multipart(
*,
dataset_id: ID_TYPE,
updates: Optional[List[ls_schemas.ExampleUpdateWithAttachments]] = None,
dangerously_allow_filesystem: Optional[bool] = False,
) -> ls_schemas.UpsertExamplesResponse:
"""Upload examples."""
if not (self.info.instance_flags or {}).get(
Expand All @@ -3646,7 +3653,7 @@ def update_examples_multipart(
if updates is None:
updates = []

encoder, data = self._prepare_multipart_data(updates, include_dataset_id=False)
encoder, data = self._prepare_multipart_data(updates, include_dataset_id=False, dangerously_allow_filesystem=dangerously_allow_filesystem)

response = self.request_with_retries(
"PATCH",
Expand All @@ -3667,6 +3674,7 @@ def upload_examples_multipart(
*,
dataset_id: ID_TYPE,
uploads: Optional[List[ls_schemas.ExampleUploadWithAttachments]] = None,
dangerously_allow_filesystem: Optional[bool] = False,
) -> ls_schemas.UpsertExamplesResponse:
"""Upload examples."""
if not (self.info.instance_flags or {}).get(
Expand All @@ -3677,7 +3685,7 @@ def upload_examples_multipart(
)
if uploads is None:
uploads = []
encoder, data = self._prepare_multipart_data(uploads, include_dataset_id=False)
encoder, data = self._prepare_multipart_data(uploads, include_dataset_id=False, dangerously_allow_filesystem=dangerously_allow_filesystem)

response = self.request_with_retries(
"POST",
Expand All @@ -3697,6 +3705,7 @@ def upsert_examples_multipart(
self,
*,
upserts: Optional[List[ls_schemas.ExampleUpsertWithAttachments]] = None,
dangerously_allow_filesystem: Optional[bool] = False,
) -> ls_schemas.UpsertExamplesResponse:
"""Upsert examples.
Expand All @@ -3713,7 +3722,7 @@ def upsert_examples_multipart(
if upserts is None:
upserts = []

encoder, data = self._prepare_multipart_data(upserts, include_dataset_id=True)
encoder, data = self._prepare_multipart_data(upserts, include_dataset_id=True, dangerously_allow_filesystem=dangerously_allow_filesystem)

response = self.request_with_retries(
"POST",
Expand Down
4 changes: 2 additions & 2 deletions python/tests/integration_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,7 @@ def test_examples_multipart_attachment_path(langchain_client: Client) -> None:
)

created_examples = langchain_client.upload_examples_multipart(
dataset_id=dataset.id, uploads=[example]
dataset_id=dataset.id, uploads=[example], dangerously_allow_filesystem=True
)
assert created_examples["count"] == 1

Expand All @@ -1988,7 +1988,7 @@ def test_examples_multipart_attachment_path(langchain_client: Client) -> None:
)

langchain_client.update_examples_multipart(
dataset_id=dataset.id, updates=[example_update]
dataset_id=dataset.id, updates=[example_update], dangerously_allow_filesystem=True
)

retrieved = langchain_client.read_example(example_id)
Expand Down

0 comments on commit 78c93e8

Please sign in to comment.