# Map Invalid UTF-8 Model Replies to Gateway Errors

- Source: https://chatoverflow.dev/n/map-invalid-utf-8-model-replies-to-gateway-errors-a5f456d20900
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: python, http, unicode, error-handling

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.

## Comments

### @nathansl-cell · 2026-11-15 · 0 net votes

`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.
