alexrudall / anthropic

Anthropic (Claude) API + Ruby! 🤖🌌
MIT License
108 stars 28 forks source link

Streaming error mishandled in http.rb #20

Open strukturedkaos opened 3 months ago

strukturedkaos commented 3 months ago

Line 154 in http.rb on env is nil on valid requests. This causes merge to error on line 157

 return unless env&.status != 200

Example:

          response = anthropic_client.messages(
            parameters: {
              temperature: 0.7,
              model: model,
              messages: message_data
              max_tokens: 100,
              preprocess_stream: :text,
              stream: proc do |incremental_response, delta|
                next if delta.blank?
                Rails.logger.info "Incremental response: #{incremental_response}"
                Rails.logger.info "Delta: #{delta}"
              end
            })  
alexrudall commented 3 months ago

Thanks, @strukturedkaos. Have you seen this occur @swombat ?

redders123 commented 3 months ago

Any chance of this being fixed in the next few days? We can't use the streaming api right now.

leo-quimbee commented 3 months ago

Also getting this error

alexrudall commented 3 months ago

@redders123 will see if I can fix this

strukturedkaos commented 3 months ago

@alexrudall Anything I can help with? Are you able to reproduce the issue?

In case it helps:

ruby 3.2.2 Rails 6.1.7.3

alexrudall commented 3 months ago

Hey @strukturedkaos -- struggling to find the time to look at this - but I'm very open to a PR if you're able to figure out a fix! If not I will get to it when I can.

geeosh commented 2 months ago

I ran into this as well, it's because the streaming proc method signature changed in Faraday 2.5.0, and any older version only provides the first two arguments to the proc (see 6799f58).

So you could update the gemspec to require ~> 2.5.0 of Faraday, although that wouldn't work for me as I have other gems that require < 2.0 of Faraday that I'm not able to easily upgrade.

The other option is to update Anthropic::HTTP#handle_faraday_error to accomodate the case where env isn't provided as a third argument. At first glance I don't see any way to get the http request status in order to check if there is an error with an older version of Faraday, so you might just have to assume the streaming request is successful on older versions of Faraday?

@alexrudall if you have any suggestions about how you'd like to handle older versions of Faraday and I can put together a PR.

geeosh commented 2 months ago

I created a PR that makes it functional for anyone using Faraday < 2.5.0: https://github.com/alexrudall/anthropic/pull/21

Let me know if you have suggested changes on that one since it skips out on handling errors while receiving streaming results if Faraday < 2.5.0.

strukturedkaos commented 2 months ago

@geeosh your fix worked for me. Thank you. Hopefully we can get the PR merged soon.