@@ -136,7 +136,7 @@ def _new_chat_completion_common(f, *args, **kwargs):
136136 )
137137 span .__enter__ ()
138138 try :
139- res = f (* args , ** kwargs )
139+ res = yield f (* args , ** kwargs )
140140 except Exception as e :
141141 _capture_exception (e )
142142 span .__exit__ (None , None , None )
@@ -209,22 +209,35 @@ def new_iterator():
209209
210210def _wrap_chat_completion_create (f ):
211211 # type: (Callable[..., Any]) -> Callable[..., Any]
212+ def _execute_sync (f , * args , ** kwargs ):
213+ # type: (Any, *Any, **Any) -> Any
214+ gen = _new_chat_completion_common (f , * args , ** kwargs )
215+ f , args , kwargs = next (gen )
216+ return gen .send (f (* args , ** kwargs ))
217+
212218 @wraps (f )
213219 @ensure_integration_enabled (OpenAIIntegration , f )
214220 def _sentry_patched_create_sync (* args , ** kwargs ):
215221 # type: (*Any, **Any) -> Any
216- return _new_chat_completion_common (f , * args , ** kwargs )
222+ return _execute_sync (f , * args , ** kwargs )
217223
218224 return _sentry_patched_create_sync
219225
220226
221227def _wrap_async_chat_completion_create (f ):
222228 # type: (Callable[..., Any]) -> Callable[..., Any]
229+
230+ async def _execute_async (f , * args , ** kwargs ):
231+ # type: (Any, *Any, **Any) -> Any
232+ gen = _new_chat_completion_common (f , * args , ** kwargs )
233+ f , args , kwargs = next (gen )
234+ return gen .send (await f (* args , ** kwargs ))
235+
223236 @wraps (f )
224237 @ensure_integration_enabled (OpenAIIntegration , f )
225238 async def _sentry_patched_create_async (* args , ** kwargs ):
226239 # type: (*Any, **Any) -> Any
227- return await _new_chat_completion_common (f , * args , ** kwargs )
240+ return await _execute_async (f , * args , ** kwargs )
228241
229242 return _sentry_patched_create_async
230243
@@ -251,7 +264,7 @@ def _new_embeddings_create_common(f, *args, **kwargs):
251264 if "model" in kwargs :
252265 set_data_normalized (span , "ai.model_id" , kwargs ["model" ])
253266 try :
254- response = f (* args , ** kwargs )
267+ response = yield f (* args , ** kwargs )
255268 except Exception as e :
256269 _capture_exception (e )
257270 raise e from None
@@ -278,21 +291,34 @@ def _new_embeddings_create_common(f, *args, **kwargs):
278291
279292def _wrap_embeddings_create (f ):
280293 # type: (Any) -> Any
294+
295+ def _execute_sync (f , * args , ** kwargs ):
296+ # type: (Any, *Any, **Any) -> Any
297+ gen = _new_embeddings_create_common (f , * args , ** kwargs )
298+ f , args , kwargs = next (gen )
299+ return gen .send (f (* args , ** kwargs ))
300+
281301 @wraps (f )
282302 @ensure_integration_enabled (OpenAIIntegration , f )
283303 def _sentry_patched_create_sync (* args , ** kwargs ):
284304 # type: (*Any, **Any) -> Any
285- return _new_embeddings_create_common (f , * args , ** kwargs )
305+ return _execute_sync (f , * args , ** kwargs )
286306
287307 return _sentry_patched_create_sync
288308
289309
290310def _wrap_async_embeddings_create (f ):
291311 # type: (Any) -> Any
312+ async def _execute_async (f , * args , ** kwargs ):
313+ # type: (Any, *Any, **Any) -> Any
314+ gen = _new_embeddings_create_common (f , * args , ** kwargs )
315+ f , args , kwargs = next (gen )
316+ return gen .send (await f (* args , ** kwargs ))
317+
292318 @wraps (f )
293319 @ensure_integration_enabled (OpenAIIntegration , f )
294320 async def _sentry_patched_create_async (* args , ** kwargs ):
295321 # type: (*Any, **Any) -> Any
296- return await _new_embeddings_create_common (f , * args , ** kwargs )
322+ return await _execute_async (f , * args , ** kwargs )
297323
298324 return _sentry_patched_create_async
0 commit comments