Cryptic error messages and responses when a Depends() function fails to propogate the Type
See original GitHub issueFirst 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.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
Here’s a self-contained, minimal, reproducible, example with my use case:
requirements.txt
fastapi==0.65.2
tenacity==7.0.0
uvicorn==0.14.0
main.py
from fastapi import Depends, FastAPI
from tenacity import retry, stop_after_attempt, wait_fixed
app = FastAPI()
@retry(stop=stop_after_attempt(4), wait=wait_fixed(10))
async def get_five():
return 5
@app.get("/")
async def read_root(five: int = Depends(get_five)):
return {"Hello": f"World {five}"}
Description
When using Depends
in an endpoint, if you wrap the depending async function with tenacity’s retry()
decorator, the GET will fail with a 422. It seems to work with a regular synchronous function, and @retry
seems to work fine with asynchronous functions in general.
If you comment out the @retry
line, the example works fine.
Environment
- OS: Linux - Ubuntu 20.04.2
- FastAPI Version [e.g. 0.3.0]: 0.65.2
- Python version: 3.8.5
Additional context
uncommented:
HTTP/1.1 422 Unprocessable Entity
content-length: 168
content-type: application/json
date: Fri, 02 Jul 2021 00:09:16 GMT
server: uvicorn
{
"detail": [
{
"loc": [
"query",
"args"
],
"msg": "field required",
"type": "value_error.missing"
},
{
"loc": [
"query",
"kwargs"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
commented:
$ http http://localhost:8000
HTTP/1.1 200 OK
content-length: 19
content-type: application/json
date: Fri, 02 Jul 2021 00:09:40 GMT
server: uvicorn
{
"Hello": "World 5"
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Error Boundaries - React
If an error boundary fails trying to render the error message, the error will propagate to the closest error boundary above it. This,...
Read more >LEAF - 1.79.0 - Boost C++ Libraries
What is a failure? It is simply the inability of a function to return a valid result, instead producing an error object describing...
Read more >How to properly propagate error messages using Python
How to propagate the error from the method c back to the method a so I can e.g. store somewhere it or return...
Read more >Changelog — Python 3.11.1 documentation
set_int_max_str_digits() function in the error message. Patch by Victor Stinner. gh-96587: Correctly raise SyntaxError on exception groups (PEP 654) on python ...
Read more >Handling Errors - FastAPI
To return HTTP responses with errors to the client you use HTTPException . ... inside of that utility function, it won't run the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fair enough. Now that I’m pretty sure it isn’t actually a bug, updating the title makes sense. It was really hard to chase down what was happening based on the error I was getting.
This was a decidedly non trivial issue to chase down in an api with many endpoints and many nested Depends. The default return message is rather obtuse, and nothing gets logged to the fastapi console either. I opened an issue with Tenacity and referenced this one. – https://github.com/jd/tenacity/issues/318