Skip to content

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

Open
@kakasoo

Description

@kakasoo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions