Implement stream_options.include_usage for OpenAI-compatible chat completions streaming #29
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.
Fixes the issue where
stream_options.include_usagewas ignored in streaming chat completions requests, making the server always include usage information in the final chunk regardless of the client's preference.Problem
When sending a streaming chat completions request with
stream_options.include_usage, the parameter was completely ignored:The server would always include usage information in the final streaming chunk, regardless of whether
include_usagewastrue,false, or not provided.Solution
This PR implements proper handling of
stream_options.include_usageaccording to the OpenAI API specification:stream_optionsfield to theslot_paramsstructure to store stream options from the requeststream_optionsinparams_from_json_cmpl()to extract the options from the request JSONto_json_oaicompat_chat_stream()to conditionally include usage information only wheninclude_usageis explicitly set totrueBehavior Changes
stream_options.include_usage = true: Usage information is included in a final chunk with empty choices arraystream_options.include_usage = false: No usage information is included in the streamstream_optionsprovided: No usage information is included (backward compatible)Example Usage
To include usage information:
{ "stream": true, "stream_options": {"include_usage": true} }Response includes final chunk:
To exclude usage information:
{ "stream": true, "stream_options": {"include_usage": false} }No usage chunk is sent.
Backward Compatibility
This change is fully backward compatible. Existing clients that don't specify
stream_optionswill continue to work as before (no usage information in streaming responses).Testing
The exact curl command from the original issue now works correctly! 🚀
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.