Skip to content

Commit 23404e8

Browse files
kobolthomas
andauthored
fix: ensure ResponseUsage token fields are int, not None (fixes #1179) (#1181)
### 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 Co-authored-by: thomas <[email protected]>
1 parent bc32b99 commit 23404e8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/agents/models/chatcmpl_stream_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,9 @@ async def handle_stream(
493493
final_response.output = outputs
494494
final_response.usage = (
495495
ResponseUsage(
496-
input_tokens=usage.prompt_tokens,
497-
output_tokens=usage.completion_tokens,
498-
total_tokens=usage.total_tokens,
496+
input_tokens=usage.prompt_tokens or 0,
497+
output_tokens=usage.completion_tokens or 0,
498+
total_tokens=usage.total_tokens or 0,
499499
output_tokens_details=OutputTokensDetails(
500500
reasoning_tokens=usage.completion_tokens_details.reasoning_tokens
501501
if usage.completion_tokens_details

0 commit comments

Comments
 (0)