AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] 知识库写入失败:document_storage.py 使用 naive datetime 导致 SQLModel 报错

Open A1ex288 opened this issue 2 months ago • 4 comments

Description

在 AstrBot v4.28.1 中,向知识库写入文档时,Markdown 分块和 Embedding 均成功,但在写入 documents 表时失败。

最初日志只显示:

存储失败:文本块已生成,但写入知识库索引时出错。

为了定位问题,我在 knowledge_base/kb_helper.py 的异常日志中临时加入了 exc_info=True,随后得到了完整 traceback。

实际底层错误为:

ValueError: Datetime values must have timezone information. Use datetime.now(timezone.utc), or annotate the field with NaiveDatetime for naive storage.

异常发生位置:

astrbot/core/db/vec_db/faiss_impl/document_storage.py insert_documents_batch() → await session.flush()

SQL 为:

INSERT INTO documents (doc_id, text, metadata, created_at, updated_at)

检查源码后发现 document_storage.py 在创建和更新文档时使用:

datetime.now()

该值为 timezone-naive datetime,而当前 SQLModel 字段要求 timezone-aware datetime,因此在 session.flush() 时直接失败。

我将:

from datetime import datetime

修改为:

from datetime import datetime, timezone

并将该文件中的 datetime.now() 修改为:

datetime.now(timezone.utc)

修改后:

  1. 同一个测试文档成功写入知识库
  2. 文档数量和分块数量正常显示
  3. 知识库向量检索成功
  4. 查询“Bianca多大”能够正确召回对应文本

因此可以稳定确认,该问题由 document_storage.py 中 timezone-naive datetime 导致。

与 #9538、#9007 的表面现象相似,但本次已经获得底层 traceback 并定位到具体原因。

Reproduction Steps

  1. 使用 AstrBot v4.28.1。

  2. 配置可正常工作的 Embedding 模型。

  3. 创建一个新的知识库。

  4. 上传一个简单的 Markdown 文档,例如:

    Alex最喜欢黑色 Bianca是一名27岁的女性 这是一条知识库同步测试。

  5. MarkdownChunker 正常完成分块。

  6. Embedding 正常生成。

  7. 在写入本地 documents 表时失败。

开启完整 traceback 后可以看到:

ValueError: Datetime values must have timezone information.

调用链:

kb_helper.py → vec_db.insert_batch() → document_storage.insert_documents_batch() → session.flush() → SQLModel datetime 校验失败

AstrBot Version

v4.28.1

Operating System

Windows

Deployment Method

AstrBot Launcher(Windows 本地部署)

Message Platform Adapter

openclaw-weixin(个人微信);该问题与消息平台适配器无关

Error Log

[Core][INFO][knowledge_base.kb_helper:351]:
Using MarkdownChunker for structured document 'Bianca知识库测试.md'.

[Core][DBUG][faiss_impl.vec_db:127]:
Generating embeddings for 1 contents...

[Core][DBUG][faiss_impl.vec_db:136]:
Generated embeddings for 1 contents in 0.31 seconds.

Traceback (most recent call last):
  File ".../astrbot/core/knowledge_base/kb_helper.py", line 417, in upload_document
    await self.vec_db.insert_batch(

  File ".../astrbot/core/db/vec_db/faiss_impl/vec_db.py", line 191, in insert_batch
    int_ids = await self.document_storage.insert_documents_batch(

  File ".../astrbot/core/db/vec_db/faiss_impl/document_storage.py", line 341, in insert_documents_batch
    await session.flush()

ValueError: Datetime values must have timezone information.
Use datetime.now(timezone.utc), or annotate the field with NaiveDatetime for naive storage.

sqlalchemy.exc.StatementError:
(builtins.ValueError) Datetime values must have timezone information.

[SQL:
INSERT INTO documents
(doc_id, text, metadata, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)]

Supplementary Information

No response

Checklist

  • [x] I have searched the Issue list for related issues and could not resolve the problem, or this issue has never been reported before.
  • [x] I have tried disabling all plugins to rule out issues caused by plugins.
  • [x] The issue I am reporting is related to AstrBot itself, not to a specific plugin.
  • [x] I have read and agree to the project's Code of Conduct.
  • [ ] (Optional) I am willing to submit a PR to help fix this issue.

A1ex288 avatar Sep 23 '26 05:09 A1ex288

补充排查:SQLModel 0.0.45 起默认要求带时区的 UTC 时间,影响似乎不止知识库存储。 在 SQLModel 0.0.46 下,还确认了以下兼容问题:

  • 统计查询:查询时间参数未带时区,导致统计接口报错。
  • 会话批量删除:清理消息历史时使用了未带时区的时间参数,导致删除失败。
  • 备份导入的时间转换:现有逻辑未识别新的 UTCDateTime 类型,时间字符串没有按预期转换。

上述问题对应 CI 中的 5 项失败测试,我在 #10206 修改前的代码上也复现了相同失败,因此并非该 PR 引入。 #10206 目前仅修复知识库存储。历史本地时间与新写入 UTC 时间的兼容策略,以及其他模块是否统一适配,留给维护者评估。

wcqqq1214 avatar Sep 23 '26 07:09 wcqqq1214

在Linux+uv+Python3.14环境下全新安装astrbot会因为这个原因打不开统计查询

jim139129 avatar Sep 23 '26 16:09 jim139129

结论:本体 bug;已在 master 修复(#10211),尚未发版 建议标签:area:core


🤖 本评论由 AI 生成,结论已基于仓库代码核实;如有出入,以维护者判断为准。

silvaling avatar Sep 24 '26 12:09 silvaling

fixed in #10211

Soulter avatar Sep 27 '26 14:09 Soulter