TECHNOLOGY

Negotiate an inference response media type explicitly

In this article
THE QUICK TAKE

Negotiate the response representation and verify the received media type before choosing a parser.

An inference gateway can return a JSON document, an event stream, or a structured error from the same route. A consumer that assumes every successful response is JSON will fail late when the server selected streaming. Content negotiation makes this expectation visible before generation begins, while response validation catches intermediaries that return something else.

State the representation the client understands

The request’s Content-Type describes its own body. Accept expresses preferences for the response. These fields are independent: a JSON request can legitimately produce an event stream. Build the accepted media types from actual parser support, including any deliberate fallback. An overly broad wildcard tells the server that representations are acceptable even if the application cannot process them. Omitting the field can have the same practical effect.

Keep API parameters in the same contract

Some inference APIs select streaming through a JSON parameter rather than through HTTP negotiation. Check that parameter together with the headers. If the parameter asks for events while the parser expects a completed document, changing Accept alone may not fix the mismatch. Record the endpoint’s documented precedence and reject contradictory local settings before sending. A generic HTTP standard does not require a model API to support every media type a client names.

Inspect the received type before parsing

Capture final status and Content-Type before handing bytes to the decoder. Check the media type with its permitted parameters rather than comparing an entire header string literally. A reverse proxy might return an HTML error page, and an API may provide problem details for a rejected request. Both deserve explicit handling. Avoid sending the response through a JSON parser merely because the original request body used JSON.

Test alternative outcomes independently

Exercise the supported completed response, supported stream and an unsupported accepted type with synthetic inputs. Include an error generated by the application and another generated by the gateway if the test environment allows it. The latter often exposes missing type checks. When the endpoint rejects negotiation, inspect its documented error behavior instead of repeatedly submitting the same incompatible request. Keep the stream-completion test distinct from the initial media-type check.

Keep a parser-selection record in the fixture output: requested representation, received media type and selected decoder. That small observation makes a gateway-generated page easy to distinguish from malformed model JSON. It also avoids treating every parser exception as evidence that generation itself failed after admission.

Keep caches aware of selection

If a cacheable resource such as a model catalog changes representation according to request headers, its cache policy must account for those headers. Generated private responses need their own storage policy; content negotiation does not make them safe to share. Acceptance means that each supported response reaches the intended parser, unsupported representations fail clearly, and error bodies preserve their diagnostic meaning. It does not mean that streaming and completed generation have equal latency or identical buffering behavior.

Primary references

HTTP Semantics. Defines request and response representation metadata and negotiation.

Problem Details for HTTP APIs. Defines the media type and structure for machine-readable HTTP API errors.

HTTP Caching. Explains cache selection when request headers change a representation.

Related reading: Golden inference API fixtures: test the contract rather than prose; Reading an OpenRouter streaming response incrementally.

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