Replies: 9 comments
-
Hey there, To me, this looks more like you have a bug in your code somewhere --> explained here: https://fastapi.tiangolo.com/tutorial/handling-errors/?h=validation#requestvalidationerror-vs-validationerror
Edit: This is because Pydantic validates your data both coming "in" and "out" of your application. So if data coming from your app doesn't validate through Pydantic, you will get I can see you're trying to validate everything at once. Maybe you could try firstly validating only one attribute.
or
More on validators you can read on Pydantic docs: https://pydantic-docs.helpmanual.io/usage/validators/ Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I removed the interval attribute. Check Above. Still Face the same Issue when a negative value is sent |
Beta Was this translation helpful? Give feedback.
-
Okay, but, 2 things:
|
Beta Was this translation helpful? Give feedback.
-
I am facing the same issue. When using a POST with the class as the body, the validation works as expected - RequestValidationError is raised. When using a GET with the class (using Depends()) as the query parameters, if the validation fails, it raises an Internal Server Error (Instead of the RequestValidationError we are expecting, ValidationError is raised). Adding an exception handler to ValidationError instead of RequestValidationError "solves" the issue, but may expose other problems, like explained in the page @Acerinth linked. Is there a known workaround? Seems there are already other issues opened: #2180, #1474 Raising HTTPException did not work for me; ValidationExceptionError somewhat worked, but it removed the possibility to show more than a single validation problem at once. This seems to be the closest workaround I've found so far: #1474 (comment) |
Beta Was this translation helpful? Give feedback.
-
I can confirm that I'm facing the exact same issue and that #1474 (comment) fixes it but is definitely not nice. |
Beta Was this translation helpful? Give feedback.
-
Gave another suggestion there #1474 (comment) which seems to also work. |
Beta Was this translation helpful? Give feedback.
-
I also have this problem. Some things to note here:
This has been an issue for a while. Though there are some use-case-specific fixes, it becomes cumbersome with Pydantic's types like |
Beta Was this translation helpful? Give feedback.
-
I am struggling with this and probably not understanding it correctly. class Model(BaseModel):
a: str
@field_validator('a')
@classmethod
def ensure_foobar(cls, v: Any):
if 'foobar' not in v:
raise ValueError('"foobar" not found in a')
return v
I would expect FastAPI to respond with I "solved" it by doing this: @app.exception_handler(ValidationError)
async def validation_exception_handler(request: Request, exc: ValidationError) -> JSONResponse:
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"detail": jsonable_encoder(exc.errors())},
) I am confused tough. |
Beta Was this translation helpful? Give feedback.
-
The reason why this happens is the way FastAPI uses TypeAdapters when building a Model and the fact that in such cases, the Exception is raised directly: See (fastapi._compat.py [line: 116ff]):
|
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
This Throws an Internal Server Error when Input is out of Range for a
GET Request
Can we customize the Model to Raise a ValueError with a Custom Status Code
Example -
raise ValueError(status_code=400, detail="Value Must be within range (0,1000000)")
I know we can do this by raising an HTTPException like
raise HTTPException(status_code=400, detail="Value Must be within range (0,1000000)")
Log
Operating System
macOS
Operating System Details
macOS 11.4
FastAPI Version
0.68.1
Python Version
Python 3.7.7
Additional Context
I posted the question on StackOverflow - https://stackoverflow.com/questions/68914523/pydantic-value-error-raises-internal-server-error
I did find a few questions on GitHub for the same but No clear answer was available.
Thank you for taking a look in advance.
Beta Was this translation helpful? Give feedback.
All reactions