Skip to content

Commit a036254

Browse files
[bug fix][AIC-py] sort of do exception handling during streaming (#860)
[bug fix][AIC-py] sort of do exception handling during streaming This piggybacks on the cancellation during stream flow, which works as far as I've tested. The frontend gets the message. <img width="396" alt="image" src="https://github.com/lastmile-ai/aiconfig/assets/148090348/5e5f603f-4ac3-405e-aead-a3dbfb78d163">
2 parents 08f7086 + 44fbded commit a036254

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

python/src/aiconfig/editor/server/server.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,19 @@ def generate(cancellation_token_id: str): # type: ignore
234234
# Use multi-threading so that we don't block run command from
235235
# displaying the streamed output (if streaming is supported)
236236
def run_async_config_in_thread():
237-
asyncio.run(aiconfig.run(prompt_name=prompt_name, params=params, run_with_dependencies=False, options=inference_options)) # type: ignore
237+
try:
238+
asyncio.run(aiconfig.run(prompt_name=prompt_name, params=params, run_with_dependencies=False, options=inference_options)) # type: ignore
239+
except Exception as e:
240+
output_text_queue.put(e)
241+
238242
output_text_queue.put(STOP_STREAMING_SIGNAL) # type: ignore
239243

240-
def create_cancellation_payload():
244+
def create_error_payload(message: str, code: int):
241245
aiconfig_json = aiconfig_deep_copy.model_dump(exclude=EXCLUDE_OPTIONS) if aiconfig_deep_copy is not None else None
242-
return json.dumps({"error": {"message": "The task was cancelled.", "code": 499, "data": aiconfig_json}})
246+
return json.dumps({"error": {"message": message, "code": code, "data": aiconfig_json}})
247+
248+
def create_cancellation_payload():
249+
return create_error_payload(message="The task was cancelled.", code=499)
243250

244251
def handle_cancellation():
245252
yield "["
@@ -307,7 +314,10 @@ def kill_thread(thread_id: int | None):
307314
yield from handle_cancellation()
308315
return
309316

310-
if isinstance(text, str):
317+
if isinstance(text, Exception):
318+
yield from create_error_payload(message=f"Exception: {text}", code=500)
319+
return
320+
elif isinstance(text, str):
311321
accumulated_output_text += text
312322
elif isinstance(text, dict) and "content" in text:
313323
# TODO: Fix streaming output format so that it returns text

0 commit comments

Comments
 (0)