FastAPI项目,采用了异步SQLAlchemy创建数据库链接,返回一个sqlalchemy.ext.asyncio.session.AsyncSession对象
from sqlalchemy.ext.asyncio import AsyncSession
async def get_db():
async with async_session() as session:
yield session
然后在路由函数的参数部分声明依赖,省略了函数执行部分代码:
@router.post('/chat', response_model=None,
responses=responses, dependencies=[Depends(RateLimiter(times=100, seconds=86400))])
async def chat(new_message: MessageToAI,
user_identity: UserIdentity = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
) -> Any:
我可以确认如下三点:
- 已经在路径装饰中声明response_model=None,使FastAPI不自动创建响应类型;
- 路由函数部分声明返回任何数据类型 -> Any;
- 函数执行部分仅return状态码,不涉及任何AsyncSession类型。
尽管如此,我还是收到报错:
File "/app/main.py", line 18, in <module>
from app.routers import user_router, chat_router, auth_router, page_router
File "/app/routers/chat_router.py", line 37, in <module>
@router.post('/chat', response_model=None,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 省略了部分内容
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'sqlalchemy.ext.asyncio.session.AsyncSession'> is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/
Process finished with exit code 1
我目前尝试过了通义千问、Kimi、文心一言,给我的建议都是同上述操作1~3,还是不管用。
请给予解答。