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: expose errors occurring before the first yield of stream response #5002

Merged
merged 2 commits into from
Sep 27, 2024
Merged
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
22 changes: 21 additions & 1 deletion src/_bentoml_sdk/io_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async def from_http_request(cls, request: Request, serde: Serde) -> IODescriptor
@classmethod
async def to_http_response(cls, obj: t.Any, serde: Serde) -> Response:
"""Convert a output value to HTTP response"""
import itertools
import mimetypes

from pydantic import RootModel
Expand All @@ -183,10 +184,24 @@ async def to_http_response(cls, obj: t.Any, serde: Serde) -> Response:
from _bentoml_impl.serde import JSONSerde

if inspect.isasyncgen(obj):
try:
# try if there is any error before the first yield
# if so, the error can be surfaced in the response
first_chunk = await obj.__anext__()
except StopAsyncIteration:
pre_chunks = []
else:
pre_chunks = [first_chunk]

async def gen() -> t.AsyncGenerator[t.Any, None]:
for chunk in pre_chunks:
yield chunk
async for chunk in obj:
yield chunk

async def async_stream() -> t.AsyncGenerator[str | bytes, None]:
try:
async for item in obj:
async for item in gen():
if isinstance(item, (str, bytes)):
yield item
else:
Expand All @@ -201,6 +216,11 @@ async def async_stream() -> t.AsyncGenerator[str | bytes, None]:
return StreamingResponse(async_stream(), media_type=cls.mime_type())

elif inspect.isgenerator(obj):
trial, obj = itertools.tee(obj)
try:
next(trial) # try if there is any error before the first yield
except StopIteration:
pass

def content_stream() -> t.Generator[str | bytes, None, None]:
try:
Expand Down
13 changes: 8 additions & 5 deletions src/bentoml/_internal/cloud/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ def is_bento_changed(bento_info: Bento) -> bool:

spinner = Spinner(console=console)
needs_update = is_bento_changed(bento_info)
spinner.log(f"💻 View Dashboard: {self.admin_console}")
endpoint_url: str | None = None
try:
spinner.start()
upload_id = spinner.transmission_progress.add_task(
Expand Down Expand Up @@ -861,6 +863,9 @@ def is_bento_changed(bento_info: Bento) -> bool:
requirements_hash = self._init_deployment_files(
bento_dir, spinner=spinner
)
if endpoint_url is None:
endpoint_url = self.get_endpoint_urls(False)[0]
spinner.log(f"🌐 Endpoint: {endpoint_url}")
with self._tail_logs(spinner.console):
spinner.update("👀 Watching for changes")
for changes in watchfiles.watch(
Expand Down Expand Up @@ -937,13 +942,11 @@ def is_bento_changed(bento_info: Bento) -> bool:
return
except KeyboardInterrupt:
spinner.log(
"The deployment is still running, view the dashboard:\n"
f" [blue]{self.admin_console}[/]\n\n"
"Next steps:\n"
"* Push the bento to BentoCloud:\n"
" [blue]$ bentoml build --push[/]\n\n"
"\nWatcher stopped. Next steps:\n"
"* Attach to this deployment again:\n"
f" [blue]$ bentoml develop --attach {self.name} --cluster {self.cluster}[/]\n\n"
"* Push the bento to BentoCloud:\n"
" [blue]$ bentoml build --push[/]\n\n"
"* Terminate the deployment:\n"
f" [blue]$ bentoml deployment terminate {self.name} --cluster {self.cluster}[/]"
)
Expand Down