← Back to Pull requests

chore(litellm): dispatch core events around LiteLLM completion calls #22145

Open
unknown opened this pull request with 375 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 27, 2026 08:21 UTC
Author

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_completion and traced_acompletion (completion, acompletion, text_completion, atext_completion) now dispatch three core events:

  • - litellm.<op>.before with (state, args, kwargs), before the provider call;
    - litellm.<op>.after with (state, args, kwargs, response), after a non-streamed call returns;
    - litellm.<op>.finally with (state,), always.

    state is one dict per call, shared by the listeners. The contrib imports no product: products register their own listeners (AI Guard does in #22039).

  • The four stream branches (including the two Router ones) caught only Exception, so a DDBlockException (a BaseException that 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. Other BaseExceptions, such as a cancelled call, are not changed here (see #21833).

  • The events wrap fallback_dispatch (#21792), which still wraps only the provider call.

  • docs/contributing-integrations.rst and 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 DDBlockException inside 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: a DDBlockException raised by a .before listener on a streamed call, sync and async, direct and through a Router, finishes every span with an error.
  • llmobs::litellm suite passing locally on litellm 1.65.4 (py3.10) and latest (py3.14): 423 passed, 20 skipped, no snapshot or cassette change. scripts/lint checks passes.
  • Risks

    Low. Three core dispatches and one dict per call, no-ops without listeners. Measured locally with timeit around traced_completion and 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 except clauses to except BaseException to 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

    Diff: 375 lines (+281 / -94) across 4 files Reviewer: avara1986 Review duration: 5692m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.9% (MEDIUM) · Higher = larger blast radius
    47.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.5% · Safe to merge
    25.5%