[BUG] ReleaseRequest raises when Faraday response has no request options

Open Beginner friendly
#1,679 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ruby
Domain
backend

Research direction

Start with lib/ruby_llm/transport/release_request.rb, where the issue identifies the nil request-options access, then inspect the Connection#instrument_request rescue path. Reproduce with the provided plain Ruby script and add regression coverage for an env without request options and for the original error surviving cleanup. Done when cleanup returns the response and preserves the original error in the rescue path.

Written by the indexing model from the issue text.

Description

bug
Basic checks
  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code
What's broken?

RubyLLM::Transport::ReleaseRequest.release assumes that every Faraday::Env has request options. When env.request is nil, cleanup raises NoMethodError instead of returning the response.

This also masks the original error when Transport::Connection#instrument_request rescues a RubyLLM::Error and calls ReleaseRequest.release(error.response). I encountered it while a custom Responses protocol was raising a RubyLLM::ServerError subclass for a terminal response.failed event, with a Faraday response attached for status/body evidence.

How to reproduce

With the versions below installed, save this as repro.rb and run it with plain ruby repro.rb outside Bundler:

gem 'faraday', '2.15.1'
gem 'ruby_llm', '2.2.2'
require 'ruby_llm'
require 'faraday'

response = Faraday::Response.new(
  status: 500,
  body: 'provider error',
  response_headers: {}
)

RubyLLM::Transport::ReleaseRequest.release(response)

No provider credentials, network requests or WebMock are required. The response has a Faraday::Env, but response.env.request is nil.

Expected behavior

Cleanup should tolerate missing request options, still clear the request body/headers, and return the response. When invoked during error handling, it should leave the original provider/protocol error intact.

What actually happened
NoMethodError: undefined method 'on_data=' for nil

The exception comes from env.request.on_data = nil in transport/release_request.rb. The following env.request.context = nil has the same assumption. In the streaming error path, callers therefore receive this cleanup exception instead of the original RubyLLM::Error.

Suggested fix

I would keep the body/header cleanup unconditional and guard only the request-options cleanup:

env.request_body = nil
env.request_headers = Faraday::Utils::Headers.new

if (request = env.request)
  request.on_data = nil
  request.context = nil
end

response

This retains the credential/body cleanup when no request options exist. Regression tests could cover an env without request options and one with streaming callbacks/context, then exercise the Connection#instrument_request rescue path to verify that it re-raises the original error after cleanup.

Environment
  • Ruby: 4.0.7
  • RubyLLM: 2.2.2
  • Faraday: 2.15.1
  • Provider: none required for the reproduction; originally observed with the OpenAI Responses protocol
  • OS: macOS, ARM64
Dominant language
Ruby
Stars
4.5k
Forks
537
Avg merge
7h 30m
Merged PRs (30d)
378

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from crmne/ruby_llm

All issues in crmne/ruby_llm

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.