Mental model
A service error means the request could not complete reliably; a refusal means the model or application policy declined to provide requested content. Classify each outcome from transport status and typed response events, then give the user a truthful explanation and a recovery action that matches that state.
Theory
The application should classify outcomes from independent layers: HTTP status and connection state describe transport, typed response events describe model execution, and moderation fields describe a separate policy signal. A refusal can arrive over a successful connection; a disconnect can happen before the application knows whether generation completed. Preserve these facts separately so a visual state reflects evidence instead of guessing from response text.
Mechanism: keep the signal and the message separate
Chat interfaces often collapse every unsuccessful-looking turn into a red banner. That erases a crucial boundary. An HTTP 503, a dropped stream, or a provider failure is an operational problem. A model refusal is a deliberate content outcome. Moderation is another signal: a policy engine may flag input or output for review or filtering, and that flag is not itself a transport failure or necessarily a refusal.
Use protocol evidence rather than searching visible text for phrases such as “I can’t help with that.” OpenAI's Responses streaming reference defines response.failed for a failed response and response.refusal.done for finalized refusal text. A refusal output part is also typed as refusal. These events can arrive over a successful connection, so HTTP 200 alone does not establish that the answer is ordinary prose. Conversely, an error message in a network response should not be rendered as model-authored content.
Normalize provider-specific details at the API boundary into a small application outcome type. Preserve request ID, response ID when available, status code, provider error code, retry-after value, and whether the prompt remains safe to retry. The interface should not invent a cause when the stream closes before the final event. It can say that the connection ended before completion, keep the draft, and offer a deliberate retry or resume if the backend can establish the response state.
Outcome-specific recovery
| Signal | User-facing state | Recovery |
|---|---|---|
| HTTP 4xx except 429 | Request was rejected | Explain the actionable input or authorization issue; do not blindly retry |
| HTTP 429 with Retry-After | Rate limited | Preserve the draft and wait for the indicated delay before another attempt |
| HTTP 5xx or response.failed | Service could not complete the request | Retry only if the operation has an idempotency contract or a reconciliation step that proves replay is safe |
| Stream disconnect without a terminal event | Response status is unknown | Reconcile by request ID or response ID before sending again |
| response.refusal.done or a typed refusal part | Request declined | Explain the boundary and allow the user to revise the request |
| Moderation result marked for review | Application policy action | Show the policy-specific status; do not relabel it as a network failure |
RFC 9110 separates 4xx client errors from 5xx server errors; the two classes call for different user actions. A malformed or unauthorized request should return to the user for correction, while a server error describes the service's failure to fulfill a valid request. HTTP 429 is a separate rate-limit outcome under RFC 6585 and can carry Retry-After. Respect that delay, but do not make status alone a replay guarantee.
A refusal should not trigger an automatic retry with the same input: repeated attempts can hide the reason for the policy decision and consume capacity without changing the result. Offer a nearby safe reformulation only when the product can provide one accurately. For a transient service failure, retain user text and distinguish “try again” from “edit prompt.” If a request might have completed after a timeout, ask the server for its status first or use an idempotent retry key; blindly resending may create duplicate tool actions.
Moderation requires its own branch. OpenAI's moderation guide notes that a refusal can still be flagged when it discusses harmful content, and that moderation can return an error instead of scores. Therefore, a refusal string, moderation flag, and moderation-service failure are three different facts. Store them as separate fields in telemetry and apply the product's policy to each rather than mapping a numeric score directly to a user-facing refusal.
Worked example: do not blend rates
Over one day, a service accepts 1,000 user submissions. Nineteen fail operationally before a completed response: twelve receive a provider or HTTP 5xx failure and seven time out. The remaining 981 complete. Of those, 18 contain a refusal. The service-failure rate is 19 / 1,000 = 1.9%; the refusal rate is 18 / 981, about 1.83%.
If a dashboard calls all 37 outcomes “errors,” the team cannot tell whether to page the on-call engineer, improve availability, or review a refusal policy. Separate denominators matter: failures are measured against accepted requests, while refusals are measured among completed responses. A time window and request cohort belong beside each rate so retry storms do not masquerade as new user demand.
Accessible state changes
Use neutral language that tells the user what happened and what can happen next. Preserve the prompt in every path. Routine service errors and rate-limit notices should use a polite status message so they do not interrupt the user's current task. The W3C ARIA alert pattern is assertive and interruptive; reserve it for a genuinely urgent condition that requires immediate attention. WCAG's status-message guidance is relevant: a routine status should be programmatically determinable without requiring focus to move. Offer keyboard-operable retry and edit controls with names that describe their actions, not only their color or icon.
Avoid exposing raw provider stack traces or moderation internals in the public UI. Keep the low-level code in a traceable log keyed by request ID, and show a safe summary. This boundary supports support teams without teaching the interface to infer policy from arbitrary text. The human-AI interaction guidelines also emphasize communicating system capabilities and failure states in ways users can act on; an accurate label is part of keeping the user's mental model intact.
Failure modes and misconceptions
The most dangerous fallback is an optimistic retry that resubmits a request while the first attempt may still be executing. This is especially risky when a response can invoke tools or mutate external state. Reconcile request identity, make side effects idempotent, and provide a resume action when the provider exposes a durable response ID. For read-only generation, a retry may be acceptable, but retain the original draft and label it as a new attempt.
Another failure is deriving refusal from moderation alone. A moderation flag is a classification signal that needs an application decision; it may be attached to allowed content, a refusal, or an error state. Keep a typed record containing the moderation outcome separately from the rendering outcome. If moderation itself fails, fail according to the product's safety policy and tell the user that a safety check could not complete; do not claim the model refused.
Trade-offs
Typed event handling takes more adapter code than a single “success or error” branch, but it makes retry, observability, and accessible messaging accurate. A generic banner is cheaper to ship and easier to localize, yet it may encourage a retry that repeats a refusal or a tool action. Keep provider-specific parsing in one boundary module and pass a small stable outcome union to the interface; this limits coupling while preserving the distinctions the UI and telemetry need.
Quantitative model
Both rates are dimensionless proportions for one time window and request cohort. In the example, operational failures are 19 per 1,000 accepted requests; refusals are 18 per 981 completed responses. Keeping denominators separate prevents user-visible policy outcomes from masking infrastructure incidents.
Implementation pattern
This adapter distinguishes typed stream events and HTTP outcomes. A real client should also bind retries to the request ID and reconcile uncertain disconnects before creating a new request.
typescripttype StreamEvent =| { type: "response.refusal.done"; refusal: string }| { type: "response.failed"; response?: { error?: { code?: string | null } } }| { type: "response.completed"; response: { id: string } };type ReplayContract = "unsafe" | "idempotent" | "reconcile-first";type UiOutcome =| { kind: "refusal"; message: string }| { kind: "client-error"; status: number; code: string }| { kind: "rate-limit"; status: 429; retryAfterMs?: number; retryable: boolean; recovery: "wait-and-replay" | "reconcile-first" | "manual" }| { kind: "service-error"; status?: number; code: string; retryable: boolean; recovery: "replay" | "reconcile-first" | "manual" }| { kind: "completed"; responseId: string };const replayPlan = (contract: ReplayContract) =>contract === "idempotent"? { retryable: true, recovery: "replay" as const }: contract === "reconcile-first"? { retryable: true, recovery: "reconcile-first" as const }: { retryable: false, recovery: "manual" as const };export function classifyHttpFailure(status: number, replayContract: ReplayContract, retryAfterMs?: number): UiOutcome | null {if (status < 400) return null;const plan = replayPlan(replayContract);if (status === 429) {return {kind: "rate-limit", status: 429,...(retryAfterMs !== undefined ? { retryAfterMs } : {}),retryable: plan.retryable,recovery: plan.recovery === "replay" ? "wait-and-replay" : plan.recovery,};}if (status < 500) {return { kind: "client-error", status, code: "HTTP_" + status };}return { kind: "service-error", status, code: "HTTP_" + status, ...plan };}export function classifyStreamEvent(event: StreamEvent, replayContract: ReplayContract): UiOutcome {if (event.type === "response.refusal.done") return { kind: "refusal", message: event.refusal };if (event.type === "response.failed") {return { kind: "service-error", code: event.response?.error?.code ?? "response_failed", ...replayPlan(replayContract) };}return { kind: "completed", responseId: event.response.id };}export function liveRole(outcome: UiOutcome, urgentActionRequired = false): "status" | "alert" {const failure = outcome.kind !== "completed" && outcome.kind !== "refusal";return failure && urgentActionRequired ? "alert" : "status";}
The renderer can preserve the submitted prompt and show a retry only when the outcome's replay contract makes it safe. For reconcile-first outcomes, query the request or response state before enabling replay. A routine rate-limit or service notice should use role=status; pass urgentActionRequired only when an operator or user must act immediately. For a refusal, keep the event in the transcript and present an edit or safe alternative action instead of treating the refusal text as a failed HTTP request.
Decision scenario
A request times out after the provider accepted it, and the user has already seen the original prompt remain in the composer. Because the operation might have completed with a tool side effect, the interface should first query the request or response ID. If the provider confirms a transient failure, offer retry with the same idempotency key and keep the draft. If the response contains a refusal event, show the refusal in the transcript and let the user revise the request without an automatic retry.
Learning outcomes
- Distinguish a transport or provider failure from a typed model refusal.
- Choose a retry, reconciliation, or prompt-edit action from the actual outcome.
- Keep moderation, refusal, and availability telemetry in separate fields.
Further reading
The OpenAI streaming-event and moderation references describe typed refusal, failed-response, and moderation signals. RFC 9110 defines the 4xx and 5xx status classes, RFC 6585 describes rate limiting, and the W3C ARIA alert pattern clarifies announcement urgency. WCAG 2.2 provides the status-message accessibility context, and the human-AI interaction guidelines support clear communication of system behavior.