Skip to content

Commit bf94b0f

Browse files
authored
handle errors in streaming (#43)
1 parent c59b150 commit bf94b0f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.23
2+
3+
* **Handle errors in streaming responses**
4+
15
## 0.0.22
26

37
* **Bump `unstructured-ingest` to 0.4.0**
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.22" # pragma: no cover
1+
__version__ = "0.0.23" # pragma: no cover

unstructured_platform_plugins/etl_uvicorn/api_generator.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _wrap_in_fastapi(
132132
class InvokeResponse(BaseModel):
133133
usage: list[UsageData]
134134
status_code: int
135-
filedata_meta: filedata_meta_model
135+
filedata_meta: Optional[filedata_meta_model]
136136
status_code_text: Optional[str] = None
137137
output: Optional[response_type] = None
138138

@@ -156,16 +156,24 @@ async def wrap_fn(func: Callable, kwargs: Optional[dict[str, Any]] = None) -> Re
156156
try:
157157
if inspect.isasyncgenfunction(func):
158158
# Stream response if function is an async generator
159-
160159
async def _stream_response():
161-
async for output in func(**(request_dict or {})):
160+
try:
161+
async for output in func(**(request_dict or {})):
162+
yield InvokeResponse(
163+
usage=usage,
164+
filedata_meta=filedata_meta_model.model_validate(
165+
filedata_meta.model_dump()
166+
),
167+
status_code=status.HTTP_200_OK,
168+
output=output,
169+
).model_dump_json() + "\n"
170+
except Exception as e:
171+
logger.error(f"Failure streaming response: {e}", exc_info=True)
162172
yield InvokeResponse(
163173
usage=usage,
164-
filedata_meta=filedata_meta_model.model_validate(
165-
filedata_meta.model_dump()
166-
),
167-
status_code=status.HTTP_200_OK,
168-
output=output,
174+
filedata_meta=None,
175+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
176+
status_code_text=f"[{e.__class__.__name__}] {e}",
169177
).model_dump_json() + "\n"
170178

171179
return StreamingResponse(_stream_response(), media_type="application/x-ndjson")

0 commit comments

Comments
 (0)