BerriAI/litellm · error · TypeError

'models' must be a string or list of strings

Error message

'models' must be a string or list of strings

What it means

After batch_completion_models_all_responses() extracts the models kwarg, it normalizes it: a single string becomes a one-element list, and list/tuple are accepted as-is. Any other type (dict, int, None, a generator) raises TypeError with this message before the ThreadPoolExecutor is created. Note that despite the message, tuples are also accepted; contents are not validated per-element.

Solutions

  1. Pass a plain list of model name strings: models=["gpt-4o-mini", "claude-3-5-haiku"]
  2. Default None to a real list at the call site: models=models or []
  3. Materialize generators/sets with list(...) before passing
  4. Add a type guard on dynamic model lists before calling

Example fix

# before
responses = batch_completion_models_all_responses(
    models=(m for m in config["models"]),  # generator -> TypeError
    messages=[{"role": "user", "content": "hello"}],
)

# after
responses = batch_completion_models_all_responses(
    models=list(config["models"]),
    messages=[{"role": "user", "content": "hello"}],
)
Defensive patterns

Strategy: type-guard

Validate before calling

models = kwargs.setdefault("models", [])
if isinstance(models, str):
    kwargs["models"] = [models]
elif not isinstance(models, (list, tuple)):
    raise TypeError("models must be str, list[str], or tuple[str, ...]")

Type guard

def is_models_arg(value: object) -> bool:
    return isinstance(value, (str, list, tuple))

Try / catch

try:
    responses = batch_completion_models_all_responses(**kwargs)
except TypeError as e:
    if "must be a string or list" in str(e):
        kwargs["models"] = list(kwargs["models"])
        responses = batch_completion_models_all_responses(**kwargs)
    else:
        raise

Prevention

When it happens

Trigger: Passing models=None, a dict, or an int; passing a generator/iterator expression (not list/tuple); passing a set of model names; refactored code where models used to be a list but became an object.

Common situations: Optional model lists defaulting to None and passed through unconditionally; JSON configs where models parses to something unexpected; set literals used for deduplication.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/91a88058b6fe1abb. Report an issue: GitHub.

Appendix: source

Thrown at litellm/batch_completion/main.py:237

        It sends requests concurrently and collects responses from all models that respond.
    """
    import concurrent.futures

    # ANSI escape codes for colored output

    if "model" in kwargs:
        kwargs.pop("model")
    if "models" in kwargs:
        models = kwargs.pop("models")
    else:
        raise Exception("'models' param not in kwargs")

    if isinstance(models, str):
        models = [models]
    elif isinstance(models, (list, tuple)):
        models = list(models)
    else:
        raise TypeError("'models' must be a string or list of strings")

    if len(models) == 0:
        return []

    responses: Final = []

    with concurrent.futures.ThreadPoolExecutor(max_workers=len(models)) as executor:
        futures: Final = [executor.submit(litellm.completion, *args, model=model, **kwargs) for model in models]

        for future in futures:
            try:
                result = future.result()
                if result is not None:
                    responses.append(result)
            except Exception as e:
                print_verbose(f"batch_completion_models_all_responses: model request failed: {e}")
                continue

View on GitHub (pinned to 77b7c6c40c)