fix(anthropic): 修复跨供应商迁移后 thinking-only assistant 消息被剥离为空导致反复降级的问题#120
Merged
Conversation
Anthropic 额度恢复后,_strip_thinking_blocks() 剥离 zhipu 签发的 thinking blocks 时,
若 assistant 消息仅含 thinking blocks,content 会变为空列表,触发 Anthropic API 400 错误
(tool_result blocks can only be in user messages),并因语义拒绝分类不记录熔断器失败,
导致每次请求都重复降级到 zhipu 的死循环。
修复:剥离后 content 为空时插入占位 text block {"type":"text","text":"[thinking]"},
保持消息结构合法性。新增 2 个测试用例覆盖该场景。
🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist)
Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_strip_thinking_blocks()在剥离 thinking blocks 后 assistant 消息 content 变为空列表的 edge case{"type": "text", "text": "[thinking]"}保持消息结构合法性问题背景
当 Anthropic 额度用尽(429)时,proxy 自动降级到 zhipu。zhipu 作为原生 Anthropic 兼容端点薄透传代理,会保留响应中的 thinking blocks(含 zhipu 签发的 signature)。当 Anthropic 额度重置、active vendor 切回 Anthropic 后:
_strip_thinking_blocks()剥离所有非 Anthropic 签发的 thinking/redacted_thinking blocks[]invalid_request_error改动详情
src/coding/proxy/vendors/anthropic.py—_strip_thinking_blocks()Before:剥离后 content 为空时仅记录 warning,仍将空列表赋值给 message
After:插入最小占位 text block
{"type": "text", "text": "[thinking]"},满足 Anthropic API 非空约束tests/test_vendors.py— 新增 2 个测试用例test_anthropic_prepare_request_thinking_only_gets_placeholdertest_anthropic_prepare_request_thinking_only_with_tool_result_context设计决策
[thinking]占位文本:简短、无指令性、与原 block type 语义呼应,对模型推理干扰最小AnthropicVendor._prepare_request()的深拷贝分支,zhipu 及其他 vendor 完全不受影响Test plan
uv run pytest tests/test_vendors.py -v— 41 passed (含 2 个新增)uv run pytest tests/ -v— 957 passed, 无回归