Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add path as an option for uploading attachments #1331

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
49 changes: 35 additions & 14 deletions python/langsmith/_internal/_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import itertools
import logging
import os
import uuid
from typing import Literal, Optional, Union, cast
from io import BufferedReader
from typing import Dict, Literal, Optional, Union, cast

from langsmith import schemas as ls_schemas
from langsmith._internal import _orjson
Expand Down Expand Up @@ -211,9 +213,9 @@ def serialized_feedback_operation_to_multipart_parts_and_context(

def serialized_run_operation_to_multipart_parts_and_context(
op: SerializedRunOperation,
) -> MultipartPartsAndContext:
) -> tuple[MultipartPartsAndContext, Dict[str, BufferedReader]]:
acc_parts: list[MultipartPart] = []

opened_files_dict: Dict[str, BufferedReader] = {}
# this is main object, minus inputs/outputs/events/attachments
acc_parts.append(
(
Expand Down Expand Up @@ -246,7 +248,7 @@ def serialized_run_operation_to_multipart_parts_and_context(
),
)
if op.attachments:
for n, (content_type, valb) in op.attachments.items():
for n, (content_type, data) in op.attachments.items():
if "." in n:
logger.warning(
f"Skipping logging of attachment '{n}' "
Expand All @@ -256,18 +258,37 @@ def serialized_run_operation_to_multipart_parts_and_context(
)
continue

acc_parts.append(
(
f"attachment.{op.id}.{n}",
if isinstance(data, bytes):
acc_parts.append(
(
None,
valb,
content_type,
{"Content-Length": str(len(valb))},
),
f"attachment.{op.id}.{n}",
(
None,
data,
content_type,
{"Content-Length": str(len(data))},
),
)
)
else:
file_size = os.path.getsize(data)
if str(data) in opened_files_dict:
file = opened_files_dict[str(data)]
else:
file = open(data, "rb")
opened_files_dict[str(data)] = file
acc_parts.append(
(
f"attachment.{op.id}.{n}",
(
None,
file, # type: ignore[arg-type]
f"{content_type}; length={file_size}",
{},
),
)
)
)
return MultipartPartsAndContext(
acc_parts,
f"trace={op.trace_id},id={op.id}",
)
), opened_files_dict
Loading