Skip to content
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation
Theme

Connect

Connect to a persistent Responses API WebSocket. Send response.create events to create responses and response.steer events to queue user input containing text, images, or files for a continuation. Receive response stream events and steering acceptance, pending, or failure events over the socket.

WebSocket wss://api.openai.com/v1/responses

Connect

require "openai"
require "async"

client = OpenAI::Client.new
# Run the deadline in the same scheduler as the WebSocket and its cleanup.
Sync do |task|
  task.with_timeout(60) do
    # The connection block closes the socket on completion or error.
    client
      .beta
      .responses
      .connect(
        request_options: {extra_headers: {"OpenAI-Beta" => "responses_multi_agent=v1"}}
      ) do |connection|
        connection.send_event(type: :"response.create", model: "gpt-6-astra", input: "Say hello in one sentence.")
        # A partial answer or an early close must not count as success.
        completed = false
        connection.each do |event|
          case event.type.to_s
          when "response.output_text.delta", "response.refusal.delta"
            print(event.delta)
            $stdout.flush
          when "response.completed"
            completed = true
            puts
            break
          when "error"
            raise "Responses request failed: #{event.error.message}"
          when "response.failed", "response.incomplete"
            raise "Responses request failed: #{event.type}"
          end
        end

        raise "Connection closed before response.completed" unless completed
      end
  end
end