Skip to content

Commit 090f990

Browse files
authored
Enhance reasoning_tokens handling in response processing (#318)
Add error handling and type coercion for reasoning_tokens.
1 parent c6b21d9 commit 090f990

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/cai/sdk/agents/models/openai_chatcompletions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,17 @@ async def get_response(
760760
and response.usage.completion_tokens_details
761761
and hasattr(response.usage.completion_tokens_details, "reasoning_tokens")
762762
):
763-
reasoning_tokens = response.usage.completion_tokens_details.reasoning_tokens
763+
# Guard against None or unexpected types for reasoning_tokens
764+
try:
765+
reasoning_tokens = response.usage.completion_tokens_details.reasoning_tokens
766+
if reasoning_tokens is None:
767+
reasoning_tokens = 0
768+
else:
769+
# coerce numeric-like values to int
770+
reasoning_tokens = int(reasoning_tokens)
771+
except Exception:
772+
reasoning_tokens = 0
773+
764774
self.total_reasoning_tokens += reasoning_tokens
765775

766776
# Process costs for non-streaming mode

0 commit comments

Comments
 (0)