Skip to content

usage field may contain null, but the TypeScript type definition does not reflect this #1402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
kakasoo opened this issue Mar 20, 2025 · 4 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@kakasoo
Copy link

kakasoo commented Mar 20, 2025

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

Issue Title

usage field may contain null, but its type does not reflect this

Issue Description

The usage field in the response is described in the documentation as follows:

When present, it contains a null value except for the last chunk, which contains the token usage statistics for the entire request.

However, the TypeScript type definition does not include null as a possible value:

usage?: CompletionsAPI.CompletionUsage;

Since the field explicitly states that it can be null in most cases (except for the last chunk), the correct type definition should be:

usage?: CompletionsAPI.CompletionUsage | null;

This mismatch may cause issues in TypeScript projects that rely on strict type checking.

  • /**
    * An optional field that will only be present when you set
    * `stream_options: {"include_usage": true}` in your request. When present, it
    * contains a null value **except for the last chunk** which contains the token
    * usage statistics for the entire request.
    *
    * **NOTE:** If the stream is interrupted or cancelled, you may not receive the
    * final usage chunk which contains the total token usage for the request.
    */
    usage?: CompletionsAPI.CompletionUsage;

To Reproduce

This is a clear bug since the comments and the actual code do not match.

Code snippets

OS

macOS

Node version

Node v22.3.0

Library version

4.87.4

@kakasoo
Copy link
Author

kakasoo commented Mar 20, 2025

This is a clear bug since the comments and the actual code do not match. We discovered this issue while working on our open-source project, Agentica, where it is reproducible. Please let us know if you have any questions or need further details!

@sunrabbit123
Copy link

sunrabbit123 commented Mar 20, 2025

represent solution

just use streaming with openai sdk
and you can get usage: null, but openai sdk type is not to present null

@sunrabbit123
Copy link

it's break change pr

#1391

it's api docs
https://platform.openai.com/docs/api-reference/chat-streaming/streaming

Image

@Specy
Copy link

Specy commented Mar 23, 2025

I found this issue also in RealtimeClientEvent for the beta of the realtime api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants