Skip to content

Convert files to data URLs on webhook post #2185

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/cog/server/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from .. import schema
from ..base_input import BaseInput
from ..files import put_file_to_signed_endpoint
from ..files import put_file_to_signed_endpoint, upload_file
from ..json import upload_files
from ..types import PYDANTIC_V2
from .errors import FileUploadError, RunnerBusyError, UnknownPredictionError
Expand Down Expand Up @@ -450,7 +450,7 @@ def _send_webhook(self, event: schema.WebhookEvent) -> None:

def _upload_files(self, output: Any) -> Any:
if self._file_uploader is None:
return output
return upload_file(output)

try:
# TODO: clean up output files
Expand Down
9 changes: 5 additions & 4 deletions python/cog/server/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ def webhook_caller(webhook: str) -> Callable[[Any], None]:
def caller(response: PredictionResponse) -> None:
if throttler.should_send_response(response):
if PYDANTIC_V2:
dict_response = jsonable_encoder(
response.model_dump(exclude_unset=True)
)
response_object = response.model_dump(exclude_unset=True)
else:
dict_response = jsonable_encoder(response.dict(exclude_unset=True))
response_object = response.dict(exclude_unset=True)

dict_response = jsonable_encoder(response_object)

if Status.is_terminal(response.status):
# For terminal updates, retry persistently
retry_session.post(webhook, json=dict_response)
Expand Down
28 changes: 28 additions & 0 deletions python/tests/server/test_webhook.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import tempfile

import requests
import responses
from responses import registries

from cog.schema import PredictionResponse, Status, WebhookEvent
from cog.server.webhook import webhook_caller, webhook_caller_filtered
from cog.types import Path


@responses.activate
Expand Down Expand Up @@ -153,3 +156,28 @@ def test_webhook_caller_connection_errors():
c = webhook_caller("https://example.com/webhook/123")
# this should not raise an error
c(response)


@responses.activate
def test_webhook_caller_converts_files_data_urls():
with tempfile.NamedTemporaryFile() as tmpfile:
with open(tmpfile.name, "w", encoding="utf8") as handle:
handle.write("hello world")

c = webhook_caller("https://example.com/webhook/123")

payload = {
"status": Status.SUCCEEDED,
"output": Path(tmpfile.name),
"input": {},
}
response = PredictionResponse(**payload)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok just for my understanding, without the change we would've passed back the path to the /tmp file which then the caller could not access, so we're uploading the file (somewhere) and passing a URL back? And then the line below has a b64 encoded URL

payload["output"] = "data:None;base64,aGVsbG8gd29ybGQ="
Copy link
Contributor

Choose a reason for hiding this comment

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

Is None supposed to be here?

Suggested change
payload["output"] = "data:None;base64,aGVsbG8gd29ybGQ="
payload["output"] = "data:text/plain;base64,aGVsbG8gd29ybGQ="

Copy link
Contributor

@aron aron Mar 6, 2025

Choose a reason for hiding this comment

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

Actually, what is this test doing?

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 tests that the webhook is sent data URLs rather than tmp file directories. I can change the mime-type however it seems odd to me that the upload_file function uses None as a default if that default is considered itself invalid. Can you comment on that?


responses.post(
"https://example.com/webhook/123",
json=payload,
status=200,
)

c(response)
Loading