xtekky/gpt4free · error · ModelNotFoundError

Model {model} not found

Error message

Model {model} not found

What it means

ModelNotFoundError raised in the Cohere Command HF Space provider's model resolution: the requested model string is neither a key in cls.model_aliases nor handled by an earlier branch (the shown code is the terminal else — every accepted name is an alias, possibly mapping to a list from which one variant is randomly chosen). It means the provider's static alias table does not know the model name you passed.

Source

Thrown at g4f/Provider/hf_space/CohereForAI_C4AI_Command.py:65

        # Check if the model exists directly in our models list
        if model in cls.models:
            return model

        # Check if there's an alias for this model
        if model in cls.model_aliases:
            alias = cls.model_aliases[model]
            # If the alias is a list, randomly select one of the options
            if isinstance(alias, list):
                selected_model = random.choice(alias)
                debug.log(
                    f"{cls.__name__}: Selected model '{selected_model}' from alias '{model}'"
                )
                return selected_model
            debug.log(f"{cls.__name__}: Using model '{alias}' for alias '{model}'")
            return alias

        raise ModelNotFoundError(f"Model {model} not found")

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        api_key: str = None,
        proxy: str = None,
        conversation: JsonConversation = None,
        return_conversation: bool = True,
        **kwargs,
    ) -> AsyncResult:
        model = cls.get_model(model)
        headers = {
            "Origin": cls.url,
            "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0",
            "Accept": "*/*",
            "Accept-Language": "en-US,en;q=0.5",

View on GitHub (pinned to 973504e177)

Solutions

  1. List valid models first: use the provider's get_models()/image_models surface or inspect cls.model_aliases keys, and pass one of those exact names.
  2. Check for typos and exact casing in the model name.
  3. If the model genuinely exists on the HF Space but is missing from g4f, add it to model_aliases in CohereForAI_C4AI_Command.py (or open an upstream issue/PR).

Example fix

# before
provider.create_async_generator(model="command-r-plus", messages=msgs)

# after (use a name from cls.model_aliases)
provider.create_async_generator(model=provider.default_model, messages=msgs)
Defensive patterns

Strategy: validation

Validate before calling

if model not in CohereForAI_C4AI_Command.model_aliases:
    model = CohereForAI_C4AI_Command.default_model  # or raise your own error
# or: valid = CohereForAI_C4AI_Command.get_models()

Type guard

def is_supported_model(provider_cls, model: str) -> bool:
    return model in getattr(provider_cls, 'model_aliases', {})

Try / catch

try:
    ...
except ModelNotFoundError:
    model = provider_cls.default_model  # fail over to a known-good alias
    ...

Prevention

When it happens

Trigger: Calling CohereForAI_C4AI_Command.create_async_generator with a model string not present in its model_aliases dict — e.g. a typo, a model name belonging to a different provider, or a newly released Cohere model not yet added to the alias table.

Common situations: Passing 'command-r-plus-08-2024' or 'command-a' before it was added to model_aliases; passing generic names like 'gpt-4' or 'claude-3' to this provider; case/format mismatches with the alias keys.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/0cd272ebc253cd21. Report an issue: GitHub.