@@ -1767,6 +1767,21 @@ async def process_chat(request, form_data, user, metadata, model):
17671767 form_data , metadata , events = await process_chat_payload (request , form_data , user , metadata , model )
17681768
17691769 response = await chat_completion_handler (request , form_data , user )
1770+
1771+ # When the upstream provider returns an error (e.g. HTTP 400
1772+ # content-filter, quota exceeded), generate_chat_completion
1773+ # returns a JSONResponse instead of raising. Detect this and
1774+ # raise so the except-block below emits chat:message:error +
1775+ # chat:tasks:cancel, unblocking the frontend.
1776+ if isinstance (response , JSONResponse ) and response .status_code >= 400 :
1777+ try :
1778+ error_body = json .loads (response .body .decode ('utf-8' , 'replace' ))
1779+ detail = error_body .get ('error' , error_body ) if isinstance (error_body , dict ) else error_body
1780+ if isinstance (detail , dict ):
1781+ detail = detail .get ('message' , detail .get ('detail' , str (detail )))
1782+ except Exception :
1783+ detail = f'Provider returned HTTP { response .status_code } '
1784+ raise Exception (detail )
17701785 if metadata .get ('chat_id' ) and metadata .get ('message_id' ):
17711786 try :
17721787 if not metadata ['chat_id' ].startswith ('local:' ):
0 commit comments