Repository navigation
refactor: enhance dynamic model fetching with improved concurrency and error handling - #7829
Merged
Merged
Conversation
shaohuzhang1
commented
Nov 11, 2026
| return result.success(self._filter_model_list(fallback_list, search)) | ||
|
|
||
| class ModelListCache(APIView): | ||
| authentication_classes = [TokenAuth] |
Contributor
Author
There was a problem hiding this comment.
Main findings:
-
High priority: cache read key does not match cache write key
- Write:
cache_base_model_list(provider, model_type, api_key, api_base, credential, live_list)
- Read:
get_cached_base_model_list(provider, model_type, None, credential)
- The read omits
api_key/api_baseand passesNonefor one key component. If the cache is keyed byurl + keyas the docstring says, this will at best cause cache misses, and at worst return a model list cached for a different API key/base. - Fix by using exactly the same cache identity as the write path, for example:
Adjust to the actual function signature, but the key fields must match.
get_cached_base_model_list(provider, model_type, api_key, api_base, credential)
- Write:
-
Fallback does not handle exceptions
- The intended order is “live → cache → built-in fallback”, but if the live call raises an exception, the cache/built-in paths are never reached.
- If fallback should happen on network/provider failures, wrap the live call in
try/except. Decide explicitly whether auth/validation errors should propagate instead of falling back.
-
_get_model_listis inconsistent aboutallow_builtin_fallback- When
allow_builtin_fallback=False, the flag is passed to the provider handler. - When
True, it is not passed and relies on the handler default. - Safer:
return provider_handler.get_model_list( model_type, api_key=api_key, api_base=api_base, credential=credential, allow_builtin_fallback=allow_builtin_fallback, )
- When
-
if live_list:conflates “empty valid list” with “failure”- If a provider legitimately returns
[], the code will fall back to cache/built-in models, which may be incorrect. - If empty is a valid response, use
live_list is not Noneor a sentinel/structured result to distinguish failure from an empty result.
- If a provider legitimately returns
-
Search normalization is slightly odd
_filter_model_listchecksif not searchbefore stripping. A whitespace-only search therefore filters to an empty string and returns everything.- Cleaner:
search = str(search or "").strip().lower() if not search: return model_list
- Also, this assumes every model entry is a dict; that was true before, but worth keeping consistent.
-
Caching with search is now correct only because filtering was separated
- The new code caches
live_listbefore applyingsearch, which is good, provided the provider handler returns the full unfiltered list. This part is an improvement over the original “do not cache when search is present” logic.
- The new code caches
-
Minor / operational
- Ensure
get_cached_base_model_listis imported/defined; it is newly used here. - The walrus operator
:=requires Python 3.8+. - If
cache_base_model_listcan fail, consider whether a cache-write failure should fail the whole request or just be logged. - Update all tests/callers of
_get_model_list, sincesearchwas removed from its signature.
- Ensure
Overall, the main blocking issue is the cache read/write key mismatch. The rest are robustness and clarity improvements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
refactor: enhance dynamic model fetching with improved concurrency and error handling