Skip to content

Refactor stream handling to expose event labels #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ client.chat(
model: "deepseek-chat", # Required.
messages: [{ role: "user", content: "Hello!"}], # Required.
temperature: 0.7,
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
print chunk.dig("choices", 0, "delta", "content")
end
}
Expand Down Expand Up @@ -285,7 +285,7 @@ client.chat(
model: "llama3", # Required.
messages: [{ role: "user", content: "Hello!"}], # Required.
temperature: 0.7,
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
print chunk.dig("choices", 0, "delta", "content")
end
}
Expand All @@ -309,7 +309,7 @@ client.chat(
model: "llama3-8b-8192", # Required.
messages: [{ role: "user", content: "Hello!"}], # Required.
temperature: 0.7,
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
print chunk.dig("choices", 0, "delta", "content")
end
}
Expand Down Expand Up @@ -371,7 +371,7 @@ client.chat(
model: "gpt-4o", # Required.
messages: [{ role: "user", content: "Describe a character called Anna!"}], # Required.
temperature: 0.7,
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
print chunk.dig("choices", 0, "delta", "content")
end
}
Expand Down Expand Up @@ -457,7 +457,7 @@ You can stream it as well!
model: "gpt-4o",
messages: [{ role: "user", content: "Can I have some JSON please?"}],
response_format: { type: "json_object" },
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
print chunk.dig("choices", 0, "delta", "content")
end
}
Expand Down Expand Up @@ -542,7 +542,7 @@ client.responses.create(
parameters: {
model: "gpt-4o", # Required.
input: "Hello!", # Required.
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
if chunk["type"] == "response.output_text.delta"
print chunk["delta"]
$stdout.flush # Ensure output is displayed immediately
Expand Down Expand Up @@ -1163,7 +1163,7 @@ client.runs.create(
assistant_id: assistant_id,
max_prompt_tokens: 256,
max_completion_tokens: 16,
stream: proc do |chunk, _bytesize|
stream: proc do |chunk, _event|
if chunk["object"] == "thread.message.delta"
print chunk.dig("delta", "content", 0, "text", "value")
end
Expand Down
1 change: 1 addition & 0 deletions lib/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_relative "openai/messages"
require_relative "openai/runs"
require_relative "openai/run_steps"
require_relative "openai/stream"
require_relative "openai/vector_stores"
require_relative "openai/vector_store_files"
require_relative "openai/vector_store_file_batches"
Expand Down
29 changes: 1 addition & 28 deletions lib/openai/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,6 @@ def parse_json(response)
original_response
end

# Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
# For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
# be a data object or an error object as described in the OpenAI API documentation.
#
# @param user_proc [Proc] The inner proc to call for each JSON object in the chunk.
# @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON.
def to_json_stream(user_proc:)
parser = EventStreamParser::Parser.new

proc do |chunk, _bytes, env|
if env && env.status != 200
raise_error = Faraday::Response::RaiseError.new
raise_error.on_complete(env.merge(body: try_parse_json(chunk)))
end

parser.feed(chunk) do |_type, data|
user_proc.call(JSON.parse(data)) unless data == "[DONE]"
end
end
end

def conn(multipart: false)
connection = Faraday.new do |f|
f.options[:timeout] = @request_timeout
Expand Down Expand Up @@ -120,7 +99,7 @@ def configure_json_post_request(req, parameters)
req_parameters = parameters.dup

if parameters[:stream].respond_to?(:call)
req.options.on_data = to_json_stream(user_proc: parameters[:stream])
req.options.on_data = Stream.new(user_proc: parameters[:stream]).to_proc
req_parameters[:stream] = true # Necessary to tell OpenAI to stream.
elsif parameters[:stream]
raise ArgumentError, "The stream parameter must be a Proc or have a #call method"
Expand All @@ -129,11 +108,5 @@ def configure_json_post_request(req, parameters)
req.headers = headers
req.body = req_parameters.to_json
end

def try_parse_json(maybe_json)
JSON.parse(maybe_json)
rescue JSON::ParserError
maybe_json
end
end
end
50 changes: 50 additions & 0 deletions lib/openai/stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module OpenAI
class Stream
DONE = "[DONE]".freeze
private_constant :DONE

def initialize(user_proc:, parser: EventStreamParser::Parser.new)
@user_proc = user_proc
@parser = parser

# To be backwards compatible, we need to check how many arguments the user_proc takes.
@user_proc_arity =
case user_proc
when Proc
user_proc.arity.abs
else
user_proc.method(:call).arity.abs
end
end

def call(chunk, _bytes, env)
handle_http_error(chunk: chunk, env: env) if env && env.status != 200

parser.feed(chunk) do |event, data|
next if data == DONE
Copy link
Author

@ingemar ingemar May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be changed to check the event instead. Maybe that would be more elegant.
Example:

next if event == "done"


args = [JSON.parse(data), event].first(user_proc_arity)
user_proc.call(*args)
end
end

def to_proc
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to convert to a Proc because Faraday enforces the stream handler to be that.

method(:call).to_proc
end

private

attr_reader :user_proc, :parser, :user_proc_arity

def handle_http_error(chunk:, env:)
raise_error = Faraday::Response::RaiseError.new
raise_error.on_complete(env.merge(body: try_parse_json(chunk)))
end

def try_parse_json(maybe_json)
JSON.parse(maybe_json)
rescue JSON::ParserError
maybe_json
end
end
end
6 changes: 3 additions & 3 deletions spec/openai/client/chat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
describe "streaming" do
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
proc do |chunk, _event|
chunks << chunk
end
end
Expand Down Expand Up @@ -196,7 +196,7 @@ def call(chunk)
end
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
proc do |chunk, _event|
chunks << chunk
end
end
Expand Down Expand Up @@ -224,7 +224,7 @@ def call(chunk)
end
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
proc do |chunk, _event|
chunks << chunk
end
end
Expand Down
71 changes: 1 addition & 70 deletions spec/openai/client/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
context "streaming" do
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
proc do |chunk, _event|
chunks << chunk
end
end
Expand Down Expand Up @@ -120,75 +120,6 @@
end
end

describe ".to_json_stream" do
context "with a proc" do
let(:user_proc) { proc { |x| x } }
let(:stream) { OpenAI::Client.new.send(:to_json_stream, user_proc: user_proc) }

it "returns a proc" do
expect(stream).to be_a(Proc)
end

context "when called with a string containing a single JSON object" do
it "calls the user proc with the data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
stream.call(<<~CHUNK)
data: { "foo": "bar" }

#
CHUNK
end
end

context "when called with a string containing more than one JSON object" do
it "calls the user proc for each data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
expect(user_proc).to receive(:call).with(JSON.parse('{"baz": "qud"}'))

stream.call(<<~CHUNK)
data: { "foo": "bar" }

data: { "baz": "qud" }

data: [DONE]

#
CHUNK
end
end

context "when called with string containing invalid JSON" do
let(:chunk) do
<<~CHUNK
data: { "foo": "bar" }

data: NOT JSON

#
CHUNK
end

it "raise an error" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))

expect do
stream.call(chunk)
end.to raise_error(JSON::ParserError)
end
end

context "when called with JSON split across chunks" do
it "calls the user proc with the data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{ "foo": "bar" }'))
expect do
stream.call("data: { \"foo\":")
stream.call(" \"bar\" }\n\n")
end.not_to raise_error
end
end
end
end

describe ".parse_json" do
context "with a jsonl string" do
let(:body) { "{\"prompt\":\":)\"}\n{\"prompt\":\":(\"}\n" }
Expand Down
10 changes: 7 additions & 3 deletions spec/openai/client/responses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
describe "streaming" do
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
proc do |chunk, _event|
chunks << chunk
end
end
Expand All @@ -112,13 +112,15 @@
let(:cassette) { "responses stream without proc" }
let(:stream) do
Class.new do
attr_reader :chunks
attr_reader :chunks, :events

def initialize
@chunks = []
@events = []
end

def call(chunk)
def call(chunk, event)
@events << event
@chunks << chunk
end
end.new
Expand All @@ -132,6 +134,8 @@ def call(chunk)
.map { |chunk| chunk["delta"] }
.join
expect(output_text).to include("?")
expect(stream.events.first).to eq("response.created")
expect(stream.events.last).to eq("response.completed")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/openai/client/runs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
describe "streaming" do
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
proc do |chunk, _event|
chunks << chunk
end
end
Expand Down
Loading