-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Support input/output files by introducing FileReference Pydantic annotation #240
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #240 +/- ##
==========================================
- Coverage 76.52% 76.31% -0.21%
==========================================
Files 28 28
Lines 3156 3175 +19
Branches 505 508 +3
==========================================
+ Hits 2415 2423 +8
- Misses 520 531 +11
Partials 221 221 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@@ -356,6 +357,61 @@ def is_differentiable(obj: Any) -> bool: | |||
return False | |||
|
|||
|
|||
class FileReference: |
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.
Just reading this before our meeting and leaving my thoughts here so they do not get lost (sorry for commenting on a draft). Maybe we can do this with a field validator?
from typing import Annotated
from pydantic import AfterValidator, RootModel
def is_relative(path: Path) -> Path:
if path.is_relative_to(Path.cwd()):
path = path.relative_to(Path.cwd())
else:
raise ValueError(
f"FileReference path must be relative to the current working directory: {path}"
)
return path
class FileReference(RootModel):
root: Annotated[Path, AfterValidator(is_relative)]
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 played around with RootModel a bit. The key issue I run into is that for a schema like
class InputSchema(BaseModel):
name: str = Field(description="Name of the person you want to greet.")
input_file: FileReference = Field(description="A file that can be used as input.")
the server gets the validated model
name='Osborne' input_file=Apply_FileReference(root=PosixPath('test.txt'))
I don't see a straightforward way to get rid of the nested model and resolve straight to a path. In order to avoid nesting, my understanding is that we actually have to go with the custom type (which also has the upside that we can even pass plain strings and automatically convert them to Path objects).
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.
hmm, does this help?
In [4]: m = InputSchema(name="Osborne", input_file="./test.txt")
In [5]: m.model_dump()
Out[5]: {'name': 'Osborne', 'input_file': PosixPath('test.txt')}
In [6]: m.input_file.model_dump()
Out[6]: PosixPath('test.txt')
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.
Indeed, that gets rid of the nested model! The catch is that in apply
, the user gets an InputSchema
(not a JSON/dict), so we can't dump the input before passing to user code (and I don't think the user should have to unpack inputs).
I've pushed the RootModel experiment to https://github.com/pasteurlabs/tesseract-core/tree/linus/feat-file-schema-root-model
You can give it a try by serving the helloworld example and
curl -d '{"inputs": {"name": "Osborne", "input_file": "test.txt"}}' -H "Conn/json" http://127.0.0.1:8080/apply
Relevant issue or PR
N/A
Description of changes
We'd like to support (binary) input/output files in tesseracts. This PR adds a
FileReference
Pydantic annotation.Its validation step ensures the file exists and is a path relative to correct directory. The latter is crucial since it ensures that files are actually accessible, AND ensures we won't leak internals of the container/host.
Usage (based on hello world tesseract):
The tesseract will resolve this to simply "test.txt" and verify the file exists.
TODO
--output-path
, and ensure paths are relative to--input-path
and--output-path
. Depends on @angela-ko 's upcoming changes.Testing done
Local testing for variations of file paths.