Skip to content

fix(usage): Normalize None token detail objects on Usage initialization - #2141

Merged
seratch merged 1 commit into
openai:mainfrom
codefromthecrypt:fix/non-streaming-token-details
Dec 3, 2025
Merged

fix(usage): Normalize None token detail objects on Usage initialization#2141
seratch merged 1 commit into
openai:mainfrom
codefromthecrypt:fix/non-streaming-token-details

Conversation

@codefromthecrypt

@codefromthecrypt codefromthecrypt commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

Extends #2034 to handle providers that return None for entire input_tokens_details and output_tokens_details objects (not just the fields within them). This affects non-streaming responses.

Related to #1179 (which fixed the streaming case).

Some providers like llama-stack return null for these optional fields in their JSON responses. The OpenAI SDK maps these to None in Python. Previously, passing None to the Usage constructor would fail Pydantic validation before __post_init__ could normalize them.

This PR uses Pydantic's BeforeValidator to normalize None values at the field level, before Pydantic's type validation runs.

@seratch

seratch commented Dec 2, 2025

Copy link
Copy Markdown
Member

It seems the tests are failing

@seratch
seratch marked this pull request as draft December 2, 2025 23:36
@codefromthecrypt
codefromthecrypt force-pushed the fix/non-streaming-token-details branch 2 times, most recently from 881c90a to 053bdc1 Compare December 3, 2025 02:14
Extends openai#2034 to handle providers that return None for entire
input_tokens_details and output_tokens_details objects (not just the
fields within them). This affects non-streaming responses.

Related to openai#1179 (which fixed the streaming case).

Some providers like llama-stack return null for these optional fields
in their JSON responses. The OpenAI SDK maps these to None in Python.
Previously, passing None to the Usage constructor would fail Pydantic
validation before __post_init__ could normalize them.

This PR uses Pydantic's BeforeValidator to normalize None values at
the field level, before Pydantic's type validation runs. The validators
also convert Chat Completions API types (PromptTokensDetails,
CompletionTokensDetails) to Responses API types (InputTokensDetails,
OutputTokensDetails).
@codefromthecrypt
codefromthecrypt force-pushed the fix/non-streaming-token-details branch from 053bdc1 to aac01ef Compare December 3, 2025 02:47
@codefromthecrypt
codefromthecrypt marked this pull request as ready for review December 3, 2025 02:52
@codefromthecrypt

Copy link
Copy Markdown
Contributor Author

sorry about that @seratch

@seratch seratch added this to the 0.6.x milestone Dec 3, 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

@seratch
seratch merged commit 509ddda into openai:main Dec 3, 2025
9 checks passed
@codefromthecrypt
codefromthecrypt deleted the fix/non-streaming-token-details branch December 3, 2025 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants