openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Background responses failures lack a stable code/name that maps to an exception class

Open adnanboz opened this issue 6 months ago • 3 comments

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • [x] This is a feature request for the Python library

Describe the feature or improvement you're requesting

When the Responses API runs in background mode (background=True) and a run terminates in status="failed" (or status="incomplete"), the SDK does not surface a stable, machine-readable identifier that callers can map to an existing exception class.

The failure payload looks like:

{
  "status": "failed",
  "error": { "code": "server_error", "message": "..." },
  "incomplete_details": null
}

The HTTP poll itself returns 200 OK, so the SDK's status-code dispatcher is never invoked and no typed exception is raised. The only signal of what kind of failure occurred is the free-form error.code string, which:

  • is not documented as a stable enum,
  • does not correspond 1:1 to any SDK exception class (server_error ≠ InternalServerError, rate_limit_exceeded ≠ RateLimitError, etc.),
  • is not guaranteed to be present or non-null for every failure mode (e.g. incomplete runs surface their reason in incomplete_details.reason instead, with yet another vocabulary),
  • and may carry values not enumerated anywhere the SDK or callers can rely on.

What exists today (synchronous mode)

For synchronous calls there is a clean, code-driven mapping in openai/_client.py keyed on HTTP status code:

def _make_status_error(self, err_msg, *, body, response):
    data = body.get("error", body) if is_mapping(body) else body
    if response.status_code == 400: return BadRequestError(...)
    if response.status_code == 401: return AuthenticationError(...)
    if response.status_code == 403: return PermissionDeniedError(...)
    if response.status_code == 404: return NotFoundError(...)
    if response.status_code == 409: return ConflictError(...)
    if response.status_code == 422: return UnprocessableEntityError(...)
    if response.status_code == 429: return RateLimitError(...)
    if response.status_code >= 500: return InternalServerError(...)
    return APIStatusError(...)

So the synchronous path has a fully deterministic, documented contract: HTTP code → exception class. The background path has no equivalent — neither in the response body (no stable code) nor in the SDK (no dispatcher).

Why this matters

In synchronous mode the SDK gives us a clean contract. In background mode the same root cause surfaces only as an opaque string with no contract. Any retry/backoff logic built around isinstance(e, InternalServerError) or isinstance(e, RateLimitError) simply cannot work for background runs, even though those are exactly the conditions where retry would be most useful.

Suggested feature

Either:

  1. Add a stable, documented field to background-run failure payloads (e.g. error.type or error.class) whose values directly correspond to existing SDK exception classes — e.g. internal_server_error, rate_limit_error, bad_request_error, content_filter_error, etc. The SDK can then mechanically map this field to the right exception class, mirroring what _make_status_error does today for HTTP status codes.

  2. Or: publish a documented, versioned mapping from each possible error.code / incomplete_details.reason value to the SDK exception class (or "no class") and to a retryable flag. Treat it as part of the public API contract.

Either approach gives background runs the same deterministic contract synchronous calls already enjoy.

Environment

  • openai Python SDK version: <fill in>
  • API: POST /v1/responses with background=true, polled via GET /v1/responses/{id}

Additional context

No response

adnanboz avatar May 08 '26 13:05 adnanboz

@adnanboz , I would like to work on this

DhineshPonnarasan avatar May 09 '26 11:05 DhineshPonnarasan

@adnanboz I would like to contribute on this

Zem-0 avatar May 10 '26 18:05 Zem-0

A practical workaround until the SDK/API exposes a stable enum is to treat a terminal background response as a second error-dispatch layer after polling, rather than relying on the HTTP status from the poll request.

For example, keep the poll loop boring, then normalize status, error.code, and incomplete_details.reason into your own small internal enum:

RETRYABLE_BACKGROUND_CODES = {
    "server_error",
    "rate_limit_exceeded",
    "temporarily_unavailable",
}

NON_RETRYABLE_BACKGROUND_CODES = {
    "invalid_request_error",
    "authentication_error",
    "permission_denied",
    "content_filter",
}

def classify_background_failure(response):
    if response.status == "failed":
        code = getattr(getattr(response, "error", None), "code", None)
        if code in RETRYABLE_BACKGROUND_CODES:
            return "retryable"
        if code in NON_RETRYABLE_BACKGROUND_CODES:
            return "non_retryable"
        return "unknown_failed"

    if response.status == "incomplete":
        reason = getattr(getattr(response, "incomplete_details", None), "reason", None)
        if reason in {"max_output_tokens", "content_filter"}:
            return "non_retryable"
        return "unknown_incomplete"

    return None

Then only retry retryable, log/alert on unknown_*, and include the original response.id, status, error.code, and incomplete_details.reason in telemetry. That keeps production behavior deterministic even though the SDK currently cannot raise InternalServerError, RateLimitError, etc. from a successful GET /v1/responses/{id} poll.

I agree with the feature request: a documented mapping to SDK exception classes or a stable retryable/error.type field would be cleaner than every client maintaining this adapter.

diva0078 avatar May 11 '26 08:05 diva0078