Skip to content

fix: enhance model list retrieval to fallback on built-in models when dynamic list is empty - #7929

Merged
wxg0103 merged 1 commit into
v3from
pr@v3@fix_model_error
Nov 16, 2026
Merged

wxg0103 merged 1 commit into
v3from
pr@v3@fix_model_error

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

fix: enhance model list retrieval to fallback on built-in models when dynamic list is empty

@wxg0103
wxg0103 merged commit ab51f17 into v3 Nov 16, 2026
3 of 4 checks passed
@wxg0103
wxg0103 deleted the pr@v3@fix_model_error branch November 16, 2026 04:10
credential = self.provider.get_model_credential("STT", "wand-asr-v1")
self.assertIsInstance(credential, TencentTokenhubSTTModelCredential)
fields = {item.get("field") for item in credential.to_form_list()}
self.assertIn("api_key", fields)

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.

Overall, the added tests are syntactically fine and cover the intended Tencent STT credential paths. Main concerns are test isolation, strictness of assertions, and Django test class choice.

Potential issues / suggestions

  1. SimpleTestCase may be too restrictive

    • SimpleTestCase disallows database access.
    • If TencentModelProvider() or credential.to_form_list() touches the DB, cache, settings requiring DB, or network, these tests may fail or behave inconsistently.
    • If no DB access is involved, SimpleTestCase is appropriate. Otherwise use TestCase or mock external dependencies.
  2. Shared provider instance across tests

    • cls.provider = TencentModelProvider() in setUpClass means all tests share one instance.
    • If the provider caches credentials or has mutable state, tests may become order-dependent.
    • Safer:
      def setUp(self):
          self.provider = TencentModelProvider()
    • If the provider is guaranteed stateless, the current approach is acceptable for speed.
  3. Default-credential test is too permissive

    • This only checks that the credential is not None and that its form list is non-empty:
      self.assertIsNotNone(credential)
      self.assertTrue(credential.to_form_list())
    • This could pass even if the wrong default credential is returned.
    • Better to assert the expected credential type and/or expected fields:
      self.assertIsNotNone(credential)
      self.assertIsInstance(credential, ExpectedDefaultSTTCredential)
      fields = {item["field"] for item in credential.to_form_list()}
      self.assertIn("api_key", fields)
    • Adjust to the actual default credential class.
  4. Clarify empty string vs. missing model name

    • The test name says without_model_name, but it passes "", not None.
    • Confirm the real contract. If both "" and None are valid “no model selected” inputs, test both. If only one is valid, rename the test to match, e.g. test_stt_default_credential_with_empty_model_name.
  5. Local import inside test method

    • This import:
      from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
          TencentTokenhubSTTModelCredential,
      )
      is fine functionally, but normally belongs at module level.
    • Move it to the top unless it is intentionally local to avoid a circular import. If so, add a short comment explaining why.
  6. Clearer failure for assertIsInstance

    • assertIsInstance(credential, ...) will fail clearly if credential is None, but adding:
      self.assertIsNotNone(credential)
      before it can make the failure reason clearer.
  7. Missing negative coverage

    • Consider adding tests for:
      • unsupported model type,
      • unsupported model name,
      • empty model name for non-STT types.
    • This depends on the expected behavior of get_model_credential.
  8. Minor robustness in form-field extraction

    • {item.get("field") for item in ...} may silently include None if malformed data is returned.
    • Using item["field"] makes malformed entries fail explicitly. This is optional.

Suggested refactor sketch

# -*- coding: utf-8 -*-
from django.test import SimpleTestCase

from models_provider.impl.tencent_model_provider.tencent_model_provider import TencentModelProvider
from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
    TencentTokenhubSTTModelCredential,
)


class TencentModelProviderTests(SimpleTestCase):
    def setUp(self):
        self.provider = TencentModelProvider()

    def test_stt_model_type_is_exposed(self):
        model_types = {item.get("value") for item in self.provider.get_model_type_list()}
        self.assertIn("STT", model_types)

    def test_stt_default_credential_with_empty_model_name(self):
        credential = self.provider.get_model_credential("STT", "")
        self.assertIsNotNone(credential)
        # Assert the expected default credential type as well.
        self.assertTrue(credential.to_form_list())

    def test_wand_asr_v1_uses_tokenhub_credential(self):
        credential = self.provider.get_model_credential("STT", "wand-asr-v1")
        self.assertIsNotNone(credential)
        self.assertIsInstance(credential, TencentTokenhubSTTModelCredential)
        fields = {item["field"] for item in credential.to_form_list()}
        self.assertIn("api_key", fields)

No blocking syntax issues were found. The most important checks are whether SimpleTestCase is safe for this provider and whether the default-credential assertion is strong enough.

elif not allow_builtin_fallback:
return []
return self.get_model_info_manage().get_model_list_by_model_type(model_type)

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.

Looking at this diff, here are the observations:

Potential Issues

  1. Behavior change / semantic shift: Previously, when manage is not None, the function always returned the dynamic list (possibly empty) immediately. Now it falls through to the builtin list when dynamic_list is empty and allow_builtin_fallback is True. This changes behavior for existing callers and should be verified against intended contract.

  2. Control flow asymmetry between branches:

    • if manage is not None: branch does NOT return [] when not allow_builtin_fallback — instead it returns dynamic_list (which may be empty), effectively matching. That's fine.
    • elif not allow_builtin_fallback: returns [] only when manage is None. But if manage is None and allow_builtin_fallback is True, it silently falls through to builtin. Confirm that's intended — a failing/absent dynamic manage will now silently use builtins even when the caller asked for "no fallback" indirectly via the credential path. Slight inconsistency: not allow_builtin_fallback semantics differ depending on whether manage was constructed.
  3. Comment language/style: The inline comment is in Chinese while the file/answers convention appears consistent — verify project style guide (some repos require English or i18n comments). Also the comment says "交由视图继续走缓存/内置兜底" which describes caller behavior — this couples the helper to view-layer decisions; consider documenting in docstring instead.

  4. Redundant nested check: if dynamic_list or not allow_builtin_fallback: return dynamic_list followed by falling out of the if manage is not None block is correct, but the readability could be improved with an early return [] explicitly for the fallthrough-non-fallback case to make intent obvious.

Optimization / Readability Suggestions

if api_key or credential:
    manage = self._get_dynamic_manage_cached(api_key, api_base, credential)
    if manage is not None:
        dynamic_list = manage.get_model_list_by_model_type(model_type)
        if dynamic_list or not allow_builtin_fallback:
            return dynamic_list
        # else: fall through to builtin list below
    elif not allow_builtin_fallback:
        return []
return self.get_model_info_manage().get_model_list_by_model_type(model_type)
  • Consider extracting builtin = self.get_model_info_manage().get_model_list_by_model_type(model_type) once for reuse.
  • Add/update docstring to describe allow_builtin_fallback precisely (does it apply when manage is None? when dynamic list is empty?).
  • Add unit tests for the four combinations: manage present/absent × allow_builtin_fallback True/False, plus dynamic_list empty/non-empty.

Minor

  • No null-check on the return value of get_model_list_by_model_type — ensure it's guaranteed to be list-like (or short-circuit depends on truthiness; None would behave as empty, likely fine but worth confirming).
  • The diff header shows credential=None truncated; confirm signature unchanged elsewhere.

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