Repository navigation
fix(logging): stamp request async-ness at the client wrapper so async entrypoints never double-log - #33496
Conversation
… entrypoints never double-log The a* flag allowlist in _is_sync_litellm_request required every async entrypoint to plant its own flag in litellm_params; entrypoints that forgot (anthropic_messages, agenerate_content) were misclassified as sync and fired CustomLogger sync hooks on top of the async ones. The client decorator already knows which wrapper ran, so each wrapper now records logging_obj.is_async_entrypoint (first-wins across nested client calls sharing a logging object; agenerate_content delegates to the sync generate_content wrapper mid-request) and the classifier reads the recorded bit, keeping the flag heuristic only for logging objects built outside the client decorator. Replaces the anthropic_messages call-type guard from the previous commit. Resolves LIT-4475
Greptile SummaryThis PR fixes double-logging for async-only entrypoints (e.g.,
Confidence Score: 5/5Safe to merge; the change is well-scoped to the logging dispatch path and does not touch request routing, auth, or cost calculation. The first-wins stamp on No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/litellm_logging.py | Adds is_async_entrypoint field and converts _is_sync_litellm_request to instance method; all three call-sites updated correctly; fallback heuristic preserved for out-of-@client logging objects. |
| litellm/utils.py | Stamps is_async_entrypoint = False in sync wrapper and True in wrapper_async, both guarded by if logging_obj.is_async_entrypoint is None for correct first-wins behavior. |
| litellm/litellm_core_utils/streaming_handler.py | Single call-site update: _is_sync_litellm_request(litellm_params) without call_type kwarg; straightforward and correct. |
| tests/test_litellm/litellm_core_utils/test_litellm_logging.py | Test suite updated to instance-method signature; two new end-to-end tests added covering flagless-call-type dedup and first-wins nested-delegation; existing tests correctly updated with explicit is_async_entrypoint = True setup. |
| tests/test_litellm/passthrough/test_passthrough_main.py | Static-method call replaced by constructing a fresh LitellmLogging (with is_async_entrypoint=None) to exercise the flag fallback path; correctly verifies allm_passthrough_route still classifies as async. |
| tests/test_litellm/proxy/guardrails/test_deferred_guardrail_logging.py | Minimal change: static-method call updated to instance-method call on an already-existing logging_obj; semantics unchanged. |
Reviews (2): Last reviewed commit: "test(logging): assert exactly-once async..." | Re-trigger Greptile
…c-outer sync-inner wrapper flow Extends the first-wins regression test to also count CustomLogger hook firings with the executor sync dispatch open, per review feedback on the PR; kills the sync-stamp mutants
|
The gap named in the review is addressed by 7e96d77: test_client_wrapper_stamp_is_first_wins_across_nested_calls now registers a recording CustomLogger with the executor sync dispatch open around the nested async-outer/sync-inner call and asserts events == ["async"], i.e. exactly one async hook and zero sync hooks for the agenerate_content-style delegation pattern, with a deterministic executor drain instead of a sleep. The suggestion in the review (assert no sync callbacks fired during the nested call, not just the bit value) is implemented verbatim in that commit |
|
The fix in 7e96d77 is exactly what was needed. Looking at the updated test:
This kills all three sync-stamp mutants. LGTM. |
f4494e6
into
litellm_lit4447_dd_dedupe
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Relevant issues
Linear ticket
Resolves LIT-4475
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Stacked on #33481. Live proxy on localhost:4475 (real Postgres, real Anthropic and Gemini APIs), DataDog logs intake redirected to a local recording sink via
DD_BASE_URL; config hassuccess_callback: ["datadog"]pluscache: true(the cache setting registers the legacy"cache"string callback that opens the sync dispatch for async calls)Before (base of this PR, i.e. with only the #33481 call-type guard), one native gemini call double-logs; this upgrades LIT-4475 from suspected to reproduced:
After (this PR), the same calls plus a
/chat/completionscontrol and a streaming/v1/messagescall:LiteLLM_SpendLogshas exactly one row per request throughout (the duplicate-request_id group-by returns zero rows). SDK-level probes confirm the sync entrypoint keeps its sync hook:litellm.google_genai.generate_content(sync) fireslog_success_eventexactly once,litellm.agenerate_contentfiresasync_log_success_eventexactly onceType
🐛 Bug Fix
Changes
Follow-up to #33481, which fixed the async-request misclassification for
anthropic_messageswith a call-type guard. This PR replaces that guard with the general mechanism so every async-only entrypoint is classified correctly, including theagenerate_contentsibling that this PR reproduces liveThe
@clientdecorator already knows whether a request is sync or async; it selectswrapperorwrapper_asyncfromis_async_callable(original_function). Each wrapper now records that fact on the logging object it runs (logging_obj.is_async_entrypoint), and_is_sync_litellm_requestreads the recorded bit instead of guessing from an allowlist ofa*flags inlitellm_params. The flag heuristic remains only as a fallback for logging objects constructed outside@client(passthrough, realtime). Theanthropic_messagescall-type guard from #33481 is deleted; the bit covers itThe stamp is first-wins: an async entrypoint that internally invokes a sync
@clientfunction with the shared logging object (exactly whatagenerate_contentdoes when delegating togenerate_content) keeps its async classification; without first-wins the inner sync wrapper flips the bit back and the gemini double-log survives, which is how the live repro caught itThis is the same pattern the logging object already uses for
call_typeandstream: a fact the wrapper knows at entry, recorded at creation, read downstream. It replaces the per-entrypoint flag planting (acompletion,aresponses,async_call) whose forget-one-flag failure mode produced LIT-4192, LIT-4447, and LIT-4475Behavior changes
Async-only entrypoints that set no
a*flag stop double-firing CustomLogger hooks; they now deliver via the async hooks exactly once, matching/chat/completions. Verified live foragenerate_content(gemini native route) and/v1/messages; the same correction applies to any other async@cliententrypoint without a flag. As in #33481, a custom integration implementing only the sync hooks stops receiving events for those async entrypoints; that profile was already unsupported on every flagged async routeSync entrypoints are unchanged: the sync wrapper stamps false, which matches what the flag heuristic already said for them, and sync
generate_contentwas probe-verified to keep firing its sync hook exactly once. Logging objects created outside@clientkeep the flag-based classification byte-for-byte (the bit stays unset). Spend rows, response bodies, and cost headers are unchangedQA runbook
success_callback: ["datadog"],cache: true, and envDD_API_KEY=<any>,DD_SITE=datadoghq.com,DD_BASE_URL=<local sink URL>/v1/messages, one/v1beta/models/<gemini-model>:generateContent, one/chat/completions, and one streaming/v1/messagesrequestlitellm_call_idin the eventmessagepayload)litellm.google_genai.generate_content(sync SDK) with a CustomLogger registered and confirmlog_success_eventstill fires onceFinal Attestation