Skip to content

Commit e25afa7

Browse files
add attribute check
1 parent 7b7bd06 commit e25afa7

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ def _new_chat_completion_common(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
577577
start_time = time.perf_counter()
578578
response = yield f, args, kwargs
579579

580-
if isinstance(response, Stream):
580+
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
581+
if isinstance(response, Stream) and hasattr(response, "_iterator"):
581582
messages = kwargs.get("messages")
582583

583584
if messages is not None and isinstance(messages, str):
@@ -592,7 +593,9 @@ def _new_chat_completion_common(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
592593
old_iterator=response._iterator,
593594
finish_span=True,
594595
)
595-
elif isinstance(response, AsyncStream):
596+
597+
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
598+
elif isinstance(response, AsyncStream) and hasattr(response, "_iterator"):
596599
messages = kwargs.get("messages")
597600

598601
if messages is not None and isinstance(messages, str):
@@ -1098,6 +1101,7 @@ def _new_responses_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "An
10981101
start_time = time.perf_counter()
10991102
response = yield f, args, kwargs
11001103

1104+
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
11011105
if isinstance(response, Stream):
11021106
input = kwargs.get("input")
11031107

@@ -1114,6 +1118,7 @@ def _new_responses_create_common(f: "Any", *args: "Any", **kwargs: "Any") -> "An
11141118
finish_span=True,
11151119
)
11161120

1121+
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
11171122
elif isinstance(response, AsyncStream):
11181123
input = kwargs.get("input")
11191124

0 commit comments

Comments
 (0)