feat(chat): add completion_tokens_details to CompletionUsage#411
Merged
nezhyborets merged 1 commit intomainfrom Apr 29, 2026
Merged
feat(chat): add completion_tokens_details to CompletionUsage#411nezhyborets merged 1 commit intomainfrom
nezhyborets merged 1 commit intomainfrom
Conversation
OpenAI's chat completion `usage` payload includes a `completion_tokens_details` object for reasoning models, audio outputs, and Predicted Outputs. The library was silently dropping this on decode, making fields like reasoning_tokens unreachable. Mirror the existing `PromptTokensDetails` shape with a new `CompletionTokensDetails` struct exposing accepted_prediction_tokens, audio_tokens, reasoning_tokens, and rejected_prediction_tokens. Fields are `Int?` because each only appears under specific conditions (reasoning_tokens for o-series, audio_tokens for audio output, the prediction fields for Predicted Outputs). The new init parameter has a default value, so existing call sites in tests continue to compile unchanged. Closes #258 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for OpenAI Chat Completions usage.completion_tokens_details by extending the ChatResult.CompletionUsage model to decode (optionally) reasoning/audio/predicted-output token breakdowns.
Changes:
- Add
completionTokensDetailstoChatResult.CompletionUsagewith a new nestedCompletionTokensDetailsmodel (all fields optional) and corresponding coding key. - Add decoder tests covering payloads both with and without
completion_tokens_details.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Tests/OpenAITests/OpenAITestsDecoder.swift | Adds decoder coverage for completion_tokens_details presence/absence. |
| Sources/OpenAI/Public/Models/ChatResult.swift | Extends CompletionUsage with completionTokensDetails and a new nested CompletionTokensDetails Codable struct. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
nezhyborets
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #258.
OpenAI's chat completion
usagepayload includes acompletion_tokens_detailsobject that surfaces three pieces of information that are otherwise unreachable today:reasoning_tokens— emitted by o-series reasoning models.audio_tokens— emitted when the model produces audio output.accepted_prediction_tokens/rejected_prediction_tokens— emitted when Predicted Outputs are used.This PR mirrors the existing
PromptTokensDetailspattern with a new nestedCompletionTokensDetailsstruct onChatResult.CompletionUsage. Fields areInt?because each only appears under specific request conditions, and we don't want a missing field to fail decoding.The new
CompletionUsage.initparameter has a default ofnil, so existing call sites in tests and downstream code continue to compile unchanged.Test plan
testCompletionUsageWithCompletionTokensDetails) round-trips a representative reasoning-model usage payload.testCompletionUsageDecodesWithoutCompletionTokensDetails) confirms backward-compat decode of payloads that don't include the new field.swift test— all 177 tests pass locally.🤖 Generated with Claude Code