FastAPI Error Caused By Dependency - NFSandbox/sh_trade_backend GitHub Wiki
This blog is used to record a FastAPI error with an error message that is not so clear:
When I working with FastAPI and its Dependency Injection pattern, I faced this error:
fastapi.exceptions.FastAPIError:
Invalid args for response field!
Hint: check that <class 'schemes.sql.User'> is a valid Pydantic field type.
If you are using a return type annotation that is not a valid Pydantic field
(e.g. Union[Response, dict, None])
you can disable generating the response model from the type annotation
with the path operation decorator parameter response_model=None.
Read more: https://fastapi.tiangolo.com/tutorial/response-model/
The full error is like:
Exception Details
Process SpawnProcess-90:
Traceback (most recent call last):
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\multiprocessing\process.py", line 314, in _bootstrap
self.run()
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\uvicorn\server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\asyncio\runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\asyncio\base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\uvicorn\server.py", line 67, in serve
config.load()
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\uvicorn\config.py", line 477, in load
self.loaded_app = import_from_string(self.app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\importlib\__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "C:\Data\Study\Database Design\sh_trade_backend\main.py", line 17, in <module>
from endpoints.auth import auth_router, token_router
File "C:\Data\Study\Database Design\sh_trade_backend\endpoints\__init__.py", line 2, in <module>
from . import user
File "C:\Data\Study\Database Design\sh_trade_backend\endpoints\user.py", line 77, in <module>
@user_router.post("/contact_info/add", response_model=db_sche.ContactInfoOut)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\fastapi\routing.py", line 704, in decorator
self.add_api_route(
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\fastapi\routing.py", line 643, in add_api_route
route = route_class(
^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\fastapi\routing.py", line 489, in __init__
self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\fastapi\dependencies\utils.py", line 261, in get_dependant
type_annotation, depends, param_field = analyze_param(
^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\fastapi\dependencies\utils.py", line 429, in analyze_param
field = create_response_field(
^^^^^^^^^^^^^^^^^^^^^^
File "c:\ProgramData\Anaconda3\envs\sh_trade\Lib\site-packages\fastapi\utils.py", line 101, in create_response_field
raise fastapi.exceptions.FastAPIError(
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'schemes.sql.User'> is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/
The error seems to imply that I have some issue with my Response Model, however my endpoint function is like below:
@user_router.post("/contact_info/add", response_model=db_sche.ContactInfoOut)
async def add_user_contact_info(
ss: SessionDep,
current_user: Annotated[orm.User, user_provider.get_current_user],
info: db_sche.ContactInfoIn,
):
uew_contact_info = await user_provider.add_contact_info(ss, current_user, info)
return uew_contact_info
The error says:
Hint: check that <class 'schemes.sql.User'> is a valid Pydantic field type.
However our Response Model and Return Type Annotation has nothing to do with orm.User
class. So I try to check out what place in this function has some connection with orm.User
class, then I found this line:
current_user: Annotated[orm.User, user_provider.get_current_user],
I wrote this code suppose that current_user
becomes a dependency, but the issue is that I forgot Depends()
annotation.
So the correct version should be:
current_user: Annotated[orm.User, Depends(user_provider.get_current_user)]