@@ -71,10 +71,13 @@ type Transcription struct {
71
71
// models `gpt-4o-transcribe` and `gpt-4o-mini-transcribe` if `logprobs` is added
72
72
// to the `include` array.
73
73
Logprobs []TranscriptionLogprob `json:"logprobs"`
74
+ // Token usage statistics for the request.
75
+ Usage TranscriptionUsageUnion `json:"usage"`
74
76
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
75
77
JSON struct {
76
78
Text respjson.Field
77
79
Logprobs respjson.Field
80
+ Usage respjson.Field
78
81
ExtraFields map [string ]respjson.Field
79
82
raw string
80
83
} `json:"-"`
@@ -109,6 +112,153 @@ func (r *TranscriptionLogprob) UnmarshalJSON(data []byte) error {
109
112
return apijson .UnmarshalRoot (data , r )
110
113
}
111
114
115
+ // TranscriptionUsageUnion contains all possible properties and values from
116
+ // [TranscriptionUsageTokens], [TranscriptionUsageDuration].
117
+ //
118
+ // Use the [TranscriptionUsageUnion.AsAny] method to switch on the variant.
119
+ //
120
+ // Use the methods beginning with 'As' to cast the union to one of its variants.
121
+ type TranscriptionUsageUnion struct {
122
+ // This field is from variant [TranscriptionUsageTokens].
123
+ InputTokens int64 `json:"input_tokens"`
124
+ // This field is from variant [TranscriptionUsageTokens].
125
+ OutputTokens int64 `json:"output_tokens"`
126
+ // This field is from variant [TranscriptionUsageTokens].
127
+ TotalTokens int64 `json:"total_tokens"`
128
+ // Any of "tokens", "duration".
129
+ Type string `json:"type"`
130
+ // This field is from variant [TranscriptionUsageTokens].
131
+ InputTokenDetails TranscriptionUsageTokensInputTokenDetails `json:"input_token_details"`
132
+ // This field is from variant [TranscriptionUsageDuration].
133
+ Duration float64 `json:"duration"`
134
+ JSON struct {
135
+ InputTokens respjson.Field
136
+ OutputTokens respjson.Field
137
+ TotalTokens respjson.Field
138
+ Type respjson.Field
139
+ InputTokenDetails respjson.Field
140
+ Duration respjson.Field
141
+ raw string
142
+ } `json:"-"`
143
+ }
144
+
145
+ // anyTranscriptionUsage is implemented by each variant of
146
+ // [TranscriptionUsageUnion] to add type safety for the return type of
147
+ // [TranscriptionUsageUnion.AsAny]
148
+ type anyTranscriptionUsage interface {
149
+ implTranscriptionUsageUnion ()
150
+ }
151
+
152
+ func (TranscriptionUsageTokens ) implTranscriptionUsageUnion () {}
153
+ func (TranscriptionUsageDuration ) implTranscriptionUsageUnion () {}
154
+
155
+ // Use the following switch statement to find the correct variant
156
+ //
157
+ // switch variant := TranscriptionUsageUnion.AsAny().(type) {
158
+ // case openai.TranscriptionUsageTokens:
159
+ // case openai.TranscriptionUsageDuration:
160
+ // default:
161
+ // fmt.Errorf("no variant present")
162
+ // }
163
+ func (u TranscriptionUsageUnion ) AsAny () anyTranscriptionUsage {
164
+ switch u .Type {
165
+ case "tokens" :
166
+ return u .AsTokens ()
167
+ case "duration" :
168
+ return u .AsDuration ()
169
+ }
170
+ return nil
171
+ }
172
+
173
+ func (u TranscriptionUsageUnion ) AsTokens () (v TranscriptionUsageTokens ) {
174
+ apijson .UnmarshalRoot (json .RawMessage (u .JSON .raw ), & v )
175
+ return
176
+ }
177
+
178
+ func (u TranscriptionUsageUnion ) AsDuration () (v TranscriptionUsageDuration ) {
179
+ apijson .UnmarshalRoot (json .RawMessage (u .JSON .raw ), & v )
180
+ return
181
+ }
182
+
183
+ // Returns the unmodified JSON received from the API
184
+ func (u TranscriptionUsageUnion ) RawJSON () string { return u .JSON .raw }
185
+
186
+ func (r * TranscriptionUsageUnion ) UnmarshalJSON (data []byte ) error {
187
+ return apijson .UnmarshalRoot (data , r )
188
+ }
189
+
190
+ // Usage statistics for models billed by token usage.
191
+ type TranscriptionUsageTokens struct {
192
+ // Number of input tokens billed for this request.
193
+ InputTokens int64 `json:"input_tokens,required"`
194
+ // Number of output tokens generated.
195
+ OutputTokens int64 `json:"output_tokens,required"`
196
+ // Total number of tokens used (input + output).
197
+ TotalTokens int64 `json:"total_tokens,required"`
198
+ // The type of the usage object. Always `tokens` for this variant.
199
+ Type constant.Tokens `json:"type,required"`
200
+ // Details about the input tokens billed for this request.
201
+ InputTokenDetails TranscriptionUsageTokensInputTokenDetails `json:"input_token_details"`
202
+ // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
203
+ JSON struct {
204
+ InputTokens respjson.Field
205
+ OutputTokens respjson.Field
206
+ TotalTokens respjson.Field
207
+ Type respjson.Field
208
+ InputTokenDetails respjson.Field
209
+ ExtraFields map [string ]respjson.Field
210
+ raw string
211
+ } `json:"-"`
212
+ }
213
+
214
+ // Returns the unmodified JSON received from the API
215
+ func (r TranscriptionUsageTokens ) RawJSON () string { return r .JSON .raw }
216
+ func (r * TranscriptionUsageTokens ) UnmarshalJSON (data []byte ) error {
217
+ return apijson .UnmarshalRoot (data , r )
218
+ }
219
+
220
+ // Details about the input tokens billed for this request.
221
+ type TranscriptionUsageTokensInputTokenDetails struct {
222
+ // Number of audio tokens billed for this request.
223
+ AudioTokens int64 `json:"audio_tokens"`
224
+ // Number of text tokens billed for this request.
225
+ TextTokens int64 `json:"text_tokens"`
226
+ // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
227
+ JSON struct {
228
+ AudioTokens respjson.Field
229
+ TextTokens respjson.Field
230
+ ExtraFields map [string ]respjson.Field
231
+ raw string
232
+ } `json:"-"`
233
+ }
234
+
235
+ // Returns the unmodified JSON received from the API
236
+ func (r TranscriptionUsageTokensInputTokenDetails ) RawJSON () string { return r .JSON .raw }
237
+ func (r * TranscriptionUsageTokensInputTokenDetails ) UnmarshalJSON (data []byte ) error {
238
+ return apijson .UnmarshalRoot (data , r )
239
+ }
240
+
241
+ // Usage statistics for models billed by audio input duration.
242
+ type TranscriptionUsageDuration struct {
243
+ // Duration of the input audio in seconds.
244
+ Duration float64 `json:"duration,required"`
245
+ // The type of the usage object. Always `duration` for this variant.
246
+ Type constant.Duration `json:"type,required"`
247
+ // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
248
+ JSON struct {
249
+ Duration respjson.Field
250
+ Type respjson.Field
251
+ ExtraFields map [string ]respjson.Field
252
+ raw string
253
+ } `json:"-"`
254
+ }
255
+
256
+ // Returns the unmodified JSON received from the API
257
+ func (r TranscriptionUsageDuration ) RawJSON () string { return r .JSON .raw }
258
+ func (r * TranscriptionUsageDuration ) UnmarshalJSON (data []byte ) error {
259
+ return apijson .UnmarshalRoot (data , r )
260
+ }
261
+
112
262
type TranscriptionInclude string
113
263
114
264
const (
@@ -131,11 +281,14 @@ type TranscriptionStreamEventUnion struct {
131
281
Logprobs TranscriptionStreamEventUnionLogprobs `json:"logprobs"`
132
282
// This field is from variant [TranscriptionTextDoneEvent].
133
283
Text string `json:"text"`
134
- JSON struct {
284
+ // This field is from variant [TranscriptionTextDoneEvent].
285
+ Usage TranscriptionTextDoneEventUsage `json:"usage"`
286
+ JSON struct {
135
287
Delta respjson.Field
136
288
Type respjson.Field
137
289
Logprobs respjson.Field
138
290
Text respjson.Field
291
+ Usage respjson.Field
139
292
raw string
140
293
} `json:"-"`
141
294
}
@@ -279,11 +432,14 @@ type TranscriptionTextDoneEvent struct {
279
432
// [create a transcription](https://platform.openai.com/docs/api-reference/audio/create-transcription)
280
433
// with the `include[]` parameter set to `logprobs`.
281
434
Logprobs []TranscriptionTextDoneEventLogprob `json:"logprobs"`
435
+ // Usage statistics for models billed by token usage.
436
+ Usage TranscriptionTextDoneEventUsage `json:"usage"`
282
437
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
283
438
JSON struct {
284
439
Text respjson.Field
285
440
Type respjson.Field
286
441
Logprobs respjson.Field
442
+ Usage respjson.Field
287
443
ExtraFields map [string ]respjson.Field
288
444
raw string
289
445
} `json:"-"`
@@ -318,6 +474,57 @@ func (r *TranscriptionTextDoneEventLogprob) UnmarshalJSON(data []byte) error {
318
474
return apijson .UnmarshalRoot (data , r )
319
475
}
320
476
477
+ // Usage statistics for models billed by token usage.
478
+ type TranscriptionTextDoneEventUsage struct {
479
+ // Number of input tokens billed for this request.
480
+ InputTokens int64 `json:"input_tokens,required"`
481
+ // Number of output tokens generated.
482
+ OutputTokens int64 `json:"output_tokens,required"`
483
+ // Total number of tokens used (input + output).
484
+ TotalTokens int64 `json:"total_tokens,required"`
485
+ // The type of the usage object. Always `tokens` for this variant.
486
+ Type constant.Tokens `json:"type,required"`
487
+ // Details about the input tokens billed for this request.
488
+ InputTokenDetails TranscriptionTextDoneEventUsageInputTokenDetails `json:"input_token_details"`
489
+ // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
490
+ JSON struct {
491
+ InputTokens respjson.Field
492
+ OutputTokens respjson.Field
493
+ TotalTokens respjson.Field
494
+ Type respjson.Field
495
+ InputTokenDetails respjson.Field
496
+ ExtraFields map [string ]respjson.Field
497
+ raw string
498
+ } `json:"-"`
499
+ }
500
+
501
+ // Returns the unmodified JSON received from the API
502
+ func (r TranscriptionTextDoneEventUsage ) RawJSON () string { return r .JSON .raw }
503
+ func (r * TranscriptionTextDoneEventUsage ) UnmarshalJSON (data []byte ) error {
504
+ return apijson .UnmarshalRoot (data , r )
505
+ }
506
+
507
+ // Details about the input tokens billed for this request.
508
+ type TranscriptionTextDoneEventUsageInputTokenDetails struct {
509
+ // Number of audio tokens billed for this request.
510
+ AudioTokens int64 `json:"audio_tokens"`
511
+ // Number of text tokens billed for this request.
512
+ TextTokens int64 `json:"text_tokens"`
513
+ // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
514
+ JSON struct {
515
+ AudioTokens respjson.Field
516
+ TextTokens respjson.Field
517
+ ExtraFields map [string ]respjson.Field
518
+ raw string
519
+ } `json:"-"`
520
+ }
521
+
522
+ // Returns the unmodified JSON received from the API
523
+ func (r TranscriptionTextDoneEventUsageInputTokenDetails ) RawJSON () string { return r .JSON .raw }
524
+ func (r * TranscriptionTextDoneEventUsageInputTokenDetails ) UnmarshalJSON (data []byte ) error {
525
+ return apijson .UnmarshalRoot (data , r )
526
+ }
527
+
321
528
type AudioTranscriptionNewParams struct {
322
529
// The audio file object (not file name) to transcribe, in one of these formats:
323
530
// flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
0 commit comments