Skip to content

Allow custom Faraday adapter#26

Merged
andreibondarev merged 1 commit intopatterns-ai-core:mainfrom
martinechtner:configurable-adapter
Apr 27, 2026
Merged

Allow custom Faraday adapter#26
andreibondarev merged 1 commit intopatterns-ai-core:mainfrom
martinechtner:configurable-adapter

Conversation

@martinechtner
Copy link
Copy Markdown
Contributor

Summary

Adds an adapter: keyword argument to Cohere::Client.new so callers can configure the Faraday HTTP adapter. Defaults to Faraday.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::HTTP adapter 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), but Cohere::Client currently hardcodes Faraday.default_adapter with no override path. This PR adds that path while preserving the existing default.

Backward compatibility

  • Cohere::Client.new(api_key: …) — unchanged behavior. The new adapter: argument is optional and defaults to Faraday.default_adapter.
  • All existing methods (chat, generate, embed, rerank, classify, tokenize, detokenize, detect_language, summarize) are untouched.
  • No changes to attr_reader :connection; the existing public surface is preserved.

Tests

  • Added 3 specs covering adapter: default, symbol override, and [adapter, options] array form.
  • All existing specs (9) pass unchanged.

Usage

require "faraday/net_http_persistent"

client = Cohere::Client.new(
  api_key: ENV["COHERE_API_KEY"],
  adapter: :net_http_persistent
)

Or with adapter options:

client = Cohere::Client.new(
  api_key: ENV["COHERE_API_KEY"],
  adapter: [:net_http_persistent, {name: "cohere", pool_size: 5}]
)

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.
@andreibondarev andreibondarev requested a review from Copilot April 27, 2026 23:13
@andreibondarev andreibondarev merged commit 6f9feac into patterns-ai-core:main Apr 27, 2026
6 checks passed
@andreibondarev
Copy link
Copy Markdown
Collaborator

@martinechtner Thank you for the PR.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 to Cohere::Client.new (defaulting to Faraday.default_adapter) and expose it via attr_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.

Comment thread lib/cohere/client.rb
@@ -215,7 +216,7 @@ def v1_connection
faraday.request :authorization, :Bearer, api_key
Comment thread lib/cohere/client.rb
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)
@martinechtner
Copy link
Copy Markdown
Contributor Author

@andreibondarev no problem are you able to cut a new gem version?

@andreibondarev
Copy link
Copy Markdown
Collaborator

@andreibondarev no problem are you able to cut a new gem version?

@martinechtner 1.0.2

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.

3 participants