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
+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),