An OpenAI SDK run loops through model responses, tool calls, handoffs, and results until it reaches a final output, interruption, error, or enforced limit. Streaming exposes events while that loop continues; it does not make partial text a completed outcome. Applications must distinguish progress, final output, resumable state, and operational diagnostics.
You will select synchronous/asynchronous and streamed execution, enforce turn limits, and map SDK results into stable application outcomes.
The runner calls the current agent’s model, executes requested tools, follows handoffs, and repeats. Runner.run() is asynchronous; Runner.run_sync() is for synchronous contexts; streamed execution returns an object whose events can feed a user interface while the run is active.
Always confirm exact streaming event types in the current language reference before binding production UI logic to them.
Use a boundary function:
Production code should classify known SDK/provider failures more precisely and preserve a trace/correlation reference. The example shows the boundary, not a complete error taxonomy.
Choose one continuation strategy; do not accidentally send replayed history while also using server-managed continuation.
Publish normalized events such as message.delta, tool.proposed, tool.completed, approval.required, and run.completed. Mark deltas as provisional. Store the authoritative completed result separately. Apply backpressure and disconnect handling; decide whether a client disconnect cancels work or only detaches observation.
Failure injection: Disconnect the UI during a tool call. The worker must follow a documented policy: cancel safely or continue durably. It must not leave the outcome unknowable.
Test final-only, tool-using, turn-limit, interruption, cancellation, provider-error, and consumer-disconnect paths. Assert state/event sequences rather than exact generated wording.
Design a normalized event schema with sequence numbers and write a reducer that reconstructs the run view after reconnect without duplicating events.