Skip to content

fix: handle PPIO platform context-length error messages#7888

Merged
Soulter merged 5 commits intoAstrBotDevs:masterfrom
leonforcode:master
May 2, 2026
Merged

fix: handle PPIO platform context-length error messages#7888
Soulter merged 5 commits intoAstrBotDevs:masterfrom
leonforcode:master

Conversation

@leonforcode
Copy link
Copy Markdown
Contributor

@leonforcode leonforcode commented Apr 29, 2026

修复 PPIO 平台上下文长度错误检测失效的问题

AstrBot 在调用 PPIO 平台的 LLM 模型时,出现 400 BadRequestError 错误(The input is longer than the model's context length),导致所有模型 fallback 失败后进入 AgentState.ERROR 状态。这是因为 _handle_api_error 方法中的上下文长度检测逻辑只匹配 "maximum context length",无法识别 PPIO 平台的错误消息格式。

Modifications / 改动点

修改文件:

  • astrbot/core/provider/sources/openai_source.py(第 1072 行)

改动内容:

  • 扩展上下文长度错误检测逻辑,新增对 "context length" 的匹配(不区分大小写)
  • 保留原有的 "maximum context length" 匹配,确保向后兼容(OpenAI、Azure 等平台)
  • 使用 .lower() 确保不区分大小写,增强鲁棒性

修复前:

if "maximum context length" in str(e):

修复后:

if "maximum context length" in str(e) or "context length" in str(e).lower():

影响范围:

  • 修复 PPIO 平台模型(如 ppio/zai-org/glm-5-turboppio/minimax/minimax-m2.7 等)的上下文长度超限错误处理

  • 不影响其他平台的现有功能

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

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

修复后,当 PPIO 平台返回 The input is longer than the model's context length 错误时:

  1. 正确识别为上下文长度超限错误
  2. 调用 self.pop_record(context_query) 弹出最早的记录
  3. 重试请求,而不会直接抛出异常
  4. 避免级联 fallback 失败

Summary by Sourcery

Bug Fixes:

  • Handle PPIO platform context-length error messages by broadening the matching logic for context length errors so requests can be retried instead of causing agent errors.

leonforcode and others added 5 commits March 13, 2026 18:08
…bility

- Extend error detection to handle PPIO's error message format:
  'The input is longer than the model's context length'
- Add case-insensitive matching using .lower() for robustness
- Maintain backward compatibility with existing 'maximum context length' check

This fixes the issue where PPIO platform models (e.g., ppio/zai-org/glm-5-turbo)
would fail with AgentState.ERROR due to unrecognized context length errors.
@auto-assign auto-assign Bot requested review from Raven95676 and anka-afk April 29, 2026 07:15
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Apr 29, 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 left some high level feedback:

  • Consider normalizing the exception message once (e.g., msg = str(e).lower()) and reusing it in the condition to avoid calling str(e) twice and to keep the logic clearer.
  • The new "context length" substring is quite generic; if possible, narrow the match (for example by anchoring to a phrase like "model's context length") to reduce the risk of misclassifying unrelated errors that mention context length.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider normalizing the exception message once (e.g., `msg = str(e).lower()`) and reusing it in the condition to avoid calling `str(e)` twice and to keep the logic clearer.
- The new `"context length"` substring is quite generic; if possible, narrow the match (for example by anchoring to a phrase like `"model's context length"`) to reduce the risk of misclassifying unrelated errors that mention context length.

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 updates the error handling logic in the OpenAI source provider to catch context length errors more effectively by adding a case-insensitive check for the string 'context length'. A review comment points out that the new case-insensitive check makes the original specific string check redundant and suggests simplifying the condition for better readability and consistency.

)
raise e
if "maximum context length" in str(e):
if "maximum context length" in str(e) or "context length" in str(e).lower():
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.

medium

这里的逻辑存在冗余。"context length" in str(e).lower() 已经完全涵盖了 "maximum context length" in str(e) 的情况(包括各种大小写组合)。建议简化为统一的小写匹配,这不仅能保持对原有错误消息的兼容,也使代码更简洁,并与该函数后续处理其他错误(如第 1127-1129 行)的风格保持一致。

