Spaces:
Paused
Paused
Commit
·
f23410e
1
Parent(s):
dd504cd
added more error handling
Browse files- app/routes/chat_api.py +24 -3
app/routes/chat_api.py
CHANGED
|
@@ -186,13 +186,34 @@ async def chat_completions(fastapi_request: Request, request: OpenAIRequest, api
|
|
| 186 |
extra_body=openai_extra_body
|
| 187 |
)
|
| 188 |
async for chunk in stream_response:
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
yield "data: [DONE]\n\n"
|
| 191 |
except Exception as stream_error:
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
print(f"ERROR: {error_msg_stream}")
|
|
|
|
| 194 |
error_response_content = create_openai_error_response(500, error_msg_stream, "server_error")
|
| 195 |
-
|
|
|
|
|
|
|
| 196 |
yield "data: [DONE]\n\n"
|
| 197 |
return StreamingResponse(openai_stream_generator(), media_type="text/event-stream")
|
| 198 |
else: # Not streaming
|
|
|
|
| 186 |
extra_body=openai_extra_body
|
| 187 |
)
|
| 188 |
async for chunk in stream_response:
|
| 189 |
+
try:
|
| 190 |
+
yield f"data: {chunk.model_dump_json()}\n\n"
|
| 191 |
+
except Exception as chunk_serialization_error:
|
| 192 |
+
error_msg_chunk = f"Error serializing OpenAI chunk for {request.model}: {str(chunk_serialization_error)}. Chunk: {str(chunk)[:200]}"
|
| 193 |
+
print(f"ERROR: {error_msg_chunk}")
|
| 194 |
+
# Truncate
|
| 195 |
+
if len(error_msg_chunk) > 1024:
|
| 196 |
+
error_msg_chunk = error_msg_chunk[:1024] + "..."
|
| 197 |
+
error_response_chunk = create_openai_error_response(500, error_msg_chunk, "server_error")
|
| 198 |
+
json_payload_for_chunk_error = json.dumps(error_response_chunk)
|
| 199 |
+
print(f"DEBUG: Yielding chunk serialization error JSON payload (OpenAI path): {json_payload_for_chunk_error}")
|
| 200 |
+
yield f"data: {json_payload_for_chunk_error}\n\n"
|
| 201 |
+
yield "data: [DONE]\n\n"
|
| 202 |
+
return # Stop further processing for this request
|
| 203 |
yield "data: [DONE]\n\n"
|
| 204 |
except Exception as stream_error:
|
| 205 |
+
original_error_message = str(stream_error)
|
| 206 |
+
# Truncate very long error messages
|
| 207 |
+
if len(original_error_message) > 1024:
|
| 208 |
+
original_error_message = original_error_message[:1024] + "..."
|
| 209 |
+
|
| 210 |
+
error_msg_stream = f"Error during OpenAI client streaming for {request.model}: {original_error_message}"
|
| 211 |
print(f"ERROR: {error_msg_stream}")
|
| 212 |
+
|
| 213 |
error_response_content = create_openai_error_response(500, error_msg_stream, "server_error")
|
| 214 |
+
json_payload_for_stream_error = json.dumps(error_response_content)
|
| 215 |
+
print(f"DEBUG: Yielding stream error JSON payload (OpenAI path): {json_payload_for_stream_error}")
|
| 216 |
+
yield f"data: {json_payload_for_stream_error}\n\n"
|
| 217 |
yield "data: [DONE]\n\n"
|
| 218 |
return StreamingResponse(openai_stream_generator(), media_type="text/event-stream")
|
| 219 |
else: # Not streaming
|