From 301c4b8a0654b2795a914b247422dfe649176ae9 Mon Sep 17 00:00:00 2001 From: colin-sentry <161344340+colin-sentry@users.noreply.github.com> Date: Fri, 12 Jul 2024 12:19:01 -0400 Subject: [PATCH] OpenAI: Lazy initialize tiktoken to avoid http at import time (#3287) --- sentry_sdk/integrations/openai.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index b2c9500026..052d65f7a6 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -32,10 +32,13 @@ try: import tiktoken # type: ignore - enc = tiktoken.get_encoding("cl100k_base") + enc = None # lazy initialize def count_tokens(s): # type: (str) -> int + global enc + if enc is None: + enc = tiktoken.get_encoding("cl100k_base") return len(enc.encode_ordinary(s)) logger.debug("[OpenAI] using tiktoken to count tokens")