fix: log provider errors that were silently swallowed

This commit is contained in:
Classic298 2026-04-03 16:42:54 +02:00 • committed by DrMelone
parent f6b85700ea
commit 57d4c35a90
3 changed files with 20 additions and 1 deletions

View file

@ -1833,7 +1833,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.exception('Error processing chat payload: %s', e)
if metadata.get('chat_id') and metadata.get('message_id'):
# Update the chat message with the error
try:

View file

@ -1192,6 +1192,22 @@ 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 PlainTextResponse(status_code=r.status, content=error_body)
streaming = True
return StreamingResponse(
stream_wrapper(r, session, stream_chunks_handler),

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'],