[PR #6784] [MERGED] openai: support include_usage stream option to return final usage chunk #74520

Closed
opened 2026-05-05 06:38:44 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/6784
Author: @anuraaga
Created: 9/13/2024
Status: Merged
Merged: 12/13/2024
Merged by: @ParthSareen

Base: mainHead: openai-stream-usage


📝 Commits (10+)

📊 Changes

3 files changed (+176 additions, -33 deletions)

View changed files

📝 docs/openai.md (+4 -0)
📝 openai/openai.go (+84 -33)
📝 openai/openai_test.go (+88 -0)

📄 Description

Fixes #5200

Reads stream_options parameter in chat or completion requests and when include_usage is true, adds a null usage field to all chunks with choices and before completing, returns a final chunk with no choices and the usage.

Being able to both return no usage field and a null usage field required some complexity in how to marshal it to JSON, I have tried to document it in a comment.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/6784 **Author:** [@anuraaga](https://github.com/anuraaga) **Created:** 9/13/2024 **Status:** ✅ Merged **Merged:** 12/13/2024 **Merged by:** [@ParthSareen](https://github.com/ParthSareen) **Base:** `main` ← **Head:** `openai-stream-usage` --- ### 📝 Commits (10+) - [`220108d`](https://github.com/ollama/ollama/commit/220108d3f4ed70c511ab30b39936963fb04df096) openai: support include_usage stream option to return final usage chunk - [`7ed8143`](https://github.com/ollama/ollama/commit/7ed81437fe28233f270f25e76926ec55f09ec6f0) Document stream_options - [`7355ab3`](https://github.com/ollama/ollama/commit/7355ab37035c680219153b12911f9d7549ac0c6d) Return empty choices on usage chunk - [`c6509bf`](https://github.com/ollama/ollama/commit/c6509bf76e71bd396da00da9e7b42c87d3429df8) Merge branch 'main' of https://github.com/ollama/ollama into openai-stream-usage - [`01e52d2`](https://github.com/ollama/ollama/commit/01e52d24bc00defcd9bcc6d995b11df0f08bc6f1) Merge branch 'main' of https://github.com/ollama/ollama into openai-stream-usage - [`97abd7b`](https://github.com/ollama/ollama/commit/97abd7bfeab38cc5bfb31796551c1948c917cf1b) Code cleanup - [`2536ffe`](https://github.com/ollama/ollama/commit/2536ffe0aba390bef45541f376aaa0ec68676763) More cleanup - [`d139ba4`](https://github.com/ollama/ollama/commit/d139ba487a043a5850e76f2f8c1bf2a886a6d3c5) Return null, not 0's - [`6d3249e`](https://github.com/ollama/ollama/commit/6d3249ef3de63819cc535de284399cdc9486a7b9) Merge branch 'parth/openai-stream-usage' into openai-stream-usage - [`439496d`](https://github.com/ollama/ollama/commit/439496dec261fa953453897cd7f6ae129b8240f3) Updating to still include completion chunk data ### 📊 Changes **3 files changed** (+176 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `docs/openai.md` (+4 -0) 📝 `openai/openai.go` (+84 -33) 📝 `openai/openai_test.go` (+88 -0) </details> ### 📄 Description Fixes #5200 Reads `stream_options` parameter in chat or completion requests and when `include_usage` is true, adds a `null` usage field to all chunks with choices and before completing, returns a final chunk with no choices and the usage. Being able to both return no `usage` field and a `null` `usage` field required some complexity in how to marshal it to JSON, I have tried to document it in a comment. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-05 06:38:45 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#74520