feat: log provider errors to console for better insights (#23379)
* fix: log provider errors that were silently swallowed * Update main.py * fix: wrap non-JSON SSE error responses in JSON so middleware handles them
This commit is contained in:
1 parent
67023037f8
commit
4292358bd5
3 files changed
+23
-1
No files matched your search
@@ -1797,7 +1797,7 @@ async def chat_completion(
|
|||||||
finally:
|
finally:
|
||||||
raise # re-raise to ensure proper task cancellation handling
|
raise # re-raise to ensure proper task cancellation handling
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.debug(f'Error processing chat payload: {e}')
|
log.error('Error processing chat payload: %s', e)
|
||||||
if metadata.get('chat_id') and metadata.get('message_id'):
|
if metadata.get('chat_id') and metadata.get('message_id'):
|
||||||
# Update the chat message with the error
|
# Update the chat message with the error
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1195,6 +1195,25 @@ async def generate_chat_completion(
|
|||||||
|
|
||||||
# Check if response is SSE
|
# Check if response is SSE
|
||||||
if 'text/event-stream' in r.headers.get('Content-Type', ''):
|
if 'text/event-stream' in r.headers.get('Content-Type', ''):
|
||||||
|
# If the provider returned an error status with SSE content-type,
|
||||||
|
# read the body and return a proper error response instead of
|
||||||
|
# streaming the error back (which hides the error from logs).
|
||||||
|
if r.status >= 400:
|
||||||
|
error_body = await r.text()
|
||||||
|
log.error(
|
||||||
|
'Provider returned HTTP %d with SSE content-type: %s',
|
||||||
|
r.status,
|
||||||
|
error_body[:1000],
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
error_json = json.loads(error_body)
|
||||||
|
return JSONResponse(status_code=r.status, content=error_json)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=r.status,
|
||||||
|
content={"error": {"message": error_body, "code": r.status}},
|
||||||
|
)
|
||||||
|
|
||||||
streaming = True
|
streaming = True
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
stream_wrapper(r, content_handler=stream_chunks_handler),
|
stream_wrapper(r, content_handler=stream_chunks_handler),
|
||||||
|
|||||||
@@ -3077,6 +3077,8 @@ async def non_streaming_chat_response_handler(response, ctx):
|
|||||||
else:
|
else:
|
||||||
error = str(error)
|
error = str(error)
|
||||||
|
|
||||||
|
log.error('Provider returned error (non-streaming): %s', error)
|
||||||
|
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
metadata['message_id'],
|
metadata['message_id'],
|
||||||
@@ -3645,6 +3647,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
if not choices:
|
if not choices:
|
||||||
error = data.get('error', {})
|
error = data.get('error', {})
|
||||||
if error:
|
if error:
|
||||||
|
log.error('Provider returned error (streaming): %s', error)
|
||||||
try:
|
try:
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
|
|||||||
Reference in new issue
Block a user