@@ -34,7 +34,8 @@ async def __call__(self, *args, **kwargs):
3434from litellm .litellm_core_utils .logging_worker import GLOBAL_LOGGING_WORKER
3535from litellm .llms .custom_httpx .http_handler import AsyncHTTPHandler , HTTPHandler
3636from openai import AsyncOpenAI , OpenAI
37- from openai .types import CompletionUsage
37+ from openai .types import Completion , CompletionUsage
38+ from openai .types .completion_choice import CompletionChoice
3839
3940from sentry_sdk import start_transaction
4041from sentry_sdk ._types import BLOB_DATA_SUBSTITUTE
@@ -2651,6 +2652,7 @@ def test_response_without_usage(
26512652 kwargs = {
26522653 "model" : "gpt-3.5-turbo" ,
26532654 "messages" : messages ,
2655+ "call_type" : "completion" ,
26542656 }
26552657
26562658 _input_callback (kwargs )
@@ -2674,6 +2676,7 @@ def test_response_without_usage(
26742676 kwargs = {
26752677 "model" : "gpt-3.5-turbo" ,
26762678 "messages" : messages ,
2679+ "call_type" : "completion" ,
26772680 }
26782681
26792682 _input_callback (kwargs )
@@ -2733,6 +2736,7 @@ def test_litellm_message_truncation(sentry_init, capture_events):
27332736 kwargs = {
27342737 "model" : "gpt-3.5-turbo" ,
27352738 "messages" : messages ,
2739+ "call_type" : "completion" ,
27362740 }
27372741
27382742 _input_callback (kwargs )
@@ -3847,3 +3851,113 @@ def test_embeddings_data_collection(
38473851 assert span_data [SPANDATA .GEN_AI_OPERATION_NAME ] == "embeddings"
38483852 assert span_data [SPANDATA .GEN_AI_REQUEST_MODEL ] == "text-embedding-ada-002"
38493853 assert span_data [SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ] == 5
3854+
3855+
3856+ def test_text_completion_operation_name (
3857+ sentry_init ,
3858+ capture_events ,
3859+ get_model_response ,
3860+ reset_litellm_executor ,
3861+ ):
3862+ """text_completion calls get the text_completion op and record their prompt."""
3863+ sentry_init (
3864+ integrations = [LiteLLMIntegration (include_prompts = True )],
3865+ disabled_integrations = [StdlibIntegration ],
3866+ traces_sample_rate = 1.0 ,
3867+ send_default_pii = True ,
3868+ stream_gen_ai_spans = False ,
3869+ )
3870+ events = capture_events ()
3871+
3872+ client = OpenAI (api_key = "test-key" )
3873+
3874+ model_response = get_model_response (
3875+ Completion (
3876+ id = "cmpl-test" ,
3877+ choices = [
3878+ CompletionChoice (finish_reason = "stop" , index = 0 , text = "Test response" )
3879+ ],
3880+ created = 1234567890 ,
3881+ model = "gpt-3.5-turbo-instruct" ,
3882+ object = "text_completion" ,
3883+ usage = CompletionUsage (
3884+ prompt_tokens = 10 ,
3885+ completion_tokens = 20 ,
3886+ total_tokens = 30 ,
3887+ ),
3888+ ),
3889+ serialize_pydantic = True ,
3890+ request_headers = {"X-Stainless-Raw-Response" : "true" },
3891+ )
3892+
3893+ with mock .patch .object (
3894+ client .completions ._client ._client ,
3895+ "send" ,
3896+ return_value = model_response ,
3897+ ), start_transaction (name = "litellm test" ):
3898+ litellm .text_completion (
3899+ model = "gpt-3.5-turbo-instruct" ,
3900+ prompt = "Hello!" ,
3901+ client = client ,
3902+ )
3903+
3904+ litellm_utils .executor .shutdown (wait = True )
3905+
3906+ (event ,) = events
3907+ (span ,) = [s for s in event ["spans" ] if s ["origin" ] == "auto.ai.litellm" ]
3908+
3909+ assert span ["op" ] == OP .GEN_AI_TEXT_COMPLETION
3910+ assert span ["description" ] == "text_completion gpt-3.5-turbo-instruct"
3911+ assert span ["data" ][SPANDATA .GEN_AI_OPERATION_NAME ] == "text_completion"
3912+ assert json .loads (span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]) == [
3913+ {"role" : "user" , "content" : "Hello!" }
3914+ ]
3915+
3916+
3917+ def test_responses_operation_name (
3918+ sentry_init ,
3919+ capture_events ,
3920+ get_model_response ,
3921+ nonstreaming_responses_model_response ,
3922+ reset_litellm_executor ,
3923+ ):
3924+ """Responses API calls get the responses op and record their input."""
3925+ sentry_init (
3926+ integrations = [LiteLLMIntegration (include_prompts = True )],
3927+ disabled_integrations = [StdlibIntegration ],
3928+ traces_sample_rate = 1.0 ,
3929+ send_default_pii = True ,
3930+ stream_gen_ai_spans = False ,
3931+ )
3932+ events = capture_events ()
3933+
3934+ client = HTTPHandler ()
3935+
3936+ model_response = get_model_response (
3937+ nonstreaming_responses_model_response ,
3938+ serialize_pydantic = True ,
3939+ )
3940+
3941+ with mock .patch .object (
3942+ client ,
3943+ "post" ,
3944+ return_value = model_response ,
3945+ ), start_transaction (name = "litellm test" ):
3946+ litellm .responses (
3947+ model = "gpt-4" ,
3948+ input = "Hello!" ,
3949+ client = client ,
3950+ api_key = "test-key" ,
3951+ )
3952+
3953+ litellm_utils .executor .shutdown (wait = True )
3954+
3955+ (event ,) = events
3956+ (span ,) = [s for s in event ["spans" ] if s ["origin" ] == "auto.ai.litellm" ]
3957+
3958+ assert span ["op" ] == OP .GEN_AI_RESPONSES
3959+ assert span ["description" ] == "responses gpt-4"
3960+ assert span ["data" ][SPANDATA .GEN_AI_OPERATION_NAME ] == "responses"
3961+ assert json .loads (span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]) == [
3962+ {"role" : "user" , "content" : "Hello!" }
3963+ ]
0 commit comments