Repository navigation
perf(caching): memoize _get_all_llm_api_params, rebuilt per request - #31430
Conversation
ModelParamHelper._get_all_llm_api_params() introspects six sets of supported kwargs from static OpenAI type annotations and fixed sets and unions them. The result is constant for the process lifetime, but it was recomputed on every request through both Cache.get_cache_key (caching path) and _get_relevant_args_to_use_for_logging -> get_standard_logging_model_parameters (spend-logging / callback path). Memoize it with lru_cache(maxsize=1); the function takes no arguments, its result is process-static, and both callers treat it as read-only. ~4.7 us/call to ~0.02 us/call.
|
|
Greptile SummaryThis PR memoizes
Confidence Score: 4/5Safe to merge; the change is a narrow, well-tested memoization of a process-static function with no side effects. The only concern is that the cached function still advertises a mutable Set[str] return type, meaning a future caller could accidentally mutate the shared cached object. No current caller does this, so there is no present breakage. The decorator order (@staticmethod above @lru_cache) is correct, and the tests cover both correctness and memoization. No files require special attention, though model_param_helper.py would benefit from changing the return type to FrozenSet[str] to prevent accidental mutation of the shared cached set by future callers.
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/model_param_helper.py | Adds lru_cache(maxsize=1) to _get_all_llm_api_params; result type is still a mutable set, which is a latent mutation hazard for future callers sharing the same cached object. |
| tests/test_litellm/litellm_core_utils/test_model_param_helper.py | New test file verifying correctness (cached == uncached) and memoization (same object identity); uses wrapped correctly to bypass the cache. |
Comments Outside Diff (1)
-
litellm/litellm_core_utils/model_param_helper.py, line 57-94 (link)Mutable return type shared across callers via lru_cache
_get_all_llm_api_paramsis now cached withlru_cachebut still returns a plain (mutable)Set[str]. Every caller gets back the exact samesetobject. The current callers are safe —_get_relevant_args_to_use_for_loggingcalls.difference()(which creates a new set), andCache.get_cache_keyonly does membership tests — but any future caller that calls.add(),.discard(), or|=on the return value would silently corrupt the cache for all subsequent requests in that process. Returning afrozensetwould make the contract explicit and enforce immutability at the type level.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Reviews (1): Last reviewed commit: "perf(caching): memoize _get_all_llm_api_..." | Re-trigger Greptile
Greptile SummaryMemoizes
Confidence Score: 4/5Safe to merge; the change is minimal and well-tested, with one minor concern about the mutable return type of the now-shared cached object. The memoization is correct: Only
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/model_param_helper.py | Adds @lru_cache(maxsize=1) to _get_all_llm_api_params; result is correct and the decorator order is valid for Python 3.8+, but the return type remains mutable Set[str] despite the cached object being shared across all callers. |
| tests/test_litellm/litellm_core_utils/test_model_param_helper.py | New test file with two well-targeted tests: one asserts value equality against __wrapped__ (uncached) computation, the other asserts object identity across calls to verify memoization. |
Reviews (2): Last reviewed commit: "perf(caching): memoize _get_all_llm_api_..." | Re-trigger Greptile
| @staticmethod | ||
| @lru_cache(maxsize=1) | ||
| def _get_all_llm_api_params() -> Set[str]: |
There was a problem hiding this comment.
The
lru_cache now returns the same mutable set object on every call. Any caller that mutates the result (e.g. .add(), .update(), .discard()) would silently corrupt the process-lifetime cache, causing every subsequent caller to receive a wrong set. The current callers are verified read-only, but the Set[str] return type annotation does not communicate this contract to future callers. Returning a frozenset makes mutation a TypeError and documents the read-only intent in the type signature.
| @staticmethod | |
| @lru_cache(maxsize=1) | |
| def _get_all_llm_api_params() -> Set[str]: | |
| @staticmethod | |
| @lru_cache(maxsize=1) | |
| def _get_all_llm_api_params() -> frozenset: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Relevant issues
Linear ticket
LIT-4050 (found while investigating LIT-3910)
Context
Pre-existing per-request inefficiency, not a regression in any particular release.
ModelParamHelper._get_all_llm_api_params()introspects six sets of supported kwargs (chat, text, embedding, transcription, rerank, responses) from static OpenAI type annotations plus fixed sets, unions them, and subtracts an exclude set. The result is constant for the process lifetime, yet it was recomputed on every request through two near-universal paths:Cache.get_cache_key()runs on every request when caching is enabled_get_relevant_args_to_use_for_logging()->get_standard_logging_model_parameters()runs on every request that builds a StandardLoggingPayload (spend logging and most callbacks)Fix
Memoize with
lru_cache(maxsize=1). The function takes no arguments and its result is process-static, and both callers treat the result as read-only (membership tests and.difference(), no mutation), so sharing one cached instance is safe.Pre-Submission checklist
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewScreenshots / Proof of Fix
Microbenchmark, cached vs the uncached underlying function (
__wrapped__):About 213x faster. Across the two per-request call sites that is roughly 9 us/req saved on every cached/logged request. Correctness verified in the same run: the cached result equals a fresh uncached computation, and repeated calls return the same object.
Type
🚄 Infrastructure
Changes
litellm/litellm_core_utils/model_param_helper.py: addlru_cache(maxsize=1)to_get_all_llm_api_paramstests/test_litellm/litellm_core_utils/test_model_param_helper.py: new mapped test file asserting the cached result equals the uncached computation and that the result is memoized (the memoization test fails if the cache is removed)