TECHNOLOGY

An API returned HTML: status, media type and JSON parsing are separate checks

In this article
THE QUICK TAKE

A JSON decoding error explains the representation failure; the response status and headers may reveal the earlier operational failure.

A JSON endpoint can have a non-JSON failure path

An AI client may expect a JSON error object but receive an HTML page from a gateway or routing layer. Calling the JSON parser immediately can replace the useful HTTP context with a generic decoding exception.

A proposed response handler keeps the status and selected response metadata before interpreting the body. It treats a representation mismatch as additional evidence about the failure path, rather than assuming that the model server itself produced the page or that the submitted request had invalid JSON.

Media type describes the representation

Content-Type identifies the returned media type, with parameters such as character encoding where applicable. It is distinct from content encoding. Comparing the entire header to one literal string can reject a legitimate value carrying supported parameters.

Define the accepted success and error representations for the actual API. A JSON-related media type does not establish that the bytes are syntactically valid JSON or that the decoded object satisfies the application’s schema. Media-type recognition and parsing therefore remain separate steps.

Decoding success does not imply operation success

Requests explicitly documents that a successful json method call does not establish a successful HTTP response. A failed request can carry perfectly valid JSON error details. Conversely, an unexpected empty or malformed body can cause decoding to fail.

The client needs both transport-status handling and representation handling. It should preserve a known server error when a later decoder fails, instead of reporting only that a character was unexpected. That separation makes a gateway incident distinguishable from a response-schema regression.

Client libraries expose different exception boundaries

HTTPX documents status checking and request failures as distinct exception paths, with request and response context available for status errors. An application’s wrapper decides how much of that context survives into its own error abstraction.

Avoid collapsing every exception into an empty model answer. A user-facing tool can present a concise service failure while retaining safe diagnostic metadata for the operation. Its retry policy should follow the actual operation and failure category, not the mere presence of a parsing exception.

Bound diagnostic handling of unexpected bodies

An HTML error page may contain content unsuitable for raw display in an application. A proposed diagnostic path retains a bounded, appropriately escaped excerpt only when needed and avoids copying request secrets or complete response content into routine records.

The review cases are documented JSON failures, HTML intermediary failures, malformed bodies and expected successful responses. Their purpose is to verify that status, media type, decoding and application interpretation remain distinct enough to explain what actually failed.

Primary references

HTTP Content-Type reference.

Requests JSON response behavior.

HTTPX response and exception handling.

Related explanations: API contract changes; Operation identity.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles