Skip to content

fix: ensure ResponseUsage token fields are int, not None (fixes #1179) - #1181

Merged
rm-openai merged 1 commit into
openai:mainfrom
kobol:fix/responseusage-token-none
Jul 18, 2025
Merged

fix: ensure ResponseUsage token fields are int, not None (fixes #1179)#1181
rm-openai merged 1 commit into
openai:mainfrom
kobol:fix/responseusage-token-none

Conversation

@kobol

@kobol kobol commented Jul 18, 2025

Copy link
Copy Markdown
Contributor

Problem

When using streaming responses, some models or API endpoints may return usage fields (prompt_tokens, completion_tokens, total_tokens) as None or omit them entirely. The current implementation passes these values directly to the ResponseUsage Pydantic model, which expects integers. This causes a validation error:

3 validation errors for ResponseUsage
input_tokens
Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
output_tokens
Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
total_tokens
Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]

Solution

This PR ensures that all token fields passed to ResponseUsage are always integers. If any of the fields are None or missing, they default to 0. This is achieved by using or 0 and explicit is not None checks for nested fields.

Key changes:

  • All input_tokens, output_tokens, total_tokens fields use or 0 fallback.

Impact

  • Fixes Pydantic validation errors for streaming responses with missing/None usage fields.
  • Improves compatibility with OpenAI and third-party models.
  • No breaking changes; only adds robustness.

fixes #1179

@seratch seratch added bug Something isn't working feature:chat-completions labels Jul 18, 2025

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rm-openai
rm-openai merged commit 23404e8 into openai:main Jul 18, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:chat-completions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streaming: ResponseUsage validation error when token fields are None

3 participants