Suggested change
if "maximum context length" in str(e) or "context length" in str(e).lower():
if "context length" in str(e).lower():

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 2, 2026
@Soulter Soulter changed the title fix: AstrBot 在调用 PPIO 平台的 LLM 模型时,出现 400 BadRequestError 上下文超长错误,导致所有模型 fallback 失败后进入 AgentState.ERROR 状态。 fix: handle PPIO platform context-length error messages May 2, 2026
@Soulter Soulter merged commit 909b4ad into AstrBotDevs:master May 2, 2026
21 checks passed
Soulter added a commit that referenced this pull request May 3, 2026
* fix: 压缩算法删除 user 消息 Bug 修复

* perf: improve truncate algo

* fix: improve context length error detection for PPIO platform compatibility

- Extend error detection to handle PPIO's error message format:
  'The input is longer than the model's context length'
- Add case-insensitive matching using .lower() for robustness
- Maintain backward compatibility with existing 'maximum context length' check

This fixes the issue where PPIO platform models (e.g., ppio/zai-org/glm-5-turbo)
would fail with AgentState.ERROR due to unrecognized context length errors.

---------

Co-authored-by: Soulter <905617992@qq.com>
Soulter added a commit that referenced this pull request May 3, 2026
* feat: supports plugin to add skills

* fix tests

* fix: fs tools

* Add tests for plugin skills handling and improve skill management

- Implement test for restricted local member reading plugin skill inventory even if the plugin is inactive.
- Ensure that the skill synchronization process retains built-in skills when local skills are empty, including proper handling of plugin paths.
- Update dashboard tests to verify that plugin details include components when requested.
- Enhance skill metadata enrichment tests to include inactive plugin-provided skills for inventory.
- Add filtering tests for plugin skills based on current configuration, ensuring only allowed plugins are considered and inactive plugins are skipped.

Co-authored-by: Copilot <copilot@github.com>

