Skip to content

Commit 23411e5

Browse files
fix(litellm): Classify embeddings correctly (#4918)
Check the `call_type` value to distinguish embeddings from chats. The `client` decorator sets `call_type` by introspecting the function name and wraps all of the top-level `litellm` functions. If users import from `litellm.llms`, embedding calls still may appear as chats, but the input callback we provide does not have enough information in that case. Closes #4908
1 parent a311e3b commit 23411e5

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

sentry_sdk/integrations/litellm.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ def _input_callback(kwargs):
4848
model = full_model
4949
provider = "unknown"
5050

51-
messages = kwargs.get("messages", [])
52-
operation = "chat" if messages else "embeddings"
51+
call_type = kwargs.get("call_type", None)
52+
if call_type == "embedding":
53+
operation = "embeddings"
54+
else:
55+
operation = "chat"
5356

5457
# Start a new span/transaction
5558
span = get_start_span_function()(
@@ -71,6 +74,7 @@ def _input_callback(kwargs):
7174
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, operation)
7275

7376
# Record messages if allowed
77+
messages = kwargs.get("messages", [])
7478
if messages and should_send_default_pii() and integration.include_prompts:
7579
set_data_normalized(
7680
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages, unpack=False

tests/integrations/litellm/test_litellm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,15 @@ def test_embeddings_create(sentry_init, capture_events):
208208
)
209209
events = capture_events()
210210

211+
messages = [{"role": "user", "content": "Some text to test embeddings"}]
211212
mock_response = MockEmbeddingResponse()
212213

213214
with start_transaction(name="litellm test"):
214-
# For embeddings, messages would be empty
215215
kwargs = {
216216
"model": "text-embedding-ada-002",
217217
"input": "Hello!",
218-
"messages": [], # Empty for embeddings
218+
"messages": messages,
219+
"call_type": "embedding",
219220
}
220221

221222
_input_callback(kwargs)

0 commit comments

Comments
 (0)