Skip to content

test: isolate model cost maps in runtime registration tests - #47559

Merged
mateo-berri merged 4 commits into
BerriAI:mainfrom
CNSeniorious000:fix-test-runtime-registration-isolation
Oct 20, 2026
Merged

mateo-berri merged 4 commits into
BerriAI:mainfrom
CNSeniorious000:fix-test-runtime-registration-isolation

Conversation

@CNSeniorious000

@CNSeniorious000 CNSeniorious000 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Runtime registration tests leak model metadata into later tests

How it solves it:

  • Give both tests a fresh cost map through local_model_cost_map
  • Restore the cost map and provider model lists after each unit test
  • Add a canary pair that fails if that restore goes away

The tests save litellm.model_cost by reference, mutate it through register_model, then restore the already-mutated dictionary, so disabled GPT-4o function calling or custom prices leak into later tests on the same xdist worker. The shared conftest had the same by-reference restore, and nothing restored litellm.models_by_provider, which register_model also writes

A new autouse fixture in the tests/unit conftest snapshots litellm.model_cost, litellm.models_by_provider and every litellm.*_models set before each test and puts their contents back in place after it, so references other modules hold stay valid. A canary pair in test_conftest_isolation.py registers a model and overrides a price in one test, then checks the next test sees neither. Production code is unchanged

User Flow

Before: a contributor whose PR touches unrelated code sees the required unit passed check go red on a test they never touched

  1. They open a PR against main and CI runs the root unit shard, shown at https://github.com/BerriAI/litellm/pull//checks
  2. Pytest happens to run the runtime registration override test before the function calling consistency test on the same worker
  3. The unit passed check fails with AssertionError: Direct model gpt-4o should return True, even after pytest's reruns
  4. The PR stays blocked until someone reruns the whole job and gets a luckier test order

After: the same PR gets a green unit passed check whatever order the tests run in

  1. They open a PR against main and CI runs the root unit shard, shown at https://github.com/BerriAI/litellm/pull//checks
  2. Pytest happens to run the runtime registration override test before the function calling consistency test on the same worker
  3. The function calling consistency test passes, since gpt-4o still reports function calling support and its catalog prices
  4. The unit passed check goes green with no rerun

Relevant issues

Observed in #38908 (failed root job)

#47135 includes a fix for the override test alongside several unrelated flakes. This PR isolates the model-cost fix and also covers the adjacent request-scoped registration test

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

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

The client is pytest, run the way the required root unit shard runs it: LITELLM_LOCAL_MODEL_COST_MAP=True, under xdist, with CI's ci dependency group installed. Both trees were bootstrapped with make bootstrap. Before ran at the merge base and After at this PR's tip, with the same commands

Case Before (dd20bb0) After (43fd294)
CI worker replay gpt-4o case fails all pass
Loadscope order, one worker gpt-4o case fails all pass
Minimal pair gpt-4o case fails both pass
Provider model list valid models case fails both pass
Catalog restore canary rollback check fails both pass

Before (dd20bb0)

CI worker replay

  1. Replay, in one xdist worker and in order, the 747 tests worker gw3 ran in the failing root job up to the failing case (test ids)
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest @root_job_gw3_replay_args.txt -n 1 --dist load
E       AssertionError: Direct model gpt-4o should return True
E       assert False == True
FAILED tests/unit/test_utils.py::TestProxyFunctionCalling::test_proxy_function_calling_support_consistency[gpt-4o-litellm_proxy/gpt-4o-True]
1 failed, 745 passed, 1 skipped, 41 warnings in 42.29s
  1. This is the same failure the CI job showed after its two reruns

Loadscope order, one worker

Steps and output
  1. Run the whole file with CI's scheduler on one worker, so the module's plain test functions run before the TestProxyFunctionCalling class, as they did on gw3
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q tests/unit/test_utils.py -n 1 --dist loadscope
E       AssertionError: Direct model gpt-4o should return True
FAILED tests/unit/test_utils.py::TestProxyFunctionCalling::test_proxy_function_calling_support_consistency[gpt-4o-litellm_proxy/gpt-4o-True]
1 failed, 697 passed, 1 skipped, 40 warnings in 42.36s

