Skip to content

fix(core): 修复 Pydantic v2 下 Record.path 被错误视为必填的问题#7048

Open
No-22-Github wants to merge 2 commits intoAstrBotDevs:masterfrom
No-22-Github:master
Open

fix(core): 修复 Pydantic v2 下 Record.path 被错误视为必填的问题#7048
No-22-Github wants to merge 2 commits intoAstrBotDevs:masterfrom
No-22-Github:master

Conversation

@No-22-Github
Copy link
Copy Markdown

@No-22-Github No-22-Github commented Mar 27, 2026

修复 Record 在 Python 3.14 / Pydantic v2 环境下的一个核心校验问题。

Record.path 虽然被标注为可选类型,但没有设置默认值,因此在 Pydantic v2 中仍然会被当成必填字段处理。
这会导致某些平台适配器或插件在构造 Record(...) 时,如果没有显式传入 path,就会触发 ValidationError

Telegram 语音消息是一个已经确认的复现场景:

  • Telegram 语音会先被下载并转换为 wav
  • 适配器随后构造 Comp.Record(file=path_wav, url=path_wav)
  • 由于没有传入 path,模型校验直接失败,导致后续 STT 流程无法继续

Modifications / 改动点

  • Record.path 改为真正可选,补充默认值 None

  • 在 Telegram 语音消息分支中构造 Comp.Record(...) 时,显式传入 path=path_wav

  • 修复在 Python 3.14 / Pydantic v2 下的兼容性问题

  • 保持现有行为不变,不引入破坏性修改

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

修复前可稳定复现以下错误:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Record
path
  Field required [type=missing, input_value={'file': '...', 'url': '...'}, input_type=dict]

复现步骤:

  1. 在 Telegram 中发送一条语音消息
  2. AstrBot 下载并转换语音文件
  3. 在构造 Comp.Record(...) 时触发校验异常
  4. 消息转换阶段中断,后续语音转文本流程无法继续

修复后验证:

  1. 再次发送 Telegram 语音消息
  2. 消息转换可以正常完成
  3. 后续语音转文本流程可以继续执行
image

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“Verification Steps”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Ensure Telegram voice messages correctly produce Record components when path is omitted under Pydantic v2.

Bug Fixes:

  • Make the Record.path field truly optional by providing a default value to avoid validation errors under Pydantic v2.

Tests:

  • Add a Telegram voice message test to verify a Record component is created successfully with the converted WAV file.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 27, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/test_telegram_adapter.py" line_range="111-120" />
<code_context>
+@pytest.mark.asyncio
</code_context>
<issue_to_address>
**issue (testing):** Strengthen this regression test by also asserting the `path` field on the `Record` component.

This test only verifies that a `Comp.Record` is created and its `file` matches `wav_path`, so it would still pass even if `path` were missing or wrong. Please also assert `result.message[0].path == str(wav_path)` (and optionally `result.message[0].url == str(wav_path)` if desired) so the test will fail if `path` stops being populated correctly.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request initializes the path attribute in the Record component with a default value of None and adds a unit test for Telegram voice message processing. The feedback suggests strengthening the new test by asserting the url and path fields of the generated Record object.

@dosubot dosubot bot added area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant