Skip to content

Commit 99387d5

Browse files
committed
Add exec_sync and exec_async to correctly handle function execution in patched method
1 parent 3979682 commit 99387d5

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _sentry_patched_create_common(f, *args, **kwargs):
9696
span.__enter__()
9797

9898
try:
99-
result = f(*args, **kwargs)
99+
result = yield f(*args, **kwargs)
100100
except Exception as exc:
101101
_capture_exception(exc)
102102
span.__exit__(None, None, None)
@@ -173,21 +173,34 @@ def new_iterator():
173173

174174
def _wrap_message_create(f):
175175
# type: (Any) -> Any
176+
def _execute_sync(f, *args, **kwargs):
177+
# type: (Any, *Any, **Any) -> Any
178+
gen = _sentry_patched_create_common(f, *args, **kwargs)
179+
f, args, kwargs = next(gen)
180+
return gen.send(f(*args, **kwargs))
181+
176182
@wraps(f)
177183
@ensure_integration_enabled(AnthropicIntegration, f)
178184
def _sentry_patched_create_sync(*args, **kwargs):
179185
# type: (*Any, **Any) -> Any
180-
return _sentry_patched_create_common(f, *args, **kwargs)
186+
return _execute_sync(f, *args, **kwargs)
181187

182188
return _sentry_patched_create_sync
183189

184190

185191
def _wrap_async_message_create(f):
186192
# type: (Any) -> Any
193+
194+
async def _execute_async(f, *args, **kwargs):
195+
# type: (Any, *Any, **Any) -> Any
196+
gen = _sentry_patched_create_common(f, *args, **kwargs)
197+
f, args, kwargs = next(gen)
198+
return gen.send(await f(*args, **kwargs))
199+
187200
@wraps(f)
188201
@ensure_integration_enabled(AnthropicIntegration, f)
189202
async def _sentry_patched_create_async(*args, **kwargs):
190203
# type: (*Any, **Any) -> Any
191-
return await _sentry_patched_create_common(f, *args, **kwargs)
204+
return await _execute_async(f, *args, **kwargs)
192205

193206
return _sentry_patched_create_async

sentry_sdk/integrations/openai.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

210210
def _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

221227
def _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

279292
def _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

290310
def _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

Comments
 (0)