From 57a25d5b75c6a2728b7561755a941ec17f0c0b7c Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 7 Jan 2026 14:32:57 +0100 Subject: [PATCH] Fix openai count_tokens --- sentry_sdk/integrations/openai.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index 53d464c3c4..a2c7cc8d1d 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -84,9 +84,12 @@ def setup_once() -> None: AsyncResponses.create = _wrap_async_responses_create(AsyncResponses.create) def count_tokens(self: "OpenAIIntegration", s: str) -> int: - if self.tiktoken_encoding is not None: + if self.tiktoken_encoding is None: + return 0 + try: return len(self.tiktoken_encoding.encode_ordinary(s)) - return 0 + except Exception: + return 0 def _capture_exception(exc: "Any", manual_span_cleanup: bool = True) -> None: @@ -157,8 +160,8 @@ def _calculate_token_usage( output_tokens += count_tokens(message) elif hasattr(response, "choices"): for choice in response.choices: - if hasattr(choice, "message"): - output_tokens += count_tokens(choice.message) + if hasattr(choice, "message") and hasattr(choice.message, "content"): + output_tokens += count_tokens(choice.message.content) # Do not set token data if it is 0 input_tokens = input_tokens or None