Skip to content

Faraday Streaming Requires faraday-net_http >= 3.0 Due to on_data Callback Signature #107

@darthrighteous

Description

@darthrighteous

There's a compatibility issue when using ruby_llm's streaming capabilities with versions of faraday-net_http older than 3.0.

Cause:
The RubyLLM::Streaming#to_json_stream method defines an on_data callback that expects three arguments: chunk, _bytes, and env. It uses env.status to check if the response status is 200.

  # lib/ruby_llm/streaming.rb
  proc do |chunk, _bytes, env|
    # ...
    if error_chunk?(chunk)
      handle_error_chunk(chunk, env)
    elsif env&.status != 200 # <-- Relies on env
      handle_failed_response(chunk, buffer, env)
    else
      yield handle_sse(chunk, parser, env, &block)
    end
  end

However, in faraday-net_http versions prior to 3.0 (e.g., this line), the on_data callback was invoked with only two arguments (chunk, size):

  # Older faraday-net_http code
  env[:request].on_data.call(chunk, size)

This mismatch causes env to be nil within the to_json_stream callback when using older adapter versions, leading to successful stream chunks being incorrectly handled as errors.

Resolution:

This issue is resolved by ensuring faraday-net_http version 3.0 or later is used.

  # ruby_llm.gemspec
  spec.add_dependency 'faraday-net_http', '~> 3'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions