Fix missing streaming usage tracking for OpenAI-compatible providers#2111
Fix missing streaming usage tracking for OpenAI-compatible providers#2111luispater merged 1 commit intorouter-for-me:devfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a bug affecting OpenAI-compatible providers where streaming requests failed to report token usage statistics. The core issue stemmed from the omission of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request correctly identifies and fixes an issue where usage statistics were not being tracked for streaming requests with OpenAI-compatible providers. The fix involves adding stream_options.include_usage: true to the request payload, which is the right approach. I have one suggestion to improve the robustness of the implementation by handling a potential error that is currently being ignored.
|
|
||
| // Request usage data in the final streaming chunk so that token statistics | ||
| // are captured even when the upstream is an OpenAI-compatible provider. | ||
| translated, _ = sjson.SetBytes(translated, "stream_options.include_usage", true) |
There was a problem hiding this comment.
It's a best practice in Go to handle errors rather than ignoring them with _. The sjson.SetBytes function can return an error, for instance, if the translated byte slice does not contain valid JSON. While an error might be unlikely in the current code flow, explicitly checking for it makes the code more robust against future changes and prevents potential silent failures.
| translated, _ = sjson.SetBytes(translated, "stream_options.include_usage", true) | |
| translated, err = sjson.SetBytes(translated, "stream_options.include_usage", true) | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to set stream_options.include_usage: %w", err) | |
| } |
There was a problem hiding this comment.
sjson.SetBytes only errors on invalid path syntax. A hardcoded "stream_options.include_usage" with true can't fail. Same _ pattern used in qwen_executor:359 and all over claude_executor. Can switch to explicit check if the owner want, just matching existing style.
OpenAI-compatible upstreams don't return usage data in streaming mode unless
stream_options.include_usageis explicitly set totruein the request. This was already handled inkimi_executorandqwen_executor, but missed inopenai_compat_executor.As a result, all streaming requests through
openai-compatibilityproviders showed 0 tokens in the usage statistics panel, while non-streaming requests worked fine.Fix: Add
stream_options.include_usage: trueto the translated streaming payload inOpenAICompatExecutor.ExecuteStream(), matching the existing pattern in kimi/qwen executors.