Allow custom Faraday adapter#26
Merged
andreibondarev merged 1 commit intopatterns-ai-core:mainfrom Apr 27, 2026
Merged
Conversation
Add an `adapter:` keyword argument to `Cohere::Client.new`, defaulting to `Faraday.default_adapter`. This lets callers opt into persistent adapters (e.g., `:net_http_persistent`) to amortize TCP/TLS handshakes across requests and reduce tail latency under load.
Collaborator
|
@martinechtner Thank you for the PR. |
There was a problem hiding this comment.
Pull request overview
Adds a supported way to customize the Faraday HTTP adapter used by Cohere::Client, enabling persistent-connection adapters (e.g. :net_http_persistent) while keeping the default behavior unchanged.
Changes:
- Add
adapter:keyword argument toCohere::Client.new(defaulting toFaraday.default_adapter) and expose it viaattr_reader. - Configure Faraday with the selected adapter (including
[adapter, options]form). - Document the new option in README and CHANGELOG; add initializer specs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/cohere/client.rb | Adds adapter: initialization/configuration and applies it when building Faraday connections. |
| spec/cohere/client_spec.rb | Adds specs for adapter: default and override forms. |
| README.md | Documents how to pass a custom/persistent Faraday adapter. |
| CHANGELOG.md | Notes the new adapter: keyword argument under Unreleased. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -215,7 +216,7 @@ def v1_connection | |||
| faraday.request :authorization, :Bearer, api_key | |||
| faraday.request :json | ||
| faraday.response :json, content_type: /\bjson$/ | ||
| faraday.adapter Faraday.default_adapter | ||
| faraday.adapter(*Array(@adapter)) |
Comment on lines
+8
to
+21
| describe "#initialize" do | ||
| it "defaults to Faraday.default_adapter" do | ||
| client = described_class.new(api_key: "x") | ||
| expect(client.adapter).to eq(Faraday.default_adapter) | ||
| end | ||
|
|
||
| it "accepts a custom adapter" do | ||
| client = described_class.new(api_key: "x", adapter: :test) | ||
| expect(client.adapter).to eq(:test) | ||
| end | ||
|
|
||
| it "accepts an adapter with options" do | ||
| adapter = [:net_http_persistent, {name: "cohere", pool_size: 10}] | ||
| client = described_class.new(api_key: "x", adapter: adapter) |
Contributor
Author
|
@andreibondarev no problem are you able to cut a new gem version? |
Collaborator
@martinechtner 1.0.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an
adapter:keyword argument toCohere::Client.newso callers can configure the Faraday HTTP adapter. Defaults toFaraday.default_adapter(Net::HTTP), preserving existing behavior. The motivating use case is:net_http_persistent, which reuses TCP/TLS connections across requests and dramatically reduces tail latency for high-throughput callers.Why
Faraday's default
Net::HTTPadapter doesn't reuse connections — every request opens a fresh TCP socket and performs a new TLS handshake. For high-frequency callers, that handshake overhead can dominate request latency. The standard way to fix this with Faraday is to swap the adapter to a persistent one (e.g.,:net_http_persistent), butCohere::Clientcurrently hardcodesFaraday.default_adapterwith no override path. This PR adds that path while preserving the existing default.Backward compatibility
Cohere::Client.new(api_key: …)— unchanged behavior. The newadapter:argument is optional and defaults toFaraday.default_adapter.chat,generate,embed,rerank,classify,tokenize,detokenize,detect_language,summarize) are untouched.attr_reader :connection; the existing public surface is preserved.Tests
adapter:default, symbol override, and[adapter, options]array form.Usage
Or with adapter options: