Skip to content

Commit

Permalink
flag
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 committed Dec 18, 2024
1 parent 78c93e8 commit 5d0a956
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 44 deletions.
51 changes: 43 additions & 8 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ def multipart_ingest(
] = None,
*,
pre_sampled: bool = False,
dangerously_allow_filesystem: bool = False,
) -> None:
"""Batch ingest/upsert multiple runs in the Langsmith system.
Expand Down Expand Up @@ -1621,6 +1622,18 @@ def multipart_ingest(
)
)

for op in serialized_ops:
if isinstance(op, SerializedRunOperation) and op.attachments:
for attachment in op.attachments.values():
if (
isinstance(attachment, tuple)
and isinstance(attachment[1], Path)
and not dangerously_allow_filesystem
):
raise ValueError(
"Must set dangerously_allow_filesystem to True to use filesystem paths in multipart ingest."
)

# sent the runs in multipart requests
self._multipart_ingest_ops(serialized_ops)

Expand Down Expand Up @@ -1684,6 +1697,7 @@ def update_run(
extra: Optional[Dict] = None,
tags: Optional[List[str]] = None,
attachments: Optional[ls_schemas.Attachments] = None,
dangerously_allow_filesystem: bool = False,
**kwargs: Any,
) -> None:
"""Update a run in the LangSmith API.
Expand Down Expand Up @@ -1726,6 +1740,15 @@ def update_run(
"session_name": kwargs.pop("session_name", None),
}
if attachments:
for _, attachment in attachments.items():
if (
isinstance(attachment, tuple)
and isinstance(attachment[1], Path)
and not dangerously_allow_filesystem
):
raise ValueError(
"Must set dangerously_allow_filesystem=True to allow filesystem attachments."
)
data["attachments"] = attachments
use_multipart = (
self.tracing_queue is not None
Expand Down Expand Up @@ -3481,7 +3504,7 @@ def _prepare_multipart_data(
| List[ls_schemas.ExampleUpdateWithAttachments],
],
include_dataset_id: bool = False,
dangerously_allow_filesystem: Optional[bool] = False,
dangerously_allow_filesystem: bool = False,
) -> Tuple[Any, bytes]:
parts: List[MultipartPart] = []
if include_dataset_id:
Expand Down Expand Up @@ -3567,7 +3590,7 @@ def _prepare_multipart_data(
for name, attachment in example.attachments.items():
if isinstance(attachment, tuple):
if isinstance(attachment[1], Path):
if dangerously_allow_filesystem == True:
if dangerously_allow_filesystem:
mime_type, file_path = attachment
file_size = os.path.getsize(file_path)
parts.append(
Expand Down Expand Up @@ -3641,7 +3664,7 @@ def update_examples_multipart(
*,
dataset_id: ID_TYPE,
updates: Optional[List[ls_schemas.ExampleUpdateWithAttachments]] = None,
dangerously_allow_filesystem: Optional[bool] = False,
dangerously_allow_filesystem: bool = False,
) -> ls_schemas.UpsertExamplesResponse:
"""Upload examples."""
if not (self.info.instance_flags or {}).get(
Expand All @@ -3653,7 +3676,11 @@ def update_examples_multipart(
if updates is None:
updates = []

encoder, data = self._prepare_multipart_data(updates, include_dataset_id=False, dangerously_allow_filesystem=dangerously_allow_filesystem)
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 @@ -3674,7 +3701,7 @@ def upload_examples_multipart(
*,
dataset_id: ID_TYPE,
uploads: Optional[List[ls_schemas.ExampleUploadWithAttachments]] = None,
dangerously_allow_filesystem: Optional[bool] = False,
dangerously_allow_filesystem: bool = False,
) -> ls_schemas.UpsertExamplesResponse:
"""Upload examples."""
if not (self.info.instance_flags or {}).get(
Expand All @@ -3685,7 +3712,11 @@ def upload_examples_multipart(
)
if uploads is None:
uploads = []
encoder, data = self._prepare_multipart_data(uploads, include_dataset_id=False, dangerously_allow_filesystem=dangerously_allow_filesystem)
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 @@ -3705,7 +3736,7 @@ def upsert_examples_multipart(
self,
*,
upserts: Optional[List[ls_schemas.ExampleUpsertWithAttachments]] = None,
dangerously_allow_filesystem: Optional[bool] = False,
dangerously_allow_filesystem: bool = False,
) -> ls_schemas.UpsertExamplesResponse:
"""Upsert examples.
Expand All @@ -3722,7 +3753,11 @@ def upsert_examples_multipart(
if upserts is None:
upserts = []

encoder, data = self._prepare_multipart_data(upserts, include_dataset_id=True, dangerously_allow_filesystem=dangerously_allow_filesystem)
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
Loading

0 comments on commit 5d0a956

Please sign in to comment.