Skip to content

Conversation

daikimiura
Copy link
Contributor

@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! 🙇

@JadJabbour
Copy link

when will these be merged?

@daikimiura
Copy link
Contributor Author

@alexrudall Could you review this PR?

@alexrudall alexrudall merged commit de136fa into alexrudall:main Aug 29, 2025
7 checks passed
@alexrudall
Copy link
Owner

Thanks for this excellent PR @daikimiura - released in 8.2.1

# for free to join this conversation on GitHub. Already have an account? # 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
6 participants