{"record":{"id":"91a88058b6fe1abb","repo":"BerriAI/litellm","slug":"models-must-be-a-string-or-list-of-strings","errorCode":null,"errorMessage":"'models' must be a string or list of strings","messagePattern":"'models' must be a string or list of strings","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"litellm/batch_completion/main.py","lineNumber":237,"sourceCode":"        It sends requests concurrently and collects responses from all models that respond.\n    \"\"\"\n    import concurrent.futures\n\n    # ANSI escape codes for colored output\n\n    if \"model\" in kwargs:\n        kwargs.pop(\"model\")\n    if \"models\" in kwargs:\n        models = kwargs.pop(\"models\")\n    else:\n        raise Exception(\"'models' param not in kwargs\")\n\n    if isinstance(models, str):\n        models = [models]\n    elif isinstance(models, (list, tuple)):\n        models = list(models)\n    else:\n        raise TypeError(\"'models' must be a string or list of strings\")\n\n    if len(models) == 0:\n        return []\n\n    responses: Final = []\n\n    with concurrent.futures.ThreadPoolExecutor(max_workers=len(models)) as executor:\n        futures: Final = [executor.submit(litellm.completion, *args, model=model, **kwargs) for model in models]\n\n        for future in futures:\n            try:\n                result = future.result()\n                if result is not None:\n                    responses.append(result)\n            except Exception as e:\n                print_verbose(f\"batch_completion_models_all_responses: model request failed: {e}\")\n                continue\n","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/batch_completion/main.py#L219-L255","documentation":"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.","triggerScenarios":"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.","commonSituations":"Optional model lists defaulting to None and passed through unconditionally; JSON configs where models parses to something unexpected; set literals used for deduplication.","solutions":["Pass a plain list of model name strings: models=[\"gpt-4o-mini\", \"claude-3-5-haiku\"]","Default None to a real list at the call site: models=models or []","Materialize generators/sets with list(...) before passing","Add a type guard on dynamic model lists before calling"],"exampleFix":"# before\nresponses = batch_completion_models_all_responses(\n    models=(m for m in config[\"models\"]),  # generator -> TypeError\n    messages=[{\"role\": \"user\", \"content\": \"hello\"}],\n)\n\n# after\nresponses = batch_completion_models_all_responses(\n    models=list(config[\"models\"]),\n    messages=[{\"role\": \"user\", \"content\": \"hello\"}],\n)\n","handlingStrategy":"type-guard","validationCode":"models = kwargs.setdefault(\"models\", [])\nif isinstance(models, str):\n    kwargs[\"models\"] = [models]\nelif not isinstance(models, (list, tuple)):\n    raise TypeError(\"models must be str, list[str], or tuple[str, ...]\")\n","typeGuard":"def is_models_arg(value: object) -> bool:\n    return isinstance(value, (str, list, tuple))\n","tryCatchPattern":"try:\n    responses = batch_completion_models_all_responses(**kwargs)\nexcept TypeError as e:\n    if \"must be a string or list\" in str(e):\n        kwargs[\"models\"] = list(kwargs[\"models\"])\n        responses = batch_completion_models_all_responses(**kwargs)\n    else:\n        raise\n","preventionTips":["Materialize iterables with list(...) before passing them as models","Default optional model lists to [] rather than None at the call site","Add a type guard on config-driven model lists"],"tags":["batch-completion","models-param","type-error","api-misuse"],"backgroundTag":"invalid-argument-value","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}