Minimal pair

Steps and output
  1. Run the override test, then the gpt-4o case, in one process
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q \
  'tests/unit/test_utils.py::test_reapply_runtime_registrations_replays_register_model_overrides[None-0.0]' \
  'tests/unit/test_utils.py::TestProxyFunctionCalling::test_proxy_function_calling_support_consistency[gpt-4o-litellm_proxy/gpt-4o-True]'
E       AssertionError: Direct model gpt-4o should return True
E       assert False == True
FAILED tests/unit/test_utils.py::TestProxyFunctionCalling::test_proxy_function_calling_support_consistency[gpt-4o-litellm_proxy/gpt-4o-True]
1 failed, 1 passed, 10 warnings in 1.96s

Provider model list

Steps and output
  1. Run the request-scoped registration test, then the valid models test, in one process
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q \
  'tests/unit/test_utils.py::test_reapply_runtime_registrations_drops_request_scoped_registrations' \
  tests/unit/test_utils.py::test_aget_valid_models
E             Extra items in the left set:
E             'openai/gpt-4o'
E             'openai/gpt-4o-mini'
FAILED tests/unit/test_utils.py::test_aget_valid_models - AssertionError: ass...
1 failed, 1 passed, 10 warnings in 1.74s

Catalog restore canary

Steps and output
  1. Copy this PR's tests/unit/test_conftest_isolation.py into the merge base tree, leaving the conftest as it is there

  2. Run the canary pair

$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q --tb=line \
  tests/unit/test_conftest_isolation.py -k catalog_writes
