chore(litellm): dispatch core events around LiteLLM completion calls #22145
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 375 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.355
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #22145"
general
weight: 0.35
avara1986
Description
Part of APPSEC-71906. This PR contains only the LiteLLM contrib change, so it can be reviewed without the AI Guard code that uses it (#22039, stacked on this branch).
traced_completionandtraced_acompletion(completion,acompletion,text_completion,atext_completion) now dispatch three core events:-
litellm.<op>.beforewith(state, args, kwargs), before the provider call;-
litellm.<op>.afterwith(state, args, kwargs, response), after a non-streamed call returns;-
litellm.<op>.finallywith(state,), always.stateis one dict per call, shared by the listeners. The contrib imports no product: products register their own listeners (AI Guard does in #22039).Exception, so aDDBlockException(aBaseExceptionthat products raise to block a call) left the litellm span, and the Router span, open. They now catch(Exception, DDBlockException)and end the span with the error. OtherBaseExceptions, such as a cancelled call, are not changed here (see #21833).fallback_dispatch(#21792), which still wraps only the provider call.docs/contributing-integrations.rstand the llmobs-integrations implementation guide now say that a wrapper ending its own context (dispatch_end_event=False) catches(Exception, DDBlockException).Customer impact
None on its own: without listeners the events are no-ops, and no integration raises
DDBlockExceptioninside these calls before #22039. No release note.Testing
tests/contrib/litellm/test_litellm.py:-
test_litellm_call_events: order and arguments of the events for sync and async, streamed and non-streamed calls; streamed calls get no.after.-
test_litellm_stream_base_exception_finishes_span: aDDBlockExceptionraised by a.beforelistener on a streamed call, sync and async, direct and through a Router, finishes every span with an error.llmobs::litellmsuite passing locally on litellm 1.65.4 (py3.10) and latest (py3.14): 423 passed, 20 skipped, no snapshot or cassette change.scripts/lint checkspasses.Risks
Low. Three core dispatches and one dict per call, no-ops without listeners. Measured locally with
timeitaroundtraced_completionand a stubbed provider (no listeners): the added events cost about 0.33 µs per call, 0.6% of the wrapper on litellm 1.65.4 (57.7 µs) and 0.7% on latest (47.6 µs). No benchmark scenario covers the litellm call path.Additional Notes
#21833 (open) changes the same
exceptclauses toexcept BaseExceptionto end the span of a cancelled call. Whichever merges first, the other rebases; both changes are compatible.🤖 Generated with Claude Code
[APPSEC-71906]: https://datadoghq.atlassian.net/browse/APPSEC-71906?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