From 2a3c4a18a09dc7f298c9b3b34725394bc3ae496a Mon Sep 17 00:00:00 2001 From: The Mavik <179817126+themavik@users.noreply.github.com> Date: Tue, 10 Feb 2026 11:07:59 +0530 Subject: [PATCH] fix(openai): isolate beta.chat wrappers to prevent cascade failure When the openai SDK removes openai.resources.beta.chat (as in newer versions), the entire streaming wrapper setup fails because all wrappers are in a single try/except block. This means even the valid chat.completions and responses wrappers are never applied. Move the beta.chat wrappers to their own try/except so a missing beta module only affects beta wrappers, not all streaming. Fixes #1260 --- .../providers/openai/instrumentor.py | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/agentops/instrumentation/providers/openai/instrumentor.py b/agentops/instrumentation/providers/openai/instrumentor.py index 9497d7252..4514ac2f8 100644 --- a/agentops/instrumentation/providers/openai/instrumentor.py +++ b/agentops/instrumentation/providers/openai/instrumentor.py @@ -109,19 +109,6 @@ def _custom_wrap(self, **kwargs): async_chat_completion_stream_wrapper(self._tracer), ) - # Beta chat completion streaming wrappers - wrap_function_wrapper( - "openai.resources.beta.chat.completions", - "Completions.parse", - chat_completion_stream_wrapper(self._tracer), - ) - - wrap_function_wrapper( - "openai.resources.beta.chat.completions", - "AsyncCompletions.parse", - async_chat_completion_stream_wrapper(self._tracer), - ) - # Responses API streaming wrappers wrap_function_wrapper( "openai.resources.responses", @@ -136,6 +123,24 @@ def _custom_wrap(self, **kwargs): ) except Exception as e: logger.warning(f"[OPENAI INSTRUMENTOR] Error setting up OpenAI streaming wrappers: {e}") + + # Beta chat completion wrappers (separate try/except because + # openai.resources.beta.chat was removed in newer openai SDK + # versions; a failure here should not prevent the main streaming + # wrappers from being applied). + try: + wrap_function_wrapper( + "openai.resources.beta.chat.completions", + "Completions.parse", + chat_completion_stream_wrapper(self._tracer), + ) + wrap_function_wrapper( + "openai.resources.beta.chat.completions", + "AsyncCompletions.parse", + async_chat_completion_stream_wrapper(self._tracer), + ) + except Exception: + pass # Module not available in this openai version else: if not is_openai_v1(): logger.debug("[OPENAI INSTRUMENTOR] Skipping custom wrapping - not using OpenAI v1")