E   AssertionError: assert 'conftest-isolation-canary-model' not in {'1024-x-1024/50-steps/bedrock/amazon.nova-canvas-v1:0': ...
FAILED tests/unit/test_conftest_isolation.py::test_register_model_catalog_writes_were_rolled_back
1 failed, 1 passed, 11 deselected, 3 warnings in 0.49s

After (43fd294)

CI worker replay

  1. Run the same replay
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest @root_job_gw3_replay_args.txt -n 1 --dist load
746 passed, 1 skipped, 41 warnings in 44.07s
  1. The gpt-4o case passes and nothing else changes

Loadscope order, one worker

Steps and output
  1. Run the same command
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q tests/unit/test_utils.py -n 1 --dist loadscope
698 passed, 1 skipped, 41 warnings in 41.69s

Minimal pair

Steps and output
  1. Run the same command
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q \
  'tests/unit/test_utils.py::test_reapply_runtime_registrations_replays_register_model_overrides[None-0.0]' \
  'tests/unit/test_utils.py::TestProxyFunctionCalling::test_proxy_function_calling_support_consistency[gpt-4o-litellm_proxy/gpt-4o-True]'
2 passed, 10 warnings in 1.72s

Provider model list

Steps and output
  1. Run the same command
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q \
  'tests/unit/test_utils.py::test_reapply_runtime_registrations_drops_request_scoped_registrations' \
  tests/unit/test_utils.py::test_aget_valid_models
2 passed, 10 warnings in 1.77s

Catalog restore canary

Steps and output
  1. Run the same canary pair on this PR's tree
$ LITELLM_LOCAL_MODEL_COST_MAP=True .venv/bin/python -m pytest -p no:cacheprovider -q --tb=line \
  tests/unit/test_conftest_isolation.py -k catalog_writes
2 passed, 11 deselected, 3 warnings in 0.66s

Notes from the run

  • Without xdist the replay passes, since a fixture reloads litellm
  • Plain make bootstrap lacks CI's ci group, so it was added
  • This PR causes neither and leaves both alone

Type

✅ Test

Caveats (if any)

Low

  • No live-proxy proof, since only test files change
  • The canary pair needs both tests on one worker
    • CI's root shard keeps them together, but a local --dist load run can split them
  • The snapshot is shallow, so an in-place edit inside one entry still leaks
    • No such edit in tests/unit leaks today
  • Writes at class, module or import scope still leak, as before this PR
  • local_model_cost_map on both reapply tests is now redundant
  • test: fix flaky team delete, refusal sink, loop-blocking, Moyai tamper and model-cost leak tests #47135 changes the same override test, so whichever PR merges second drops the duplicate hunk

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
Bugbot summary

[!NOTE]
Low Risk
Test harness and fixture changes only; no production code paths are modified.

Overview
Stops register_model and related catalog edits from leaking into later tests on the same pytest-xdist worker, which had been causing order-dependent failures (e.g. gpt-4o function-calling and valid-models assertions).

Adds an autouse isolate_model_catalog fixture in tests/unit/conftest.py that snapshots litellm.model_cost, litellm.models_by_provider, and every litellm.*_models set, then restores their contents in place after each test (plus invalidates the model-cost lowercase map and clears get_model_info cache). Canary tests in test_conftest_isolation.py assert catalog writes are scoped to one test and rolled back in the next.

test_utils.py drops duplicate isolated_openai_model_sets markers and wires local_model_cost_map into the two runtime reapply registration tests so they run against a fresh local cost map.

Reviewed by Cursor Bugbot for commit 43fd294. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T13:18:08.879530Z a32530b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@litereview

litereview Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

✅ code-review

5/5 · Ready for human review

Scope: full · Full PR review.

No correctness findings. The PR changes only test isolation in tests/unit/conftest.py, tests/unit/test_conftest_isolation.py and tests/unit/test_utils.py. The new autouse fixture restores the contents of litellm.model_cost, litellm.models_by_provider and the litellm.*_models sets in place. When I removed autouse from that fixture, the new rollback canary failed, so the canary catches the leak it was written for.

Checks and execution details
  • Passed · Real testing evidence (required): Not applicable: no LLM translation changes in the scoped paths. The PR touches only tests/unit/conftest.py, tests/unit/test_conftest_isolation.py and tests/unit/test_utils.py. Nothing changed under litellm/llms/** or litellm/litellm_core_utils/prompt_templates/**.
  • Passed · Compatibility requirements (required): The PR changes test code only. It leaves supported APIs, configuration, defaults, persisted data and public behavior unchanged, so the title needs no breaking-change marker.
  • Passed · Changed behavior and result consistency (required): Standards: read tests/AGENTS.md and tests/unit/AGENTS.md. Neither conflicts with the change.

Traced the path from register_model through _replace_cost_map_entry to add_known_models. _replace_cost_map_entry writes new AliasedCostEntry objects into litellm.model_cost. add_known_models changes the provider sets and calls models_by_provider.update. A shallow content snapshot that is restored in place therefore undoes these writes.

Teardown order also works. isolate_model_catalog tears down before isolate_litellm_globals, which restores the model_cost reference afterwards. For tests that use local_model_cost_map, monkeypatch swaps the reference and the original dict keeps its contents.

Defect-sensitive oracle: with autouse removed from isolate_model_catalog, test_register_model_catalog_writes_were_rolled_back failed. With autouse restored, both canary tests passed.

All 712 tests in test_conftest_isolation.py and test_utils.py passed under xdist with -n 4. The reapply_runtime and function_calling tests passed in file order on one worker (24 passed).

  • Passed · Unit suites for changed test files (optional): .venv/bin/python -m pytest tests/unit/test_conftest_isolation.py tests/unit/test_utils.py -q -p no:randomly -x -n 4 reported 712 passed.

View review · Reviewed 43fd29474209

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge with no new actionable findings

Summary

The PR restores model catalogs between unit tests and adds checks that registration writes do not leak into later tests

  • Unit tests restore shared model catalogs after each test.
  • The URL-fetch test keeps one model-set fixture marker.

Reviews (6) · Last reviewed commit: "test: snapshot litellm globals before sc..." · Reviewed by Greptile

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

Comment thread tests/unit/conftest.py Resolved
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

1 similar comment
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 43fd294. Configure here.

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thanks for the contribution!

@mateo-berri
mateo-berri merged commit 77e0873 into BerriAI:main Oct 20, 2026
89 checks passed
@CNSeniorious000
CNSeniorious000 deleted the fix-test-runtime-registration-isolation branch October 20, 2026 18:49
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.

2 participants