Skip to content

Strange exception, saying that error in response model's types, but referencing request model #5885

@dadyarri

Description

@dadyarri

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# Request model
from typing import Optional
from pydantic import BaseModel


class EditProductRequestBody(BaseModel):
    marketId: int
    name: Optional[str]
    price: Optional[str]
    leftover: Optional[int]

    class Config:
        arbitrary_types_allowed = True
# Endpoint, which throws the exception
@app.post("/editProduct")
async def edit_product(body: EditProductRequestBody) -> Response:
    ...
    return JSONResponse(content={"message": "success"})
# Endpoint, which lays earlier, but don't throws the exception

@app.post("/uploadImage")
async def upload_image(photo: UploadFile) -> Response:
    ...
    return JSONResponse(content={"photo": "photoId"})

Description

I have a simple endpoint, which should get a model with data from body and return a json with status. But on startup it throws the exception:

Unlike #5861, it triggers to request model, but tells about invalid response


Traceback (most recent call last):
 File "pydantic/validators.py", line 751, in pydantic.validators.find_validators
TypeError: issubclass() arg 1 must be a class
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
 File "/code/.venv/lib/python3.10/site-packages/fastapi/utils.py", line 88, in create_response_field
   return response_field(field_info=field_info)
 File "pydantic/fields.py", line 436, in pydantic.fields.ModelField.__init__
 File "pydantic/fields.py", line 557, in pydantic.fields.ModelField.prepare
 File "pydantic/fields.py", line 831, in pydantic.fields.ModelField.populate_validators
 File "pydantic/validators.py", line 760, in find_validators
RuntimeError: error checking inheritance of <module 'request_bodies.EditProductRequestBody' from '/app/./request_bodies/EditProductRequestBody.py'> (type: module)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
 File "/code/.venv/bin/uvicorn", line 8, in <module>
   sys.exit(main())
 File "/code/.venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
   return self.main(*args, **kwargs)
 File "/code/.venv/lib/python3.10/site-packages/click/core.py", line 1055, in main
   rv = self.invoke(ctx)
 File "/code/.venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
   return ctx.invoke(self.callback, **ctx.params)
 File "/code/.venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
   return __callback(*args, **kwargs)
 File "/code/.venv/lib/python3.10/site-packages/uvicorn/main.py", line 404, in main
   run(
     File "/code/.venv/lib/python3.10/site-packages/uvicorn/main.py", line 569, in run
   server.run()
 File "/code/.venv/lib/python3.10/site-packages/uvicorn/server.py", line 60, in run
   return asyncio.run(self.serve(sockets=sockets))
 File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
   return loop.run_until_complete(main)
 File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
   return future.result()
 File "/code/.venv/lib/python3.10/site-packages/uvicorn/server.py", line 67, in serve
   config.load()
 File "/code/.venv/lib/python3.10/site-packages/uvicorn/config.py", line 477, in load
   self.loaded_app = import_from_string(self.app)
 File "/code/.venv/lib/python3.10/site-packages/uvicorn/importer.py", line 21, in import_from_string
   module = importlib.import_module(module_str)
 File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
 File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
 File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 883, in exec_module
 File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
 File "/app/./main.py", line 25, in <module>
   async def edit_product(body: EditProductRequestBody) -> Response:
     File "/code/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 630, in decorator
   self.add_api_route(
     File "/code/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 569, in add_api_route
   route = route_class(
     File "/code/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 438, in __init__
   self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
 File "/code/.venv/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 299, in get_dependant
   param_field = get_param_field(
     File "/code/.venv/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 393, in get_param_field
   field = create_response_field(
     File "/code/.venv/lib/python3.10/site-packages/fastapi/utils.py", line 90, in create_response_field
   raise fastapi.exceptions.FastAPIError(
   fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <module 'request_bodies.EditProductRequestBody' from '/app/./request_bodies/EditProductRequestBody.py'> is a valid pydantic field type

Operating System

Other

Operating System Details

Linux (in docker)
Windows 11 (natively)

FastAPI Version

0.85, 0.87, 0.88, 0.89.1

Python Version

3.10, 3.11

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions