The error envelope
Every error response uses the same shape. The error detail is nested inside anerror object, and the response carries a request_id alongside it.
Example error response
/v1/messages uses the Anthropic error shape, not the envelope above. The body carries a top-level "type": "error", and error holds message and type only - there is no param or code. request_id is present on every status, though only some carry a req_ prefix - the 401 does not. See Anthropic SDK compatibility.Error categories
Use the following table to understand the various error categories.Error code reference
Use the following table to understand and troubleshoot errors programmatically.Responses that do not use the envelope
Three paths do not use this envelope. On two of them the body is still JSON, buterror is a string instead of an object. On the third the body is not JSON at all. All three omit request_id.
Handle all three shapes before you read error.message.
Malformed image data
Returned by vision requests when the API cannot decode the image. HTTP 400.A forced tool_choice that the model does not satisfy
Returned by function calling requests when you force a specific function and the model produces no valid call. HTTP 400.
error_model_output. It carries the text the model produced instead of the tool call. Note also that error_type holds a sentence here, not a code such as invalid_request_error.
A retired model on /v1/chat/completions
Returned when the requested model has been deprecated and removed. HTTP 410.
This response is not JSON. It is served as content-type: text/plain; charset=utf-8 with a plain sentence as the whole body:
content-type, or catch the decode failure, before you read the body.
Official SDKs already survive this. The OpenAI Python SDK raises
openai.APIStatusError with status_code 410 and the sentence in .body. The failure hits clients that call .json() on the raw response themselves - requests, for example, raises requests.exceptions.JSONDecodeError./v1/responses and /v1/messages does return JSON.