You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] 轮询任务状态偶发 NaN 导致 JSON 序列化崩溃,建议改用 ORJSONResponse #462
File ".../app/routers/note.py", line 264, in get_task_status
return R.success({...})
File ".../app/utils/response.py", line 12, in success
return JSONResponse(content={...})
...
ValueError: Out of range float values are not JSON compliant: nan
### 期望行为
任务处理(如构建向量索引)过程中,前端轮询 `/api/task_status/{task_id}` 接口能始终正常返回 HTTP 200 及进度状态,不因内部浮点数计算异常而导致接口序列化崩溃,笔记能顺利生成完毕。
### 实际行为
在处理向量索引阶段,接口调用抛出 `ValueError: Out of range float values are not JSON compliant: nan` 异常,返回 500 状态码,前端页面直接提示生成失败。
**后台完整报错日志:**
```text
File ".../app/routers/note.py", line 264, in get_task_status
return R.success({...})
File ".../app/utils/response.py", line 12, in success
return JSONResponse(content={...})
File ".../site-packages/starlette/responses.py", line 193, in render
return json.dumps(...)
ValueError: Out of range float values are not JSON compliant: nan
---
建议修改方案:
改用 fastapi.responses.ORJSONResponse 替换 JSONResponse,其天然支持容错序列化且基于 Rust 拥有极高的性能提升:
1. backend/app/utils/response.py 改为:
from fastapi.responses import ORJSONResponse
from app.utils.status_code import StatusCode
from pydantic import BaseModel
from typing import Optional, Any
class ResponseWrapper:
@staticmethod
def success(data=None, msg="success", code=0):
return ORJSONResponse(content={
"code": code,
"msg": msg,
"data": data
})
@staticmethod
def error(msg="error", code=500, data=None):
return ORJSONResponse(content={
"code": code,
"msg": str(msg),
"data": data
})
2. 在 backend/requirements.txt 中补充依赖:orjson>=3.9.0
### 运行环境
OS: macOS 15.8.1
- Python: 3.12
### 日志 / 堆栈
```text
Activity