New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'starlette.responses.JSONResponse'> is a valid pydantic field type #5861
Comments
I encountered the same error with response classes @zadigus You can specify |
pydantic does not recognize
The issue happens in
|
I think adding content: Any
status_code: int
headers: ...
... (As in |
Same issue here, it is only checking for instances of
And 3.11 |
Pretty sure it's a regression in release 0.89, since its only new feature deals with EDIT: Forgot to mention: my code worked with 0.88, but doesn't with 0.89, which is why I'm sure it's the culprit. |
Fix the following traceback: ``` lib/galaxy/webapps/galaxy/fast_app.py:178: in initialize_fast_app add_galaxy_middleware(app, gx_app) lib/galaxy/webapps/galaxy/fast_app.py:127: in add_galaxy_middleware async def preflight_handler(request: Request, rest_of_path: str) -> Response: .venv/lib/python3.7/site-packages/fastapi/routing.py:658: in decorator generate_unique_id_function=generate_unique_id_function, .venv/lib/python3.7/site-packages/fastapi/routing.py:598: in add_api_route generate_unique_id_function=current_generate_unique_id, .venv/lib/python3.7/site-packages/fastapi/routing.py:401: in __init__ name=response_name, type_=self.response_model .venv/lib/python3.7/site-packages/fastapi/utils.py:92: in create_response_field ) from None E fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'starlette.responses.Response'> is a valid pydantic field type ``` tracked upstream in tiangolo/fastapi#5861 .
Looks like a regression. Try with 0.88 if the issue exist? |
Yes, 0.88 works as expected. |
Small script to reproduce:
from fastapi import FastAPI, Response
app = FastAPI(debug=True)
@app.get("/plain-text")
def plain_text() -> Response:
return Response(content="bla", media_type="text/plain") Running this with
|
@hofrob with a test case so small, you'd have to wonder why the tests haven't catched this before release. |
This issue should be fixed by #5855 |
I guess we're all invited to add tests and fixes to this project. 🤷 |
The pr sets |
Thanks for the report and discussion everyone! ☕ The simple use case of a single And for the other use cases where you want to have more complex type annotations (e.g. unions, other types), you can use There's a lot of new docs about it here: https://fastapi.tiangolo.com/tutorial/response-model/#other-return-type-annotations Also, this is sort of a duplicate of #5857 (it's the same issue underneath). |
Fastapi version 0.89.1 solves the issue in my codebase. |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
I have the same issue
And now my server runs without error... |
I think we should open a new issue, but I am also reproducing, using fastapi 0.103.1, and migrating from pydantic v1 to pydantic v2 (2.4.1, tried with 2.2.1 as well) |
Please create a discussion. This issue was solved. |
First Check
Commit to Help
Example Code
Description
With version 0.88.0, my unit tests run fine, I get no error. With version 0.89.0, I get the following error:
The only way I found to get rid of the error is to remove the type hint, which I don't really want. Also, I tried to follow the guidelines provided in your documentation without success.
Interestingly, the whole thing works fine (with the type hint and fastapi 0.89.0) under Windows 10. Under Ubuntu 22.10, it doesn't work at all (i.e. it provides the above message).
Operating System
Linux
Operating System Details
Under Windows 10, it works like a charm. Under Ubuntu 22.10, it fails with the provided error above.
FastAPI Version
0.89.0
Python Version
3.8.12
Additional Context
It is also failing with python 3.9, and 3.10.
The text was updated successfully, but these errors were encountered: