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

Fix a couple of bugs in the base64 file_encoding_strategy #398

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

aron
Copy link
Contributor

@aron aron commented Nov 13, 2024

This commit adds tests for the file_encoding_strategy argument for replicate.run() and fixes two bugs that surfaced:

  1. replicate.run() would convert the file provided into base64 encoded data but not a valid data URL. We now use the base64_encode_file function used for outputs.
  2. replicate.async_run() accepted but did not use the file_encoding_strategy flag at all. This is fixed, though it is worth noting that base64_encode_file is not optimized for async workflows and will block. This might be okay as the file sizes expected for data URL payloads should be very small.

This commit adds tests for the `file_encoding_strategy` argument for
`replicate.run()` and fixes two bugs that surfaced:

 1. `replicate.run()` would convert the file provided into base64
    encoded data but not a valid data URL. We now use the
    `base64_encode_file` function used for outputs.
 2. `replicate.async_run()` accepted but did not use the
    `file_encoding_strategy` flag at all. This is fixed, though
    it is worth noting that `base64_encode_file` is not optimized
    for async workflows and will block. This migth be okay as the
    file sizes expected for data URL paylaods should be very
    small.
@@ -43,7 +43,7 @@ def encode_json(
return encode_json(file, client, file_encoding_strategy)
if isinstance(obj, io.IOBase):
if file_encoding_strategy == "base64":
return base64.b64encode(obj.read()).decode("utf-8")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lolwut. how did this ever work? did it just... not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did not.

if isinstance(obj, io.IOBase):
return (await client.files.async_create(obj)).urls["get"]
if file_encoding_strategy == "base64":
# TODO: This should ideally use an async based file reader path.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably isn't too hard if we're prepared to take a dep on aiofile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Member

@nickstenning nickstenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if I've understood correctly. Did the base64 support previously just not work?

@aron aron merged commit 07c8fbb into main Nov 15, 2024
7 checks passed
@aron aron deleted the fix-data-url-encoding#396 branch November 15, 2024 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants