Skip to content

Commit 90349ac

Browse files
authored
feat/add support for arbitrary messages (#44)
1 parent bf94b0f commit 90349ac

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.24
2+
3+
* **Add support for passing messages back other than errors**
4+
15
## 0.0.23
26

37
* **Handle errors in streaming responses**
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.23" # pragma: no cover
1+
__version__ = "0.0.24" # pragma: no cover

unstructured_platform_plugins/etl_uvicorn/api_generator.py

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class EtlApiException(Exception):
3939
logger = logging.getLogger("uvicorn.error")
4040

4141

42+
class MessageChannels(BaseModel):
43+
infos: list[str] = Field(default_factory=list)
44+
warnings: list[str] = Field(default_factory=list)
45+
46+
4247
def log_func_and_body(func: Callable, body: Optional[str] = None) -> None:
4348
msg = None
4449
if logger.level == LOG_LEVELS.get("debug", logging.NOTSET):
@@ -135,6 +140,7 @@ class InvokeResponse(BaseModel):
135140
filedata_meta: Optional[filedata_meta_model]
136141
status_code_text: Optional[str] = None
137142
output: Optional[response_type] = None
143+
message_channels: MessageChannels = Field(default_factory=MessageChannels)
138144

139145
input_schema = get_input_schema(func, omit=["usage", "filedata_meta"])
140146
input_schema_model = schema_to_base_model(input_schema)
@@ -146,11 +152,14 @@ class InvokeResponse(BaseModel):
146152
async def wrap_fn(func: Callable, kwargs: Optional[dict[str, Any]] = None) -> ResponseType:
147153
usage: list[UsageData] = []
148154
filedata_meta = FileDataMeta()
155+
message_channels = MessageChannels()
149156
request_dict = kwargs if kwargs else {}
150157
if "usage" in inspect.signature(func).parameters:
151158
request_dict["usage"] = usage
152159
else:
153160
logger.warning("usage data not an expected parameter, omitting")
161+
if "message_channels" in inspect.signature(func).parameters:
162+
request_dict["message_channels"] = message_channels
154163
if "filedata_meta" in inspect.signature(func).parameters:
155164
request_dict["filedata_meta"] = filedata_meta
156165
try:
@@ -161,6 +170,7 @@ async def _stream_response():
161170
async for output in func(**(request_dict or {})):
162171
yield InvokeResponse(
163172
usage=usage,
173+
message_channels=message_channels,
164174
filedata_meta=filedata_meta_model.model_validate(
165175
filedata_meta.model_dump()
166176
),
@@ -171,6 +181,7 @@ async def _stream_response():
171181
logger.error(f"Failure streaming response: {e}", exc_info=True)
172182
yield InvokeResponse(
173183
usage=usage,
184+
message_channels=message_channels,
174185
filedata_meta=None,
175186
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
176187
status_code_text=f"[{e.__class__.__name__}] {e}",
@@ -181,6 +192,7 @@ async def _stream_response():
181192
output = await invoke_func(func=func, kwargs=request_dict)
182193
return InvokeResponse(
183194
usage=usage,
195+
message_channels=message_channels,
184196
filedata_meta=filedata_meta_model.model_validate(filedata_meta.model_dump()),
185197
status_code=status.HTTP_200_OK,
186198
output=output,
@@ -189,6 +201,7 @@ async def _stream_response():
189201
logger.info("Unrecoverable error occurred during plugin invocation")
190202
return InvokeResponse(
191203
usage=usage,
204+
message_channels=message_channels,
192205
status_code=512,
193206
status_code_text=ex.message,
194207
filedata_meta=filedata_meta_model.model_validate(filedata_meta.model_dump()),
@@ -198,6 +211,7 @@ async def _stream_response():
198211
http_error = wrap_error(invoke_error)
199212
return InvokeResponse(
200213
usage=usage,
214+
message_channels=message_channels,
201215
filedata_meta=filedata_meta_model.model_validate(filedata_meta.model_dump()),
202216
status_code=http_error.status_code,
203217
status_code_text=f"[{invoke_error.__class__.__name__}] {invoke_error}",

0 commit comments

Comments
 (0)