Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ jobs:
files: ./coverage/coverage.xml
fail_ci_if_error: false

- name: Run the test suite with lower Faraday versions
run: FARADAY_VERSION=1.10.3 bundle install && bundle exec rspec
env:
OLLAMA_API_BASE: http://localhost:11434/v1 # dummy

publish:
name: Build + Publish
needs: test
Expand Down
57 changes: 46 additions & 11 deletions lib/ruby_llm/streaming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
accumulator = StreamAccumulator.new

connection.post stream_url, payload do |req|
req.options.on_data = handle_stream do |chunk|
accumulator.add chunk
block.call chunk
if req.options.respond_to?(:on_data)
# Handle Faraday 2.x streaming with on_data method
req.options.on_data = handle_stream do |chunk|
accumulator.add chunk
block.call chunk
end
else
# Handle Faraday 1.x streaming with :on_data key
req.options[:on_data] = handle_stream do |chunk|
accumulator.add chunk
block.call chunk

Check warning on line 25 in lib/ruby_llm/streaming.rb

View check run for this annotation

Codecov / codecov/patch

lib/ruby_llm/streaming.rb#L24-L25

Added lines #L24 - L25 were not covered by tests
end
end
end

Expand All @@ -29,19 +38,45 @@

private

def to_json_stream(&block)
def to_json_stream(&)
buffer = String.new
parser = EventStreamParser::Parser.new

proc do |chunk, _bytes, env|
RubyLLM.logger.debug "Received chunk: #{chunk}"
create_stream_processor(parser, buffer, &)
end

if error_chunk?(chunk)
handle_error_chunk(chunk, env)
elsif env&.status != 200
handle_failed_response(chunk, buffer, env)
def create_stream_processor(parser, buffer, &)
if Faraday::VERSION.start_with?('1')
# Faraday 1.x: on_data receives (chunk, size)
legacy_stream_processor(parser, &)
else
# Faraday 2.x: on_data receives (chunk, bytes, env)
stream_processor(parser, buffer, &)
end
end

def process_stream_chunk(chunk, parser, _env, &)
RubyLLM.logger.debug "Received chunk: #{chunk}"

if error_chunk?(chunk)
handle_error_chunk(chunk, nil)
else
yield handle_sse(chunk, parser, nil, &)
end
end

def legacy_stream_processor(parser, &block)
proc do |chunk, _size|
process_stream_chunk(chunk, parser, nil, &block)

Check warning on line 70 in lib/ruby_llm/streaming.rb

View check run for this annotation

Codecov / codecov/patch

lib/ruby_llm/streaming.rb#L69-L70

Added lines #L69 - L70 were not covered by tests
end
end

def stream_processor(parser, buffer, &block)
proc do |chunk, _bytes, env|
if env&.status == 200
process_stream_chunk(chunk, parser, env, &block)
else
yield handle_sse(chunk, parser, env, &block)
handle_failed_response(chunk, buffer, env)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions ruby_llm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ Gem::Specification.new do |spec|
# Runtime dependencies
spec.add_dependency 'base64'
spec.add_dependency 'event_stream_parser', '~> 1'
spec.add_dependency 'faraday', '~> 2'
spec.add_dependency 'faraday-multipart', '~> 1'
spec.add_dependency 'faraday-net_http', '~> 3'
spec.add_dependency 'faraday-retry', '~> 2'
spec.add_dependency 'faraday', ENV['FARADAY_VERSION'] || '>= 1.10.0'
spec.add_dependency 'faraday-multipart', '>= 1'
spec.add_dependency 'faraday-net_http', '>= 1'
spec.add_dependency 'faraday-retry', '>= 1'
spec.add_dependency 'marcel', '~> 1.0'
spec.add_dependency 'zeitwerk', '~> 2'
end