fix(dashscope): enhance usage parsing robustness to prevent VSCode cr…#2425
Open
magucas wants to merge 1 commit intofarion1231:mainfrom
Open
fix(dashscope): enhance usage parsing robustness to prevent VSCode cr…#2425magucas wants to merge 1 commit intofarion1231:mainfrom
magucas wants to merge 1 commit intofarion1231:mainfrom
Conversation
…ashes Enhanced build_anthropic_usage_from_responses() to handle null, missing, empty, and partial usage fields gracefully. This prevents VSCode Extension crashes with "Cannot read properties of null (reading 'output_tokens')" when connecting to DashScope (Alibaba Cloud Bailian) models. Changes: - Added defensive null checks and empty object detection - Implemented OpenAI field name fallbacks (prompt_tokens/completion_tokens) - Added comprehensive logging for malformed usage scenarios - Fixed streaming SSE event handlers with null-safe usage access - Preserved cache token fields even when input/output tokens are missing This ensures the proxy never crashes on malformed Responses API usage objects, returning valid Anthropic-compatible usage structures (input_tokens/output_tokens) in all cases.
Author
|
fix bug for ISSUE: #2422 |
Owner
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
PR: Fix DashScope Usage Parsing to Prevent VSCode Extension Crashes
Summary / 概述
Enhanced
build_anthropic_usage_from_responses()and streaming SSE handlers to handle null, missing, empty, and partial usage fields gracefully. This prevents VSCode Extension crashes with "Cannot read properties of null (reading 'output_tokens')" when connecting to DashScope (Alibaba Cloud Bailian) models.问题背景:DashScope(阿里云百炼)的 Responses API 在流式响应中可能返回畸形的 usage 对象:
usage: null- 完全缺失usage: {}- 空对象usage: {"input_tokens": 100}- 只有部分字段prompt_tokens/completion_tokens)这些情况会导致 VSCode Extension 访问
usage.output_tokens时崩溃。解决方案:实施防御性解析策略,确保在任何畸形情况下都返回有效的 Anthropic-compatible usage 结构。
Related Issue / 关联 Issue
Fixes #issue-dashscope-usage-null-pointer
Technical Details / 技术细节
Changes / 改动内容
1. Non-streaming Responses API (
transform_responses.rs)关键改进:
!v.is_null() && v.is_object()obj.is_empty()→ 使用默认值prompt_tokens/completion_tokens2. Streaming Responses API (
streaming_responses.rs)3. Streaming SSE Handler (
streaming.rs) - Merged with upstream结合了上游的缓存策略(延迟到 [DONE] 发送 message_delta)和防御性 usage 处理:
Modified Files / 修改文件
✅
src-tauri/src/proxy/providers/transform_responses.rsbuild_anthropic_usage_from_responses()with defensive parsing✅
src-tauri/src/proxy/providers/streaming_responses.rsusage_jsonis alwaysSome(Value), neverNone✅
src-tauri/src/proxy/providers/streaming.rs(merged with upstream)usage_json→latest_usage→ defaults✅
CHANGELOG.mdTest Coverage / 测试覆盖
已验证的场景:
usage: null→ 返回{"input_tokens": 0, "output_tokens": 0}usage: {}→ 返回{"input_tokens": 0, "output_tokens": 0}usage: {"input_tokens": 100}→ 返回{"input_tokens": 100, "output_tokens": 0}Rebase 验证:
origin/main(21e2d68)Screenshots / 截图
Cannot read properties of null (reading 'output_tokens')Impact Analysis / 影响分析
Benefits / 优势
Potential Risks / 潜在风险
Compatibility / 兼容性
Implementation Notes / 实现细节
Field Name Resolution Priority / 字段名解析优先级
Error Handling Strategy / 错误处理策略
Logging Strategy / 日志策略
Checklist / 检查清单
origin/mainsuccessfullypnpm typecheckpasses / 通过 TypeScript 类型检查pnpm format:checkpasses / 通过代码格式检查cargo clippypasses (if Rust code changed) / 通过 Clippy 检查(如修改了 Rust 代码)Note: Checklist items marked with
[ ]should be run before final merge.Additional Context / 额外信息
Related Commits / 相关提交
fix(proxy): preserve scoped reasoning_content for tool callsfix(proxy): dedupe streaming message_deltafix(dashscope): enhance usage parsing robustnessTesting Environment / 测试环境
Cannot read properties of null (reading 'output_tokens')Future Improvements / 未来改进方向
Commit Details: