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.responses.connect do |connection|
      connection.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