gpt4 book ai didi

python - 使用 pydantic @validator 装饰器捕获错误以进行 fastapi 输入查询

转载 作者:行者123 更新时间:2023-12-05 05:58:18 31 4
gpt4 key购买 nike

到目前为止,我有 2 个问题无法使用 docs/goole/stack info 解决:-)

1) 当使用模型作为 fastapi get 查询的查询输入时,我该如何捕获 pydantic 模型验证器装饰器抛出的 ValidationErrors?

上下文:

我有一个狂妄的模型:

class PointRankReqParams(BaseModel):
rat: Optional[Literal['2G', '3G', '4G', '5G']] = '3G'
h3resolution: Optional[int] = 5
input_srid: Optional[int] = 4326
bufferdistance: Optional[int] = 5000
x: float
y: float

@validator('h3resolution')
def h3resolution_bins(cls, v):
if v not in [4,5,6,7,8]:
raise ValueError('Hexabin size not permitted')
return v

和服务于 GET 请求的 FastAPI 函数:

@router.get("/point/overall", summary="GET Ranking for given location point")
async def get_ranking_for_location_point(inputparams: PointRankReqParams = Depends(),token: str = Depends(oauth2_scheme)):
logger.debug("Here I am")
return {}

现在,当我使用超出定义范围的参数 h3resolution curl 时,例如http://localhost:9000/rank/point/overall?rat=2G&h3resolution=9&input_srid=4326&bufferdistance=5000&x=21&y=21我在控制台中收到实际上有意义的错误

py-    |   File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
py- | pydantic.error_wrappers.ValidationError: 1 validation error for PointRankReqParams
py- | h3resolution
py- | Hexabin size not permitted (type=value_error)

但是 FastAPI 向终端客户端返回 HTTP 500 内部服务器错误。我该如何捕获来自验证器的那些异常?


<强>2。如何正确使用 Optional[Literal[...]] 结构?

我试图重新定义上面的例子以在模型中使用:

class PointRankReqParams(BaseModel):
...
h3resolution: Optional[Literal[4,5,6,7,8]] = 5

但是当我 curl GET 查询时例如 http://localhost:9000/rank/point/overall?rat=2G&h3resolution=7&input_srid=4326&bufferdistance=5000&x=21&y=21我收到 422 Unprocessable 条目,因为 h3resolution 被解释为字符串而不是整数:

{
"detail": [
{
"loc": [
"query",
"h3resolution"
],
"msg": "unexpected value; permitted: 4, 5, 6, 7, 8",
"type": "value_error.const",
"ctx": {
"given": "7",
"permitted": [
4,
5,
6,
7,
8
]
}
}
]
}

非常感谢!

最佳答案

这个问题问得好!这已经讨论了好几年了,你可以查看这个 github issue .不幸的是,对于如何处理它没有达成共识,documentation只是展示了一个使用普通 Python 类的简单用例。它与 BaseModeldataclass 一起工作,直到你想要自定义验证器或其他东西,FastAPI 的作者 says :

Having a single Pydantic model for query parameters could beinterpreted as:

http://somedomain.com/?args={"k1":"v1","k2":"v2"}

or

http://somedomain.com/?k1=v1&k2=v2

or many other alternatives...

And we are not even discussing sub-models, that are valid in Pydanticmodels (and request bodies) but the behavior would be undefined fornon-body parameters (query, path, etc).

There's no obvious way to go about how it would be interpreted thatworks for all the cases (including other people's use cases, futureuse cases, etc).

So it doesn't really make sense to have it in FastAPI for a custom usecase as it's very subjective and dependent on the conventions of theteam.

jimcarreer建议使用类似的东西:

class PagingQuery(BaseModel):
page: conint(ge=1)
page_size: conint(ge=1, le=250) = 50

@classmethod
async def depends(cls, page: int = 1, page_size: int = 50):
try:
return cls(page=page, page_size=page_size)
except ValidationError as e:
errors = e.errors()
for error in errors:
error['loc'] = ['query'] + list(error['loc'])
raise HTTPException(422, detail=errors)


@app.get("/example", tags=["basic"])
def example(paging: PagingQuery = Depends(PagingQuery.depends)):
return {"page": paging.page, "page_size": paging.page_size}

但这看起来很老套,我宁愿远离它。

回答你的第二个问题,查询字符串只是一个字符串(如 'age=20&name=John'),键值对只是字符串;您已指定 Literal 类型,因此不会进行类型转换,您会得到一个异常。

关于python - 使用 pydantic @validator 装饰器捕获错误以进行 fastapi 输入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68556288/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com