Skip to main content

classify_service_error

Function classify_service_error 

Source
pub fn classify_service_error(
    status: u16,
    body: &str,
    message: impl Into<String>,
    retry_after: Option<f64>,
) -> Error
Expand description

Classify a non-success OpenAI-wire (Chat Completions / Responses) HTTP response into a granular Error.

The single point of truth for status/body interpretation, used by every endpoint in this crate (OpenAIChatCompletionClient::post and responses) and reused by agent-framework-azure (Azure OpenAI is wire-compatible for Chat Completions and Responses), so the two stay identical rather than drifting.

Mirrors upstream’s openai/_chat_client.py / _responses_client.py:

except BadRequestError as ex:
    if ex.code == "content_filter":
        raise OpenAIContentFilterException(...)
    raise ServiceResponseException(...)

for the content-filter case, and extends upstream’s exception hierarchyServiceInvalidAuthError / ServiceInvalidRequestError already exist in agent_framework.exceptions, even though today’s Python OpenAI client folds every other status (auth failures included) into that same generic ServiceResponseException — to also classify by HTTP status:

body is parsed leniently as JSON purely to look for the content-filter marker; a non-JSON or differently-shaped body just skips that check and falls through to the plain status-based classification, so this never panics or itself errors. message is used verbatim as the resulting error’s text (callers already format a provider-specific “OpenAI API error 400: …” string; this only picks the variant).