Skip to content

Commit c0c0e9c

Browse files
feat(litellm): Add async callbacks (#5969)
Add asynchronous variants of callbacks to `litellm.input_callback` and `litellm.success_callback`.
1 parent ea74b63 commit c0c0e9c

File tree

4 files changed

+847
-10
lines changed

4 files changed

+847
-10
lines changed

scripts/populate_tox/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
"litellm": {
239239
"package": "litellm",
240240
"deps": {
241-
"*": ["anthropic", "google-genai"],
241+
"*": ["anthropic", "google-genai", "pytest-asyncio"],
242242
},
243243
},
244244
"litestar": {

sentry_sdk/integrations/litellm.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None:
8282
provider = "unknown"
8383

8484
call_type = kwargs.get("call_type", None)
85-
if call_type == "embedding":
85+
if call_type == "embedding" or call_type == "aembedding":
8686
operation = "embeddings"
8787
else:
8888
operation = "chat"
@@ -160,6 +160,10 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None:
160160
set_data_normalized(span, attribute, value)
161161

162162

163+
async def _async_input_callback(kwargs: "Dict[str, Any]") -> None:
164+
return _input_callback(kwargs)
165+
166+
163167
def _success_callback(
164168
kwargs: "Dict[str, Any]",
165169
completion_response: "Any",
@@ -224,12 +228,30 @@ def _success_callback(
224228
is_streaming = kwargs.get("stream")
225229
# Callback is fired multiple times when streaming a response.
226230
# Streaming flag checked at https://github.com/BerriAI/litellm/blob/33c3f13443eaf990ac8c6e3da78bddbc2b7d0e7a/litellm/litellm_core_utils/litellm_logging.py#L1603
227-
if is_streaming is not True or "complete_streaming_response" in kwargs:
231+
if (
232+
is_streaming is not True
233+
or "complete_streaming_response" in kwargs
234+
or "async_complete_streaming_response" in kwargs
235+
):
228236
span = metadata.pop("_sentry_span", None)
229237
if span is not None:
230238
span.__exit__(None, None, None)
231239

232240

241+
async def _async_success_callback(
242+
kwargs: "Dict[str, Any]",
243+
completion_response: "Any",
244+
start_time: "datetime",
245+
end_time: "datetime",
246+
) -> None:
247+
return _success_callback(
248+
kwargs,
249+
completion_response,
250+
start_time,
251+
end_time,
252+
)
253+
254+
233255
def _failure_callback(
234256
kwargs: "Dict[str, Any]",
235257
exception: Exception,
@@ -311,10 +333,14 @@ def setup_once() -> None:
311333
litellm.input_callback = input_callback or []
312334
if _input_callback not in litellm.input_callback:
313335
litellm.input_callback.append(_input_callback)
336+
if _async_input_callback not in litellm.input_callback:
337+
litellm.input_callback.append(_async_input_callback)
314338

315339
litellm.success_callback = success_callback or []
316340
if _success_callback not in litellm.success_callback:
317341
litellm.success_callback.append(_success_callback)
342+
if _async_success_callback not in litellm.success_callback:
343+
litellm.success_callback.append(_async_success_callback)
318344

319345
litellm.failure_callback = failure_callback or []
320346
if _failure_callback not in litellm.failure_callback:

0 commit comments

Comments
 (0)