NameError: name 'AsyncGenerator' is not defined with Python 3.12 in FastAPI 0.124.0 #14475
Replies: 2 comments 1 reply
-
|
Duplicate of: |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
Let's close this discussion and track this issue in #14464 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
After upgrading to FastAPI 0.124.0, our application fails to start with Python 3.12. The error occurs during route registration when FastAPI tries to evaluate type annotations.
Environment:
Error Traceback:
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/path/to/site-packages/langflow/main.py", line 33, in
from langflow.main import setup_app
File "/path/to/site-packages/langflow/main.py", line 25, in
from langflow.api import health_check_router, log_router, router
File "/path/to/site-packages/langflow/api/init.py", line 1, in
from langflow.api.health_check_router import health_check_router
File "/path/to/site-packages/langflow/api/health_check_router.py", line 39, in
@health_check_router.get("/health_check")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/site-packages/fastapi/routing.py", line 1128, in decorator
self.add_api_route(
File "/path/to/site-packages/fastapi/routing.py", line 1067, in add_api_route
route = route_class(
File "/path/to/site-packages/fastapi/routing.py", line 686, in init
self.dependant = get_dependant(
File "/path/to/site-packages/fastapi/dependencies/utils.py", line 303, in get_dependant
sub_dependant = get_dependant(
File "/path/to/site-packages/fastapi/dependencies/utils.py", line 277, in get_dependant
endpoint_signature = get_typed_signature(call)
File "/path/to/site-packages/fastapi/dependencies/utils.py", line 214, in get_typed_signature
signature = inspect.signature(call, eval_str=True)
File "/path/to/lib/python3.12/inspect.py", line 3348, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
File "/path/to/lib/python3.12/inspect.py", line 3085, in from_callable
return _signature_from_callable(obj, sigcls=cls,
File "/path/to/lib/python3.12/inspect.py", line 2597, in _signature_from_callable
return _signature_from_function(sigcls, obj,
File "/path/to/lib/python3.12/inspect.py", line 2424, in _signature_from_function
annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str)
File "/path/to/lib/python3.12/inspect.py", line 292, in get_annotations
value if not isinstance(value, str) else eval(value, globals, locals)
NameError: name 'AsyncGenerator' is not defined
Root Cause:
In Python 3.12, AsyncGenerator must be explicitly imported from typing or collections.abc. It seems FastAPI 0.124.0 (or one of its dependencies) uses AsyncGenerator in a type annotation as a string that gets evaluated at runtime via inspect.signature(call, eval_str=True), but the name is not available in the evaluation namespace.
Workaround:
Downgrading to FastAPI 0.115.6 resolves the issue:
pip install 'fastapi<0.124.0'
Steps to Reproduce:
Expected Behavior:
Application should start without errors, as it did with FastAPI 0.115.x and earlier versions.
Actual Behavior:
Application crashes immediately on import with NameError: name 'AsyncGenerator' is not defined.
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.124.0 (latest)
Pydantic Version
2.11.10
Python Version
3.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions