21
21
from sentry_sdk .tracing import Span
22
22
23
23
try :
24
+ try :
25
+ from openai import NOT_GIVEN
26
+ except ImportError :
27
+ NOT_GIVEN = None
28
+
24
29
from openai .resources .chat .completions import Completions , AsyncCompletions
25
30
from openai .resources import Embeddings , AsyncEmbeddings
26
31
@@ -196,12 +201,13 @@ def _set_input_data(
196
201
}
197
202
for key , attribute in kwargs_keys_to_attributes .items ():
198
203
value = kwargs .get (key )
199
- if value is not None :
204
+
205
+ if value is not NOT_GIVEN and value is not None :
200
206
set_data_normalized (span , attribute , value )
201
207
202
208
# Input attributes: Tools
203
209
tools = kwargs .get ("tools" )
204
- if tools is not None and len (tools ) > 0 :
210
+ if tools is not NOT_GIVEN and tools is not None and len (tools ) > 0 :
205
211
set_data_normalized (
206
212
span , SPANDATA .GEN_AI_REQUEST_AVAILABLE_TOOLS , safe_serialize (tools )
207
213
)
@@ -429,8 +435,7 @@ def _execute_sync(f: Any, *args: Any, **kwargs: Any) -> Any:
429
435
return e .value
430
436
431
437
@wraps (f )
432
- def _sentry_patched_create_sync (* args , ** kwargs ):
433
- # type: (Any, Any) -> Any
438
+ def _sentry_patched_create_sync (* args : Any , ** kwargs : Any ) -> Any :
434
439
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
435
440
if integration is None or "messages" not in kwargs :
436
441
# no "messages" means invalid call (in all versions of openai), let it return error
@@ -441,10 +446,8 @@ def _sentry_patched_create_sync(*args, **kwargs):
441
446
return _sentry_patched_create_sync
442
447
443
448
444
- def _wrap_async_chat_completion_create (f ):
445
- # type: (Callable[..., Any]) -> Callable[..., Any]
446
- async def _execute_async (f , * args , ** kwargs ):
447
- # type: (Any, Any, Any) -> Any
449
+ def _wrap_async_chat_completion_create (f : Callable [..., Any ]) -> Callable [..., Any ]:
450
+ async def _execute_async (f : Any , * args : Any , ** kwargs : Any ) -> Any :
448
451
gen = _new_chat_completion_common (f , * args , ** kwargs )
449
452
450
453
try :
@@ -464,8 +467,7 @@ async def _execute_async(f, *args, **kwargs):
464
467
return e .value
465
468
466
469
@wraps (f )
467
- async def _sentry_patched_create_async (* args , ** kwargs ):
468
- # type: (Any, Any) -> Any
470
+ async def _sentry_patched_create_async (* args : Any , ** kwargs : Any ) -> Any :
469
471
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
470
472
if integration is None or "messages" not in kwargs :
471
473
# no "messages" means invalid call (in all versions of openai), let it return error
@@ -476,8 +478,7 @@ async def _sentry_patched_create_async(*args, **kwargs):
476
478
return _sentry_patched_create_async
477
479
478
480
479
- def _new_embeddings_create_common (f , * args , ** kwargs ):
480
- # type: (Any, Any, Any) -> Any
481
+ def _new_embeddings_create_common (f : Any , * args : Any , ** kwargs : Any ) -> Any :
481
482
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
482
483
if integration is None :
483
484
return f (* args , ** kwargs )
@@ -499,10 +500,8 @@ def _new_embeddings_create_common(f, *args, **kwargs):
499
500
return response
500
501
501
502
502
- def _wrap_embeddings_create (f ):
503
- # type: (Any) -> Any
504
- def _execute_sync (f , * args , ** kwargs ):
505
- # type: (Any, Any, Any) -> Any
503
+ def _wrap_embeddings_create (f : Any ) -> Any :
504
+ def _execute_sync (f : Any , * args : Any , ** kwargs : Any ) -> Any :
506
505
gen = _new_embeddings_create_common (f , * args , ** kwargs )
507
506
508
507
try :
@@ -522,8 +521,7 @@ def _execute_sync(f, *args, **kwargs):
522
521
return e .value
523
522
524
523
@wraps (f )
525
- def _sentry_patched_create_sync (* args , ** kwargs ):
526
- # type: (Any, Any) -> Any
524
+ def _sentry_patched_create_sync (* args : Any , ** kwargs : Any ) -> Any :
527
525
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
528
526
if integration is None :
529
527
return f (* args , ** kwargs )
@@ -533,10 +531,8 @@ def _sentry_patched_create_sync(*args, **kwargs):
533
531
return _sentry_patched_create_sync
534
532
535
533
536
- def _wrap_async_embeddings_create (f ):
537
- # type: (Any) -> Any
538
- async def _execute_async (f , * args , ** kwargs ):
539
- # type: (Any, Any, Any) -> Any
534
+ def _wrap_async_embeddings_create (f : Any ) -> Any :
535
+ async def _execute_async (f : Any , * args : Any , ** kwargs : Any ):
540
536
gen = _new_embeddings_create_common (f , * args , ** kwargs )
541
537
542
538
try :
@@ -556,8 +552,7 @@ async def _execute_async(f, *args, **kwargs):
556
552
return e .value
557
553
558
554
@wraps (f )
559
- async def _sentry_patched_create_async (* args , ** kwargs ):
560
- # type: (Any, Any) -> Any
555
+ async def _sentry_patched_create_async (* args : Any , ** kwargs : Any ) -> Any :
561
556
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
562
557
if integration is None :
563
558
return await f (* args , ** kwargs )
@@ -567,8 +562,7 @@ async def _sentry_patched_create_async(*args, **kwargs):
567
562
return _sentry_patched_create_async
568
563
569
564
570
- def _new_responses_create_common (f , * args , ** kwargs ):
571
- # type: (Any, Any, Any) -> Any
565
+ def _new_responses_create_common (f : Any , * args : Any , ** kwargs : Any ) -> Any :
572
566
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
573
567
if integration is None :
574
568
return f (* args , ** kwargs )
@@ -592,10 +586,8 @@ def _new_responses_create_common(f, *args, **kwargs):
592
586
return response
593
587
594
588
595
- def _wrap_responses_create (f ):
596
- # type: (Any) -> Any
597
- def _execute_sync (f , * args , ** kwargs ):
598
- # type: (Any, Any, Any) -> Any
589
+ def _wrap_responses_create (f : Any ) -> Any :
590
+ def _execute_sync (f : Any , * args : Any , ** kwargs : Any ) -> Any :
599
591
gen = _new_responses_create_common (f , * args , ** kwargs )
600
592
601
593
try :
@@ -615,8 +607,7 @@ def _execute_sync(f, *args, **kwargs):
615
607
return e .value
616
608
617
609
@wraps (f )
618
- def _sentry_patched_create_sync (* args , ** kwargs ):
619
- # type: (Any, Any) -> Any
610
+ def _sentry_patched_create_sync (* args : Any , ** kwargs : Any ) -> Any :
620
611
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
621
612
if integration is None :
622
613
return f (* args , ** kwargs )
@@ -626,10 +617,8 @@ def _sentry_patched_create_sync(*args, **kwargs):
626
617
return _sentry_patched_create_sync
627
618
628
619
629
- def _wrap_async_responses_create (f ):
630
- # type: (Any) -> Any
631
- async def _execute_async (f , * args , ** kwargs ):
632
- # type: (Any, Any, Any) -> Any
620
+ def _wrap_async_responses_create (f : Any ) -> Any :
621
+ async def _execute_async (f : Any , * args : Any , ** kwargs : Any ):
633
622
gen = _new_responses_create_common (f , * args , ** kwargs )
634
623
635
624
try :
@@ -649,8 +638,7 @@ async def _execute_async(f, *args, **kwargs):
649
638
return e .value
650
639
651
640
@wraps (f )
652
- async def _sentry_patched_responses_async (* args , ** kwargs ):
653
- # type: (Any, Any) -> Any
641
+ async def _sentry_patched_responses_async (* args : Any , ** kwargs : Any ) -> Any :
654
642
integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
655
643
if integration is None :
656
644
return await f (* args , ** kwargs )
0 commit comments