Module: Anthropic::Middleware
- Included in:
- BetaRefusalFallbackMiddleware
- Defined in:
- lib/anthropic/middleware.rb,
sig/anthropic/middleware.rbs
Overview
HTTP around-middleware.
A middleware is any object that responds to #call(request, nxt) and
returns an APIResponse. nxt is itself a #call(request)-able
that invokes the rest of the chain and, ultimately, a single HTTP attempt.
The chain runs per attempt, inside the SDK's retry loop — the same
placement as Go, TypeScript, Java, and Python.
Register middleware at the client level via Anthropic::Client.new(middleware: [...]),
or per request via request_options: {middleware: [...]}. Request-level
entries run innermost (below client-level entries), so client-level
middleware still wraps every request a per-call middleware fabricates or
retries.
On provider clients (Bedrock/Vertex/AWS), a provider middleware is appended below all user entries on every dispatch: it rewrites the canonical request into the provider's wire shape and applies provider auth (SigV4/OAuth), so user middleware always sees the canonical Anthropic request and every re-issued or retried leg is re-signed.
Constant Summary collapse
- Anthropic =
Class Method Summary collapse
-
.build_chain(list, terminal) ⇒ #call
private
Compose
listinto a single callable.
Instance Method Summary collapse
-
#call(request, nxt) ⇒ Anthropic::APIResponse
Optional mixin that names the middleware contract for users who prefer a class to a lambda.
Class Method Details
.build_chain(list, terminal) ⇒ #call
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compose list into a single callable. list[0] is outermost.
428 429 430 431 432 |
# File 'lib/anthropic/middleware.rb', line 428 def build_chain(list, terminal) list.reverse.reduce(terminal) do |inner, mw| ->(req) { mw.call(req, inner) } end end |
Instance Method Details
#call(request, nxt) ⇒ Anthropic::APIResponse
Optional mixin that names the middleware contract for users who prefer a
class to a lambda. include Anthropic::Middleware and implement
#call(request, nxt). The SDK treats any #call(req, nxt) object as
middleware (lambdas, procs, Methods included), so including this is for
discovery and type-checking — under Sorbet the method is abstract, so a
missing or mis-typed #call is a static error — not a runtime
requirement.
416 417 418 |
# File 'lib/anthropic/middleware.rb', line 416 def call(request, nxt) raise NotImplementedError, "#{self.class} must implement #{Anthropic::Middleware}#call(request, nxt)" end |