You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastAPI Path()的gt与lt参数在Swagger UI中不生效问题咨询

FastAPI路径参数gt/lt约束不生效且Swagger UI不显示问题

使用版本

  • python 3.9.9
  • FastAPI 0.95.1
  • uvicorn 0.22.0
  • firefox 102.8.0

问题现象

在以下代码中,路径参数item_id通过gt=100和lt=1000设置了大于100且小于1000的范围约束,但该约束在Swagger UI中完全不显示,同时实际请求时也不生效。与之对比,使用ge(大于等于)和le(小于等于)参数时,约束既能在Swagger UI中正常展示,也能正确生效。

示例代码

from typing import Annotated

from fastapi import FastAPI, Path

app = FastAPI()

@app.get("/items/{item_id}")
async def read_items(
    item_id: Annotated[int, Path(title="The ID of the item to get", gt=100, lt=1000)]
):
    return {"item_id": item_id}

内容的提问来源于stack exchange,提问作者jung rhew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:52:39