Skip to content

refactor: enhance dynamic model fetching with improved concurrency and error handling - #7829

Merged
wxg0103 merged 1 commit into
v3from
pr@v3@refactor_model
Nov 11, 2026
Merged

wxg0103 merged 1 commit into
v3from
pr@v3@refactor_model

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

refactor: enhance dynamic model fetching with improved concurrency and error handling

return result.success(self._filter_model_list(fallback_list, search))

class ModelListCache(APIView):
authentication_classes = [TokenAuth]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Main findings:

  1. 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_base and passes None for one key component. If the cache is keyed by url + key as 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:
      get_cached_base_model_list(provider, model_type, api_key, api_base, credential)
      Adjust to the actual function signature, but the key fields must match.
  2. 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.
  3. _get_model_list is inconsistent about allow_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,
      )
  4. 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 None or a sentinel/structured result to distinguish failure from an empty result.
  5. Search normalization is slightly odd

    • _filter_model_list checks if not search before 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.
  6. Caching with search is now correct only because filtering was separated

    • The new code caches live_list before applying search, 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.
  7. Minor / operational

    • Ensure get_cached_base_model_list is imported/defined; it is newly used here.
    • The walrus operator := requires Python 3.8+.
    • If cache_base_model_list can fail, consider whether a cache-write failure should fail the whole request or just be logged.
    • Update all tests/callers of _get_model_list, since search was removed from its signature.

Overall, the main blocking issue is the cache read/write key mismatch. The rest are robustness and clarity improvements.

@wxg0103
wxg0103 merged commit 2117e9d into v3 Nov 11, 2026
3 of 4 checks passed
@wxg0103
wxg0103 deleted the pr@v3@refactor_model branch November 11, 2026 06:37
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