Skip to content

Commit 667e306

Browse files
committed
openai: Remove is_raw_response attribute from StreamWrapper
1 parent 01d1617 commit 667e306

File tree

2 files changed

+2
-12
lines changed
  • instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai

2 files changed

+2
-12
lines changed

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
196196
_record_operation_duration_metric(self.operation_duration_metric, error_attributes, start_time)
197197
raise
198198

199-
is_raw_response = _is_raw_response(result)
200199
if kwargs.get("stream"):
201200
return StreamWrapper(
202201
stream=result,
@@ -208,12 +207,12 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
208207
start_time=start_time,
209208
token_usage_metric=self.token_usage_metric,
210209
operation_duration_metric=self.operation_duration_metric,
211-
is_raw_response=is_raw_response,
212210
)
213211

214212
logger.debug(f"openai.resources.chat.completions.Completions.create result: {result}")
215213

216214
# if the caller is using with_raw_response we need to parse the output to get the response class we expect
215+
is_raw_response = _is_raw_response(result)
217216
if is_raw_response:
218217
result = result.parse()
219218
response_attributes = _get_attributes_from_response(
@@ -271,7 +270,6 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
271270
_record_operation_duration_metric(self.operation_duration_metric, error_attributes, start_time)
272271
raise
273272

274-
is_raw_response = _is_raw_response(result)
275273
if kwargs.get("stream"):
276274
return StreamWrapper(
277275
stream=result,
@@ -283,12 +281,12 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
283281
start_time=start_time,
284282
token_usage_metric=self.token_usage_metric,
285283
operation_duration_metric=self.operation_duration_metric,
286-
is_raw_response=is_raw_response,
287284
)
288285

289286
logger.debug(f"openai.resources.chat.completions.AsyncCompletions.create result: {result}")
290287

291288
# if the caller is using with_raw_response we need to parse the output to get the response class we expect
289+
is_raw_response = _is_raw_response(result)
292290
if is_raw_response:
293291
result = result.parse()
294292
response_attributes = _get_attributes_from_response(

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def __init__(
4747
start_time: float,
4848
token_usage_metric: Histogram,
4949
operation_duration_metric: Histogram,
50-
is_raw_response: bool,
5150
):
5251
# we need to wrap the original response even in case of raw_responses
5352
super().__init__(stream)
@@ -60,7 +59,6 @@ def __init__(
6059
self.token_usage_metric = token_usage_metric
6160
self.operation_duration_metric = operation_duration_metric
6261
self.start_time = start_time
63-
self.is_raw_response = is_raw_response
6462

6563
self.response_id = None
6664
self.model = None
@@ -125,8 +123,6 @@ def __exit__(self, exc_type, exc_value, traceback):
125123
def __iter__(self):
126124
stream = self.__wrapped__
127125
try:
128-
if self.is_raw_response:
129-
stream = stream.parse()
130126
for chunk in stream:
131127
self.process_chunk(chunk)
132128
yield chunk
@@ -145,8 +141,6 @@ async def __aexit__(self, exc_type, exc_value, traceback):
145141
async def __aiter__(self):
146142
stream = self.__wrapped__
147143
try:
148-
if self.is_raw_response:
149-
stream = stream.parse()
150144
async for chunk in stream:
151145
self.process_chunk(chunk)
152146
yield chunk
@@ -171,8 +165,6 @@ def parse(self):
171165
start_time=self.start_time,
172166
token_usage_metric=self.token_usage_metric,
173167
operation_duration_metric=self.operation_duration_metric,
174-
# Crucially, mark the new wrapper as NOT raw after parsing
175-
is_raw_response=False,
176168
)
177169

178170
# Handle original sync/async iterators accordingly

0 commit comments

Comments
 (0)