修复:处理TokenCounterProcessor中tiktoken特殊标记编码错误导致请求失败的问题#45
Merged
mirrorange merged 1 commit intomirrorange:mainfrom Feb 22, 2026
Merged
Conversation
用户消息包含 <|endofprompt|> 等 OpenAI 特殊 token 时, tiktoken encoder.encode() 抛出 ValueError 导致整个流式响应中断。 添加 disallowed_special=() 参数将特殊 token 当作普通文本编码, 并增加 try-except 兜底确保 token 计数不会影响主流程。 (cherry picked from commit 460b97f)
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.
概要
<|endofprompt|>)时请求崩溃的问题disallowed_special=()将特殊 token 当作普通文本编码try-except兜底,确保 token 计数永远不会中断主请求流程问题描述
当用户消息中包含 tiktoken
cl100k_base的特殊 token(如<|endofprompt|>)时,encoder.encode()抛出ValueError,该异常通过 lazy generator 在流式传输过程中冒泡,导致整个请求失败(流式/非流式均受影响)。
Token 计数仅用于使用量估算,属于非关键功能,应遵循 fail-open 设计原则。
修改内容
app/processors/claude_ai/token_counter_processor.py_calculate_input_tokens:使用disallowed_special=()编码 + 异常降级估算_calculate_output_tokens:同上预期
<|endofprompt|>的流式请求 — 正常返回~