Skip to content

Enable retry for POST requests in streaming error handling#523

Closed
brunoluigi wants to merge 1 commit into
crmne:mainfrom
brunoluigi:brunoluigi/issue-417
Closed

Enable retry for POST requests in streaming error handling#523
brunoluigi wants to merge 1 commit into
crmne:mainfrom
brunoluigi:brunoluigi/issue-417

Conversation

@brunoluigi
Copy link
Copy Markdown

What this does

  • Add POST method to Faraday retry configuration (not retried by default)
  • Add tests verifying retry behavior for both Faraday 1.x and 2.x code paths
  • Add expected_url_for helper for URL assertions in error tests
  • Reduce retry intervals in test configuration for faster test execution

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the Contributing Guide
  • This aligns with RubyLLM's focus on LLM communication
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my specific use case

Quality check

  • I ran overcommit --install and all hooks pass
  • I tested my changes thoroughly
    • For provider changes: Re-recorded VCR cassettes with bundle exec rake vcr:record[provider_name]
    • All tests pass: bundle exec rspec
  • I updated documentation if needed
  • I didn't modify auto-generated files manually (models.json, aliases.json)

API changes

  • Breaking change
  • New public methods/classes
  • Changed method signatures
  • No API changes

Related issues

Fixes #417

- Add POST method to Faraday retry configuration (not retried by default)
- Add tests verifying retry behavior for both Faraday 1.x and 2.x code paths
- Add `expected_url_for` helper for URL assertions in error tests
- Reduce retry intervals in test configuration for faster test execution
RSpec.describe RubyLLM::Chat do
include_context 'with configured RubyLLM'

before do
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've done this since those tests running with retry enable were changing the VCR recorded requests and I wasn't sure if it was supposed to happen, need some help here =)

config.retry_interval = 1
config.retry_backoff_factor = 3
config.retry_interval_randomness = 0.5
config.max_retries = 2
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Once retry is enabled the tests at spec/ruby_llm/chat_streaming_spec.rb:57 starts to wait between retries and with the current settings it takes a lot of time so I adjusted to something more adequate for test environments.

One question is, should the faraday-retry middleware actually wait in the test environment or it could be set to simply skip the waiting and retry right away?

end.to raise_error(expected_error_for(provider))
end

it 'retries the request' do
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Inspired by the test that @AlexVPopov suggested in the issue #417

@crmne
Copy link
Copy Markdown
Owner

crmne commented Feb 20, 2026

Thanks for jumping on this and for digging into the Faraday v1 test hang. The root cause analysis around test backoff settings was very helpful.

I verified the core issue locally and agree with enabling POST retries. One additional thing I found: 504 is currently listed as retryable but not mapped to ServiceUnavailableError, so it can bypass retry classification unless error.rb maps 502..504 together.

If you’re open to it, a minimal merge path could be:

  • keep the POST retry fix, but remove the other methods from the config
  • keep the reduced retry/backoff test config (to avoid long spec sleeps)
  • add 504 -> ServiceUnavailableError mapping in error.rb

Happy to jump in and do these changes myself if you can't.

crmne added a commit that referenced this pull request Feb 20, 2026
This follows up on #523 with a minimal patch set:

- enable retries for POST while preserving Faraday default idempotent
methods
- map HTTP 504 to `RubyLLM::ServiceUnavailableError` (so retry
classification is consistent)
- reduce retry backoff settings in test config to avoid long sleeps in
retry/error specs
- add focused specs for retry config and 502/503/504 error mapping

Related: #417
@crmne
Copy link
Copy Markdown
Owner

crmne commented Feb 20, 2026

My apologies for the above PR @brunoluigi. I asked codex to integrate my changes in your PR but it went ahead and create a new PR and merged it 🤦‍♂. I'll credit you properly in the release notes.

@crmne crmne closed this Feb 20, 2026
@brunoluigi brunoluigi deleted the brunoluigi/issue-417 branch April 17, 2026 09:08
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.

[BUG] RubyLLM not retrying POST requests

2 participants