Repository: sashabaranov/go-openai
Pull request #1022 (2025-06-17 11:39:30)

Fix for removing usage in every stream chunk response.
### **Describe the change**

This PR fixes an issue where the `usage` field is always present in streamed completion responses, even when `stream_options.include_usage` is explicitly set to `false`.

By changing the `Usage` field in `CompletionResponse` to a pointer and tagging it with `omitempty`, we ensure that if `usage` is not included in the actual API response, it will be omitted from the parsed Go struct as well.

This improves correctness and reduces confusion when users want to opt out of usage metadata in streaming mode.

---

### **Provide OpenAI documentation link**

API Reference: [https://platform.openai.com/docs/api-reference/completions/create](https://platform.openai.com/docs/api-reference/completions/create)

---

### **Describe your solution**

Modified the `CompletionResponse` struct to update the `Usage` field:

```go
Usage *Usage `json:"usage,omitempty"`
```

This allows Go’s `encoding/json` to omit the field when it's not present in the streamed response. This makes the client-side response cleaner and honors the behavior expected when `include_usage` is set to `false`.

No new libraries or dependencies were added.

---

### **Tests**

* Manually tested by calling `/v1/completions` with both `stream_options.include_usage: true` and `false`
* Verified that:

  * When `include_usage: true`, the field is present.
  * When `include_usage: false`, the field is omitted in streamed chunks.
* Updated integration tests to assert presence/absence of `Usage` field conditionally based on stream option.

---

### **Additional context**

This PR improves alignment with OpenAI’s API behavior and helps consumers avoid parsing unnecessary fields.

Issue: #1021 



codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sashabaranov/go-openai/pull/1022?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 85.83%. Comparing base [(`a931bf7`)](https://app.codecov.io/gh/sashabaranov/go-openai/commit/a931bf7e85af9d39af414de21d907b6835281519?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov) to head [(`c989f94`)](https://app.codecov.io/gh/sashabaranov/go-openai/commit/c989f94a3c19fa642591fda64392c67ba39d3d37?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #1022   +/-   ##
=======================================
  Coverage   85.83%   85.83%           
=======================================
  Files          43       43           
  Lines        2316     2316           
=======================================
  Hits         1988     1988           
  Misses        305      305           
  Partials       23       23           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/sashabaranov/go-openai/pull/1022?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>
Hritik003: @sashabaranov can you please look into this?
Hritik003: @sashabaranov A reminder to look into this?

Patch:
diff --git a/completion.go b/completion.go
index 21d4897..02ce7b0 100644
--- a/completion.go
+++ b/completion.go
@@ -242,7 +242,7 @@ type CompletionResponse struct {
 	Created int64              `json:"created"`
 	Model   string             `json:"model"`
 	Choices []CompletionChoice `json:"choices"`
-	Usage   Usage              `json:"usage"`
+	Usage   *Usage             `json:"usage,omitempty"`
 
 	httpHeader
 }


Test patch:
diff --git a/completion_test.go b/completion_test.go
index 27e2d15..f0ead0d 100644
--- a/completion_test.go
+++ b/completion_test.go
@@ -192,7 +192,7 @@ func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
 	}
 	inputTokens *= n
 	completionTokens := completionReq.MaxTokens * len(prompts) * n
-	res.Usage = openai.Usage{
+	res.Usage = &openai.Usage{
 		PromptTokens:     inputTokens,
 		CompletionTokens: completionTokens,
 		TotalTokens:      inputTokens + completionTokens,