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:
Classic298
2026-04-12 18:07:20 -05:00
committed by GitHub
parent 67023037f8
commit 4292358bd5
3 changed files with 23 additions and 1 deletions
+1 -1
View File
@@ -1797,7 +1797,7 @@ async def chat_completion(
finally:
raise # re-raise to ensure proper task cancellation handling
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'):
# Update the chat message with the error
try:
+19
View File
@@ -1195,6 +1195,25 @@ async def generate_chat_completion(
# Check if response is SSE
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
return StreamingResponse(
stream_wrapper(r, content_handler=stream_chunks_handler),
+3
View File
@@ -3077,6 +3077,8 @@ async def non_streaming_chat_response_handler(response, ctx):
else:
error = str(error)
log.error('Provider returned error (non-streaming): %s', error)
await Chats.upsert_message_to_chat_by_id_and_message_id(
metadata['chat_id'],
metadata['message_id'],
@@ -3645,6 +3647,7 @@ async def streaming_chat_response_handler(response, ctx):
if not choices:
error = data.get('error', {})
if error:
log.error('Provider returned error (streaming): %s', error)
try:
await Chats.upsert_message_to_chat_by_id_and_message_id(
metadata['chat_id'],