* fix: handle PPIO platform context-length error messages (#7888)

* fix: 压缩算法删除 user 消息 Bug 修复

* perf: improve truncate algo

* fix: improve context length error detection for PPIO platform compatibility

- Extend error detection to handle PPIO's error message format:
  'The input is longer than the model's context length'
- Add case-insensitive matching using .lower() for robustness
- Maintain backward compatibility with existing 'maximum context length' check

This fixes the issue where PPIO platform models (e.g., ppio/zai-org/glm-5-turbo)
would fail with AgentState.ERROR due to unrecognized context length errors.

---------

Co-authored-by: Soulter <905617992@qq.com>

* fix: 支持微信客服文件消息 (#7923)

* fix: 支持微信客服文件消息

* fix: remove WeCom file message placeholder

* fix(provider): fix Anthropic custom headers and system prompt compatibility (#7587)

* fix(provider): fix Anthropic custom headers and system prompt compatibility

- Pass custom_headers via AsyncAnthropic's `default_headers` parameter
  instead of creating a separate httpx.AsyncClient. This avoids
  `isinstance` check failures when multiple httpx installations exist
  on sys.path (e.g. bundled Python + system Python).

- Use list format for the `system` parameter (`[{"type": "text", ...}]`)
  instead of a plain string. The list format is supported by the official
  Anthropic API and is also compatible with third-party API proxies that
  reject the string format.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(provider): fix Anthropic custom headers and system prompt compatibility

- Pass custom_headers via AsyncAnthropic's `default_headers` parameter
  instead of creating a separate httpx.AsyncClient. This avoids
  `isinstance` check failures when multiple httpx installations exist
  on sys.path (e.g. bundled Python + system Python).

- Use list format for the `system` parameter (`[{"type": "text", ...}]`)
  instead of a plain string. The list format is supported by the official
  Anthropic API and is also compatible with third-party API proxies that
  reject the string format.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add test unit

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>

* perf: improve logic of adding models

Co-authored-by: piexian <piexian@users.noreply.github.com>

* chore: remove redundant logger messages and improve log clarity

Co-authored-by: Copilot <copilot@github.com>

* chore: ruff format

* docs: update knowledge base docs

closes: #7962

* fix(#7907): send_message_to_user cron 场景下 session 容错 (#7911)

* fix: send_message_to_user cron 场景下 session 容错 (#7907)

- LLM 在主动场景可能只传 session_id 而非完整三段式,
from_str 失败时用 current_session 补全前两段。

Co-authored-by: Copilot <copilot@github.com>

* fix: 限制 session 补全仅对裸 session_id 生效,避免误修带冒号的错误输入 (#7907)

* feat: add session information to cron job payload

Co-authored-by: Copilot <copilot@github.com>

* fix: improve clarity and consistency of safety mode prompts

Co-authored-by: Copilot <copilot@github.com>

---------

Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Weilong Liao <37870767+Soulter@users.noreply.github.com>
Co-authored-by: Soulter <905617992@qq.com>

* perf: tool rendering in conversation page (#7937)

* fix(dashboard): route conversation history tool messages through ToolCallCard

When viewing conversation history, large tool outputs (e.g. a single
git log --stat producing tens of KB) caused the browser renderer to
freeze. Root cause: formattedMessages mapped every role (including
tool / system / _checkpoint) into user/bot bubbles, and bot plain
strings went through markstream-vue's MarkdownRender. Single 88KB
tool messages plus 88-of-them adding up to ~349KB of synchronous
markdown parsing was enough to block the main thread for 5+ seconds.

This patch:

- Indexes tool-role messages by tool_call_id
- Filters formattedMessages to user/assistant only — tool, system and
  _checkpoint roles no longer render as standalone bubbles
- Converts assistant.tool_calls (OpenAI shape, with tc.name/tc.arguments
  fallbacks) into the existing tool_call MessagePart, attaching the
  paired result so MessageList's ToolCallCard renders it (default
  collapsed, no longer feeds large strings into the markdown renderer)
- Drops empty placeholder plain parts when an assistant message only
  carries tool_calls
- Sets ts/finished_ts to 0 as a sentinel: ToolCallCard.toolCallDuration
  returns "" when startTime <= 0, suppressing a misleading "0ms"
  duration that would otherwise appear because conversation history
  has no real timing data

Behavior change: tool results are now embedded in their assistant's
ToolCallCard.result instead of appearing as separate bot bubbles.
This matches the main chat UI's behavior.

Fixes #7929
Refs #7372 #7456

* style(dashboard): use single scrollbar in conversation history preview

ToolCallCard's result/args panes have their own max-height + overflow,
which produced a nested scrollbar when nested inside the history
preview's already-scrollable .conversation-messages-container. Override
those constraints inside the preview only — the outer 500px-bounded
container already provides scroll bounds, so a single scrollbar feels
cleaner. The main chat UI is unaffected.

---------

Co-authored-by: wanger <wanger@example.com>

* fix: ruff format

* feat: add python tool timeout param (#7953)

* feat: add python tool timeout param

* Update python.py

---------

Co-authored-by: Weilong Liao <37870767+Soulter@users.noreply.github.com>

* fix: 钉钉连接超时后自动重连失败 (#7924)

* fix: improve DingTalk adapter error handling in run() method

* fix: add retry logic for DingTalk SDK task unexpected exit

* fix: use task.add_done_callback to wake thread on task completion, handle UnboundLocalError

* refactor: extract retry logic into handle_retry helper function

---------

Co-authored-by: Blueteemo <Blueteemo@users.noreply.github.com>

---------

Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: leonforcode <leonbeyourside01@gmail.com>
Co-authored-by: AstralSolipsism <134063164+AstralSolipsism@users.noreply.github.com>
Co-authored-by: Pink YuDeer <wer00001@outlook.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: piexian <piexian@users.noreply.github.com>
Co-authored-by: NayukiMeko <ChibaNayuki@163.com>
Co-authored-by: wanger <122891289+10knamesmore@users.noreply.github.com>
Co-authored-by: wanger <wanger@example.com>
Co-authored-by: Haoran Xu <3230105281@zju.edu.cn>
Co-authored-by: 千岚之夏 <108566281+Blueteemo@users.noreply.github.com>
Co-authored-by: Blueteemo <Blueteemo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. lgtm This PR has been approved by a maintainer 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