Skip to content

Fix ArgumentError in Stream#call with faraday-net_http 2.1.0 #618

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

Conversation

daikimiura
Copy link

@daikimiura daikimiura commented Aug 17, 2025

Fixes #617

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?

Problem

After upgrading from ruby-openai 8.1 to 8.2, users encountered an ArgumentError when using streaming with Faraday 2.1.0:

ArgumentError: wrong number of arguments (given 2, expected 3)

The error occurs because Faraday 2.1.0 calls the on_data callback with only 2 arguments (chunk, size), but the new Stream#call method expects 3 arguments (chunk, _bytes, env).

Root Cause

In version 8.2.0, streaming functionality was refactored from an inline Proc in HTTP#to_json_stream to a dedicatedStream class (PR #589). This introduced a breaking change:

Before 8.2.0 (worked fine):

  • Used a Proc: proc do |chunk, _bytes, env|
  • Ruby Procs are flexible with argument counts - when called with fewer arguments, missing parameters become nil
  • Faraday calling with 2 arguments worked without errors

After 8.2.0 (breaks):

  • Uses a method: def call(chunk, _bytes, env)
  • Ruby methods strictly enforce argument counts
  • Faraday calling with 2 arguments raises ArgumentError

Faraday's Behavior

In faraday-net_http v2.1.0, the callback is invoked with only 2 parameters:

# https://github.com/lostisland/faraday-net_http/blob/v2.1.0/lib/faraday/adapter/net_http.rb#L112
env[:request].on_data.call(chunk, size)

Solution

Make the env parameter optional with a default value of nil:

def call(chunk, _bytes, env = nil)

This maintains backward compatibility with different Faraday versions while preserving the same functionality.

@daikimiura daikimiura marked this pull request as draft August 17, 2025 07:26
@daikimiura daikimiura marked this pull request as ready for review August 17, 2025 07:27
@daikimiura daikimiura changed the title Fix ArgumentError in Stream#call with Faraday 2.1.0 Fix ArgumentError in Stream#call with faraday-net_http 2.1.0 Aug 17, 2025
Copy link

@fabioxgn fabioxgn left a comment

Choose a reason for hiding this comment

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

Thank you! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stream broken in v8.2 with Faraday/NetHTTP adapter
4 participants