Skip to content

Accept awaitable-returning async middleware callables - #1856

Open
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/async-middleware-awaitable-contract
Open

Accept awaitable-returning async middleware callables#1856
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/async-middleware-awaitable-contract

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Accept function-style async middleware that is a synchronous callable returning an Awaitable, matching the SDK's public AsyncMiddlewareCallable type contract.

AsyncMiddlewareCallable is defined as a Callable[[APIRequest, AsyncCallNext], Awaitable[AsyncAPIResponse[Any]]]. That contract does not require the callable itself to be declared with async def: a regular function or callable object may validly return the coroutine produced by call_next(request).

validate_async_middleware() currently rejects that shape at client construction because it requires inspect.iscoroutinefunction(...) to be true.

Fix

For function-style async middleware, validate that the entry is callable but do not require the callable itself to be a coroutine function.

Class-based Middleware remains strict: handle_async() must still be async. Non-callable middleware remains rejected.

Regression coverage

Adds end-to-end async-client tests for:

  • a regular function returning call_next(request);
  • a callable object returning the awaitable;
  • normal request completion through both wrappers;
  • continued rejection of non-callable middleware.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 17, 2026 11: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.

1 participant