Skip to content

fix: stabilize OpenAI tool schema order - #9798

Merged
Soulter merged 1 commit into
AstrBotDevs:masterfrom
x1051445024:fix/stable-openai-tool-schema-order
Aug 25, 2026
Merged

fix: stabilize OpenAI tool schema order#9798
Soulter merged 1 commit into
AstrBotDevs:masterfrom
x1051445024:fix/stable-openai-tool-schema-order

Conversation

@x1051445024

@x1051445024 x1051445024 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

修改说明

  • 在序列化 OpenAI 兼容的函数声明前,按工具名称进行稳定排序。
  • 保持 ToolSet.tools 原有的注册顺序不变。
  • 补充工具 Schema 稳定序列化的单元测试。

当工具注册顺序在不同运行中发生变化时,稳定的声明顺序可以避免兼容上游的提示词前缀产生不必要变化,也有利于上游的 Prompt Cache 复用。

验证

  • python -m ruff format --check astrbot/core/agent/tool.py tests/unit/test_tool_google_schema.py
  • python -m ruff check astrbot/core/agent/tool.py tests/unit/test_tool_google_schema.py
  • python -m pytest tests/unit/test_tool_google_schema.py -q

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


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.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Aug 24, 2026
@x1051445024 x1051445024 changed the title fix: stabilize OpenAI tool schema order fix: 稳定 OpenAI 工具 Schema 的输出顺序 Aug 24, 2026
@Soulter Soulter changed the title fix: 稳定 OpenAI 工具 Schema 的输出顺序 fix: stabilize OpenAI tool schema order Aug 25, 2026
@Soulter
Soulter merged commit 9200e40 into AstrBotDevs:master Aug 25, 2026
21 checks passed
@Soulter

Soulter commented Aug 25, 2026

Copy link
Copy Markdown
Member

感谢,我都不知道会有这个情况(tool加载顺序可能会不同)

@x1051445024

Copy link
Copy Markdown
Contributor Author

不客气,我是在用promptxray来观察astrbot的时候,发现命中缓存的概率非常低,排查之后发现是工具调用的时候因为没有固定工具顺序,所以每次缓存前缀都没有命中

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:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants