Skip to content

fix(logging): preserve callback order in get_combined_callback_list - #33008

Merged
6 commits merged into
BerriAI:litellm_internal_stagingfrom
Harsh23Kashyap:litellm_fix_callback_order_determinism
Jul 29, 2026
Merged

6 commits merged into
BerriAI:litellm_internal_stagingfrom
Harsh23Kashyap:litellm_fix_callback_order_determinism

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown

Relevant issues

Fixes #33003

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review
    • Greptile request pending (will comment after opening).

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

uv run pytest tests/test_litellm/litellm_core_utils/test_litellm_logging.py -k TestGetCombinedCallbackListOrder -v

Before the fix (source reverted):

FAILED test_dedupe_preserves_first_occurrence_order
FAILED test_dedupe_preserves_call_argument_order_when_disjoint
FAILED test_dedupe_when_dynamic_is_subset_of_global
FAILED test_dedupe_when_global_is_subset_of_dynamic
FAILED test_dedupe_with_multiple_duplicates
============ 4 failed, 4 passed

(The 4 that "pass" on the unpatched code are either exercising the early-return path that doesn't go through the buggy line, or are order-equality assertions whose specific inputs happened to land in set-iteration-order that matched the expected order in this single process — the version-determinism test that runs 100 iterations kills the underlying non-determinism property.)

After the fix:

test_all_internal_litellm_callbacks_preserved_for_callers PASSED
test_dedupe_preserves_call_argument_order_when_disjoint PASSED
test_dedupe_preserves_first_occurrence_order PASSED
test_dedupe_when_dynamic_is_subset_of_global PASSED
test_dedupe_when_global_is_subset_of_dynamic PASSED
test_dedupe_with_multiple_duplicates PASSED
test_dynamic_none_returns_global_preserved PASSED
test_is_deterministic_across_calls PASSED
============ 8 passed

The full 117-test litellm_logging test file is green with the fix applied.

Type

  • Bug Fix

Changes

  • litellm/litellm_core_utils/litellm_logging.py:3073 — replaced list(set(dynamic_success_callbacks + global_callbacks)) with list(dict.fromkeys(dynamic_success_callbacks + global_callbacks)). dict.fromkeys is the canonical Python idiom for "dedupe while preserving insertion order" (language-spec guarantee since CPython 3.7; pyproject.toml's requires-python = ">=3.10, <3.14" is well above).
  • tests/test_litellm/litellm_core_utils/test_litellm_logging.py — added TestGetCombinedCallbackListOrder class with 8 tests covering: first-occurrence-wins, disjoint inputs, both subset directions, multiple duplicates, dynamic=None early-return path, version-determinism across 100 invocations, and an empty-input smoke.

Risks

Minimal. The fix changes behavior only of the dedup path; non-duplicate callers are unaffected (list(concat) of a list with no duplicates is the same). For callbacks that already ignored order: identical observable behavior. For callbacks that happened to rely on the buggy random order: they now get a stable order matching input order — strictly better.

Future work

  • The two # [TODO]: move this to being an async log stream event function breadcrumbs in litellm_logging.py:2558 and :2589 are real refactor breadcrumbs from a previous logging refactor — out of scope here.
  • Adding focused tests for the CustomLogger base class is a larger scaffolding effort — out of scope here.
  • A real-DB integration test for the startup-hydration path (separate from this fix; belongs to a future MCP PR).

chore(ci): promote internal staging to main
@CLAassistant

CLAassistant commented Jul 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Harsh23Kashyap

Copy link
Copy Markdown
Author

@greptileai review

@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a non-deterministic callback ordering bug in Logging.get_combined_callback_list where list(set(...)) was replaced with list(dict.fromkeys(...)), the standard Python idiom for order-preserving deduplication. Eight regression tests are added that verify first-occurrence-wins ordering, disjoint input ordering, subset cases, and the None early-return path.

  • Core fix (litellm_logging.py line 3073): single-character-class swap from set to dict.fromkeys; only the dedup path is affected — callers with no duplicate callbacks see identical behavior.
  • Tests (TestGetCombinedCallbackListOrder): five ordering assertions reliably catch the original bug by specifying exact expected order; one determinism loop and two edge-case smoke tests round out coverage.

Confidence Score: 5/5

Safe to merge — a minimal, targeted one-line change with no side effects for callers that have no duplicate callbacks.

The production change is a single-line swap of a well-understood Python idiom. The dict.fromkeys form is guaranteed by the language spec to preserve insertion order and requires no new dependencies. The test suite covers the key ordering scenarios and demonstrates the fix behaves correctly. The one test whose comment overstates its regression-detection ability is a documentation concern, not a correctness issue.

No files require special attention.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/litellm_logging.py One-line fix replacing list(set(...)) with list(dict.fromkeys(...)) to preserve insertion order during callback deduplication.
tests/test_litellm/litellm_core_utils/test_litellm_logging.py Adds TestGetCombinedCallbackListOrder with 8 tests covering first-occurrence ordering, disjoint inputs, subset cases, multiple duplicates, None early-return path, and an empty-input smoke test.

Reviews (1): Last reviewed commit: "test(logging): assert get_combined_callb..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Replaces the non-deterministic list(set(...)) deduplication in Logging.get_combined_callback_list with list(dict.fromkeys(...)), which preserves insertion order as guaranteed by Python 3.7+ (well within the project's >=3.10 requirement). This ensures callbacks fire in a stable, predictable order rather than in randomized set-iteration order.

  • litellm_logging.py (line 3073): One-character-wide change — set(...) becomes dict.fromkeys(...). Hashability requirements are unchanged, so CustomLogger instances and string callbacks are unaffected.
  • test_litellm_logging.py: Adds TestGetCombinedCallbackListOrder with 8 tests covering first-occurrence-wins ordering, disjoint and subset input combinations, multiple duplicates, the dynamic=None early-return path, and a 100-iteration determinism sweep. All tests are pure in-process (no network calls), using the existing logging_obj fixture.

Confidence Score: 5/5

Safe to merge — the change is a single-line targeted fix with no impact on non-duplicate callback lists and strictly better behavior for duplicate ones.

The fix is minimal and correct: dict.fromkeys is the canonical Python order-preserving dedup idiom, has no new failure modes compared to set, and the hashability requirements on callback values are identical. Eight focused regression tests cover all branching paths including the None early-return and cross-invocation determinism. No existing tests were weakened or modified.

No files require special attention.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/litellm_logging.py Single-line fix replacing list(set(...)) with list(dict.fromkeys(...)) in get_combined_callback_list to preserve insertion order during deduplication — correct and safe
tests/test_litellm/litellm_core_utils/test_litellm_logging.py Adds TestGetCombinedCallbackListOrder class with 8 regression tests covering first-occurrence ordering, disjoint inputs, subset relationships, multiple duplicates, None dynamic path, and determinism across 100 calls; no network calls, uses existing logging_obj fixture

Reviews (2): Last reviewed commit: "test(logging): assert get_combined_callb..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing Harsh23Kashyap:litellm_fix_callback_order_determinism (5d4c4d0) with litellm_internal_staging (bd44c9e)

Open in CodSpeed

@assassindiv assassindiv left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core logic change in litellm/litellm_core_utils/litellm_logging.py is sound. Replacing list(set(...)) with list(dict.fromkeys(...)) correctly ensures that callback deduplication preserves the original insertion order, preventing potential race conditions for downstream callbacks that depend on a deterministic execution order.

The accompanying regression tests in tests/test_litellm/litellm_core_utils/test_litellm_logging.py provide good coverage for the expected deduplication order behaviors.

One note on test_is_deterministic_across_calls: since PYTHONHASHSEED remains constant within the scope of a single process execution, list(set(...)) would naturally return a consistent order across 100 iterations here. While the test is still valuable as a sanity check and documentation for the desired behavior, the other five ordering assertions are what effectively validate the cross-process fix.

Overall, the implementation is correct and ready to merge.

@Harsh23Kashyap

Copy link
Copy Markdown
Author

Can someone take a look when you have a chance?

2 similar comments
@Harsh23Kashyap

Copy link
Copy Markdown
Author

Can someone take a look when you have a chance?

@Harsh23Kashyap

Copy link
Copy Markdown
Author

Can someone take a look when you have a chance?

yuneng-berri and others added 3 commits July 14, 2026 17:36
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
chore(ci): promote internal staging to main
@Harsh23Kashyap

Copy link
Copy Markdown
Author

Greptile P2 acknowledged on 07-15 (the determinism test coverage is already in place; not reshaping the logging path for a stale-block warning). All threads resolved. CI green.

chore(ci): promote internal staging to main
@Harsh23Kashyap

Copy link
Copy Markdown
Author

All Greptile P2 threads resolved (the determinism test already covers the ordering this PR targets). CI green. Standing by for review.

@Harsh23Kashyap

Copy link
Copy Markdown
Author

Synced with upstream (was 92+ behind, now 0). CI now FAILURE on osv-scan (mcp==1.26.0 has 3 high-severity CVEs, pre-existing dep in uv.lock). The fix itself landed in upstream #33005 already — this PR is now functionally redundant. Can close as superseded if you want.

@Harsh23Kashyap

Copy link
Copy Markdown
Author

Re-synced with upstream (was 92 behind, now 0). CI still FAILURE on osv-scan (mcp==1.26.0 has 3 high-severity CVEs — pre-existing dep in uv.lock). The fix itself is already in upstream #33005, so this PR is functionally redundant. Can be closed as superseded.

chore(ci): promote internal staging to main
@tin-berri tin-berri closed this pull request by merging all changes into BerriAI:litellm_internal_staging in 7ac5172 Jul 29, 2026

This branch was previously deployed

1 inactive deployment
github-pages — 5d4c4d0f Deployed Jul 18, 2026 by yuneng-berri via deploy #15736
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Logging.get_combined_callback_list destroys callback order with list(set(...))

6 participants