-
Notifications
You must be signed in to change notification settings - Fork 82
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
e8189ae
7b2e73d
347198e
9fbc0c8
6275c1c
52ff721
cfc2f3e
1976dcc
9e01bfd
34c0dd8
e802e31
86e2a0f
1207bb0
7d09a00
78c93e8
5d0a956
ee32603
ff7f9e3
aa255ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,7 +318,9 @@ def patch(self) -> None: | |
"""Patch the run tree to the API in a background thread.""" | ||
if not self.end_time: | ||
self.end() | ||
attachments = self.attachments | ||
attachments = { | ||
a: v for a, v in self.attachments.items() if isinstance(v, tuple) | ||
} | ||
Comment on lines
+321
to
+323
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was done strictly for typing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is v ever not a tuple? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If so might be worth doing in a for loop and raising an error if not. I think would be unexpected. But mypy probably knows better than me, so there might be a case we're not handling here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v is never not a tuple here, this is just because of the union types I believe and it thinks that something could be passed here that is never passed |
||
try: | ||
# Avoid loading the same attachment twice | ||
if attachments: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ | |
StrictInt, | ||
) | ||
|
||
from pathlib import Path | ||
|
||
from typing_extensions import Literal | ||
|
||
SCORE_TYPE = Union[StrictBool, StrictInt, StrictFloat, None] | ||
|
@@ -63,7 +65,7 @@ def my_function(bar: int, my_val: Attachment): | |
data: bytes | ||
|
||
|
||
Attachments = Dict[str, Union[Tuple[str, bytes], Attachment]] | ||
Attachments = Dict[str, Union[Tuple[str, bytes], Attachment, Tuple[str, Path]]] | ||
isahers1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Attachments associated with the run. | ||
Each entry is a tuple of (mime_type, bytes), or (mime_type, file_path)""" | ||
|
||
|
@@ -370,7 +372,7 @@ class RunBase(BaseModel): | |
tags: Optional[List[str]] = None | ||
"""Tags for categorizing or annotating the run.""" | ||
|
||
attachments: Attachments = Field(default_factory=dict) | ||
attachments: Attachments | Dict[str, AttachmentInfo] = Field(default_factory=dict) | ||
"""Attachments associated with the run. | ||
Each entry is a tuple of (mime_type, bytes).""" | ||
|
||
|
@@ -390,6 +392,11 @@ def __repr__(self): | |
"""Return a string representation of the RunBase object.""" | ||
return f"{self.__class__}(id={self.id}, name='{self.name}', run_type='{self.run_type}')" | ||
|
||
class Config: | ||
"""Configuration class for the schema.""" | ||
|
||
arbitrary_types_allowed = True | ||
|
||
Comment on lines
+393
to
+397
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to allow the AttachmentInfo |
||
|
||
class Run(RunBase): | ||
"""Run schema when loading from the DB.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we not need to read this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, I believe the multipart endpoint allows a buffer to be passed (I also added a test that should cover this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty cve worthy so we should be careful about cases where this is allowed/encouraged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestion would be to require either an environment variable of LANGSMITH_DANGEROUSLY_ALLOW_FILESYSTEM or some client init variable saying this. Otherwise throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to explicitly
close
the files the are opened here, using sometry, finally
pattern. I would structure the code such that the multipart form is assembled and sent within the same method we are opening the file handles: