Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/runtime/executor/openai_compat_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy
return nil, err
}

// 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


url := strings.TrimSuffix(baseURL, "/") + "/chat/completions"
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(translated))
if err != nil {
Expand Down
Loading