Skip to content

Commit 8b27a81

Browse files
author
chengjie
committed
修复 /documents API 序列化问题:正确解析 (doc_id, DocProcessingStatus) 元组
**问题**: - 前端报错:Cannot read properties of undefined (reading 'status') - 根因:API 返回 `{"raw_data": "(doc_id, DocProcessingStatus(...))"}`,而不是正确的 JSON 对象 **根因分析**: 1. LightRAG 的 `get_docs_paginated()` 返回元组列表:`[(doc_id, DocProcessingStatus), ...]` 2. 当前代码未正确解包元组,触发降级处理:`{"raw_data": str(doc)}` 3. 前端无法解析 raw_data 字符串,导致 undefined 错误 **修复方案**: - 检测元组格式:`isinstance(doc, tuple) and len(doc) == 2` - 解包元组:`doc_id, status_obj = doc` - 转换 DocProcessingStatus 为字典(优先使用 `asdict()` for dataclass) - 添加 doc_id 字段 - 保留向后兼容的降级处理 **测试计划**: - 调用远程 /documents API,验证返回格式正确 - 确认前端能正确渲染文档列表(content_summary、status、file_path 等字段) **关联**: - 前端团队报告的 BUG:文档列表页面空白 - 涉及文件:api/documents.py (lines 259-289)
1 parent 0b6a6b0 commit 8b27a81

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

api/documents.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from fastapi import APIRouter, HTTPException, Depends, Query, BackgroundTasks
66
from typing import Optional, Literal
7+
from dataclasses import asdict, is_dataclass
78
from src.multi_tenant import get_tenant_lightrag
89
from src.tenant_deps import get_tenant_id
910
from src.logger import logger
@@ -258,13 +259,31 @@ async def list_documents(
258259
# 格式化文档数据
259260
documents = []
260261
for doc in docs_list:
261-
# docs_list 是列表,每个元素是文档对象
262-
if hasattr(doc, '__dict__'):
262+
# docs_list 中每个元素是元组: (doc_id, DocProcessingStatus)
263+
if isinstance(doc, tuple) and len(doc) == 2:
264+
doc_id, status_obj = doc
265+
266+
# 将 DocProcessingStatus 转换为字典
267+
if is_dataclass(status_obj):
268+
doc_dict = asdict(status_obj)
269+
elif hasattr(status_obj, '__dict__'):
270+
doc_dict = status_obj.__dict__.copy()
271+
elif isinstance(status_obj, dict):
272+
doc_dict = status_obj.copy()
273+
else:
274+
logger.warning(f"Unexpected status object type: {type(status_obj)}")
275+
doc_dict = {"raw_data": str(status_obj)}
276+
277+
# 添加 doc_id 字段
278+
doc_dict["doc_id"] = doc_id
279+
280+
# 兼容其他格式(向后兼容)
281+
elif hasattr(doc, '__dict__'):
263282
doc_dict = doc.__dict__.copy()
264283
elif isinstance(doc, dict):
265284
doc_dict = doc.copy()
266285
else:
267-
# 尝试转换为字典
286+
logger.warning(f"Unexpected doc format: {type(doc)}")
268287
doc_dict = {"raw_data": str(doc)}
269288

270289
documents.append(doc_dict)

0 commit comments

Comments
 (0)