Skip to content

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

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

Merged
merged 1 commit into from
Jul 18, 2025

Conversation

kobol
Copy link
Contributor

@kobol kobol commented Jul 18, 2025

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
Copy link
Member

@seratch seratch left a comment

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