Skip to content

Commit d11c731

Browse files
authored
[Gemspec] - Allow lower faraday versions (#173)
# Description In reference to issue: #172 - this CR exists to allow for applications requiring different, lower Faraday versions to integrate with this Gem. # Changes Made 1. Updated the gemspec for Faraday and its dependencies to allow use of lower versions in case of a conflict. 2. Updated CICD.yml workflow to validate that lower Faraday versions work. 3. Specs failed on lower Faraday versions so I updated `lib/ruby_llm/streaming.rb` to allow for legacy Faraday versions to stream responses as well. Could use feedback definitely in case this breaks logic in a way you all are uncomfy with. # Testing I ran manually rspec with Faraday Version 2.13.1 and 1.10.3, here are the results prior to CI/CD integration in this repo: | Faraday Version | Validate Gem Install | Test Results | |----------|:-------------:|------:| | 1.10.3 | ![Screenshot 2025-05-14 at 12 52 57 PM](https://github.com/user-attachments/assets/5233a968-c37f-4de3-9ef0-bdaad17a3a2e) | ![Screenshot 2025-05-14 at 12 53 03 PM](https://github.com/user-attachments/assets/b21f830f-732a-4aef-94fa-5db07a2be8f8) | | 2.13.1 | ![Screenshot 2025-05-14 at 12 52 39 PM](https://github.com/user-attachments/assets/78c81a09-0bed-4f10-bc72-c8e76d16df08) | ![Screenshot 2025-05-14 at 12 52 46 PM](https://github.com/user-attachments/assets/ded40e55-7296-49a8-b4c8-b11a49ad6726) |
1 parent 8b16d38 commit d11c731

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

.github/workflows/cicd.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ jobs:
6161
files: ./coverage/coverage.xml
6262
fail_ci_if_error: false
6363

64+
- name: Run the test suite with lower Faraday versions
65+
run: FARADAY_VERSION=1.10.3 bundle install && bundle exec rspec
66+
env:
67+
OLLAMA_API_BASE: http://localhost:11434/v1 # dummy
68+
6469
publish:
6570
name: Build + Publish
6671
needs: test

lib/ruby_llm/streaming.rb

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ def stream_response(connection, payload, &block)
1212
accumulator = StreamAccumulator.new
1313

1414
connection.post stream_url, payload do |req|
15-
req.options.on_data = handle_stream do |chunk|
16-
accumulator.add chunk
17-
block.call chunk
15+
if req.options.respond_to?(:on_data)
16+
# Handle Faraday 2.x streaming with on_data method
17+
req.options.on_data = handle_stream do |chunk|
18+
accumulator.add chunk
19+
block.call chunk
20+
end
21+
else
22+
# Handle Faraday 1.x streaming with :on_data key
23+
req.options[:on_data] = handle_stream do |chunk|
24+
accumulator.add chunk
25+
block.call chunk
26+
end
1827
end
1928
end
2029

@@ -29,19 +38,45 @@ def handle_stream(&block)
2938

3039
private
3140

32-
def to_json_stream(&block)
41+
def to_json_stream(&)
3342
buffer = String.new
3443
parser = EventStreamParser::Parser.new
3544

36-
proc do |chunk, _bytes, env|
37-
RubyLLM.logger.debug "Received chunk: #{chunk}"
45+
create_stream_processor(parser, buffer, &)
46+
end
3847

39-
if error_chunk?(chunk)
40-
handle_error_chunk(chunk, env)
41-
elsif env&.status != 200
42-
handle_failed_response(chunk, buffer, env)
48+
def create_stream_processor(parser, buffer, &)
49+
if Faraday::VERSION.start_with?('1')
50+
# Faraday 1.x: on_data receives (chunk, size)
51+
legacy_stream_processor(parser, &)
52+
else
53+
# Faraday 2.x: on_data receives (chunk, bytes, env)
54+
stream_processor(parser, buffer, &)
55+
end
56+
end
57+
58+
def process_stream_chunk(chunk, parser, _env, &)
59+
RubyLLM.logger.debug "Received chunk: #{chunk}"
60+
61+
if error_chunk?(chunk)
62+
handle_error_chunk(chunk, nil)
63+
else
64+
yield handle_sse(chunk, parser, nil, &)
65+
end
66+
end
67+
68+
def legacy_stream_processor(parser, &block)
69+
proc do |chunk, _size|
70+
process_stream_chunk(chunk, parser, nil, &block)
71+
end
72+
end
73+
74+
def stream_processor(parser, buffer, &block)
75+
proc do |chunk, _bytes, env|
76+
if env&.status == 200
77+
process_stream_chunk(chunk, parser, env, &block)
4378
else
44-
yield handle_sse(chunk, parser, env, &block)
79+
handle_failed_response(chunk, buffer, env)
4580
end
4681
end
4782
end

ruby_llm.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ Gem::Specification.new do |spec|
3636
# Runtime dependencies
3737
spec.add_dependency 'base64'
3838
spec.add_dependency 'event_stream_parser', '~> 1'
39-
spec.add_dependency 'faraday', '~> 2'
40-
spec.add_dependency 'faraday-multipart', '~> 1'
41-
spec.add_dependency 'faraday-net_http', '~> 3'
42-
spec.add_dependency 'faraday-retry', '~> 2'
39+
spec.add_dependency 'faraday', ENV['FARADAY_VERSION'] || '>= 1.10.0'
40+
spec.add_dependency 'faraday-multipart', '>= 1'
41+
spec.add_dependency 'faraday-net_http', '>= 1'
42+
spec.add_dependency 'faraday-retry', '>= 1'
4343
spec.add_dependency 'marcel', '~> 1.0'
4444
spec.add_dependency 'zeitwerk', '~> 2'
4545
end

0 commit comments

Comments
 (0)