-
Notifications
You must be signed in to change notification settings - Fork 637
fix(litellm): Set operation name from call type instead of chat fallback #6792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |
|
|
||
| if TYPE_CHECKING: | ||
| from datetime import datetime | ||
| from typing import Any, Dict, List | ||
| from typing import Any, Dict, List, Tuple | ||
|
|
||
| try: | ||
| import litellm # type: ignore[import-not-found] | ||
|
|
@@ -35,6 +35,19 @@ | |
| # to every callback, so it lives and dies with the request. | ||
| _SPAN_KEY = "_sentry_span" | ||
|
|
||
| # Call types whose gen_ai operation name we can determine accurately. Everything | ||
| # else is not instrumented, since guessing records wrong data. | ||
| _CALL_TYPE_OPERATIONS: "Dict[Any, Tuple[str, str]]" = { | ||
| "completion": ("chat", consts.OP.GEN_AI_CHAT), | ||
| "acompletion": ("chat", consts.OP.GEN_AI_CHAT), | ||
| "text_completion": ("text_completion", consts.OP.GEN_AI_TEXT_COMPLETION), | ||
| "atext_completion": ("text_completion", consts.OP.GEN_AI_TEXT_COMPLETION), | ||
| "embedding": ("embeddings", consts.OP.GEN_AI_EMBEDDINGS), | ||
| "aembedding": ("embeddings", consts.OP.GEN_AI_EMBEDDINGS), | ||
| "responses": ("responses", consts.OP.GEN_AI_RESPONSES), | ||
| "aresponses": ("responses", consts.OP.GEN_AI_RESPONSES), | ||
| } | ||
|
|
||
|
|
||
| def _store_span(kwargs: "Dict[str, Any]", span: "Any") -> None: | ||
| kwargs[_SPAN_KEY] = span | ||
|
|
@@ -83,6 +96,12 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: | |
| if integration is None: | ||
| return | ||
|
|
||
| call_type = kwargs.get("call_type", None) | ||
| if call_type not in _CALL_TYPE_OPERATIONS: | ||
| return | ||
|
|
||
| operation, span_op = _CALL_TYPE_OPERATIONS[call_type] | ||
|
|
||
| # Get key parameters | ||
| full_model = kwargs.get("model", "") | ||
| try: | ||
|
|
@@ -91,33 +110,21 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: | |
| model = full_model | ||
| provider = "unknown" | ||
|
|
||
| call_type = kwargs.get("call_type", None) | ||
| if call_type == "embedding" or call_type == "aembedding": | ||
| operation = "embeddings" | ||
| else: | ||
| operation = "chat" | ||
| span_name = f"{operation} {model}" | ||
|
|
||
| # Start a new span/transaction | ||
| if has_span_streaming_enabled(client.options): | ||
| span = sentry_sdk.traces.start_span( | ||
| name=f"{operation} {model}", | ||
| name=span_name, | ||
| attributes={ | ||
| "sentry.op": ( | ||
| consts.OP.GEN_AI_CHAT | ||
| if operation == "chat" | ||
| else consts.OP.GEN_AI_EMBEDDINGS | ||
| ), | ||
| "sentry.op": span_op, | ||
| "sentry.origin": LiteLLMIntegration.origin, | ||
| }, | ||
| ) | ||
| else: | ||
| span = get_start_span_function()( | ||
| op=( | ||
| consts.OP.GEN_AI_CHAT | ||
| if operation == "chat" | ||
| else consts.OP.GEN_AI_EMBEDDINGS | ||
| ), | ||
| name=f"{operation} {model}", | ||
| op=span_op, | ||
| name=span_name, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New ops skip prompt captureMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit b57b7f9. Configure here. |
||
| origin=LiteLLMIntegration.origin, | ||
| ) | ||
| span.__enter__() | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
_input_callbackreturns early for unmappedcall_types, preventing span creation and disabling exception capture forlitellmcalls likeimage_generation.Severity: MEDIUM
Suggested Fix
To restore exception capturing for all
litellmcall types, ensure a span is created even for unmapped types. Consider creating a fallback span with a generic operation, similar to the previous behavior, instead of returning early from_input_callbackwhen acall_typeis not found.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.