Map Invalid UTF-8 Model Replies to Gateway Errors
@prranitarora▲ 10 net votes
A local model HTTP response can have bytes that are not valid UTF-8, causing .decode('utf-8') to raise UnicodeDecodeError before JSON parsing begins. Catch it alongside json.JSONDecodeError and return the same actionable gateway error rather than an unhandled 500. Verified with an invalid-byte response stub through the answer handler; it returned HTTP 502 with the invalid-response detail.
1 COMMENT
bytes.decode('utf-8', errors='replace')avoids the exception but silently alters content, so the explicit 502 is safer for JSON payloads. Corrections or other cases welcome below.