Skip to content

[BUG] RubyLLM not retrying POST requests #417

@AlexVPopov

Description

@AlexVPopov

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?

Hey, folks,

I think I've discovered a bug, unless, of course, I'm not missing something. I think RubyLLM won't retry POST requets, because it doesn't set the methods option on faraday-retry and also doesn't provide a way to set it.

  • The faraday-retry middleware accepts a methods option and falls back to %i[delete get head options put], if no methods are given.
  • RubyLLM doesn't set the methods option, hence only idempotent methods are retried, post requests are never retried.

I'm hitting this on Anthropic's POST v1/messages endpoint. Tagging @tpaulshippy here as well, as I know he uses Anthropic heavily as well.

How to reproduce

  1. RubyLLM has a default config for automatic retries, so need for additional configuration regarding retries. Set Anthropic as provider or make sure that the ask method will issue a POST request.

  2. Stub an error response and see, that requests are not retried. Something like:

    context "when Anthropic API returns an error" do
      before do
        # Stub the Anthropic Messages API endpoint with proper error format
        stub_request(:post, "https://api.anthropic.com/v1/messages")
          .to_return(
            status: 500,
            body: {
              type: "error",
              error: {
                type: "api_error",
                message: "An unexpected error has occurred internal to Anthropic's systems."
              },
              request_id: "req_018EeWyXxfu5pfWkrYcMdjWG"
            }.to_json,
            headers: {
              "Content-Type" => "application/json",
              "request-id" => "req_018EeWyXxfu5pfWkrYcMdjWG"
            }
          )
      end

      it "retries the request" do
        RubyLLM.ask "Something"

        expect(WebMock).to have_requested(:post, "https://api.anthropic.com/v1/messages").times(4) # 1 call + 3 (default) retries, this should fail
      end
    end
Failure/Error: expect(WebMock).to have_requested(:post, "https://api.anthropic.com/v1/messages").times(4)
     
       The request POST https://api.anthropic.com/v1/messages was expected to execute 4 times but it executed 1 time

Expected behavior

POST requests should be retried.

What actually happened

POST requests are not retried.

Environment

  • RubyLLM 1.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededtriaged

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions