Skip to content

Conversation

@majiayu000
Copy link
Contributor

Summary

Problem

When users implement awrap_model_call and forget to await the handler:

async def awrap_model_call(self, request, handler):
    return handler(request)  # Missing await!

This causes AttributeError: 'coroutine' object has no attribute 'result' which is confusing.

Solution

The fix detects this scenario and:

  1. Auto-awaits the coroutine to prevent the crash
  2. Emits a warning explaining the issue and how to fix it

Test plan

  • Added tests for missing await scenarios (single/multiple middleware)
  • Added test verifying proper await does not emit warning
  • All existing middleware tests pass (167 tests)
  • Linting passes
  • Type checking passes

AI agents were involved in developing this contribution.

When async middleware forgets to await handler(request), it returns a
coroutine instead of ModelResponse, causing:
AttributeError: 'coroutine' object has no attribute 'result'

This fix adds _normalize_to_model_response_async() which:
1. Detects when middleware returns a coroutine
2. Auto-awaits it with a helpful warning message
3. Guides users to fix their code

Closes langchain-ai#34234
@github-actions github-actions bot added langchain `langchain` package issues & PRs fix For PRs that implement a fix labels Dec 17, 2025
@hankbesser
Copy link

Sorry if this comes off harsh, But why add 147 lines to an already loaded factory.py when this entails giving a warning when not adding the correct 'await' before the asynchronous handler. Also technically, if downstream middleware short circuits the system, you might not want to await the handler and have the bare coroutine move down stream.

I would say this is more of a documentation point as there isn't really any asynchronous specific documentation for middleware anywhere. But those are just my two cents. Thanks for the work though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix For PRs that implement a fix langchain `langchain` package issues & PRs

Projects

None yet

2 participants