{"record":{"id":"a46d3547c61ad35d","repo":"oobabooga/textgen","slug":"invalid-branch-name-only-alphanumeric-characters","errorCode":null,"errorMessage":"Invalid branch name. Only alphanumeric characters, period, underscore and dash are allowed.","messagePattern":"Invalid branch name\\. Only alphanumeric characters, period, underscore and dash are allowed\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"download-model.py","lineNumber":74,"sourceCode":"\n        return session\n\n    def sanitize_model_and_branch_names(self, model, branch):\n        model = model.removesuffix(\"/\")\n\n        if model.startswith(base + '/'):\n            model = model[len(base) + 1:]\n\n        model_parts = model.split(\":\")\n        model = model_parts[0] if len(model_parts) > 0 else model\n        branch = model_parts[1] if len(model_parts) > 1 else branch\n\n        if branch is None:\n            branch = \"main\"\n        else:\n            pattern = re.compile(r\"^[a-zA-Z0-9._-]+$\")\n            if not pattern.match(branch):\n                raise ValueError(\n                    \"Invalid branch name. Only alphanumeric characters, period, underscore and dash are allowed.\")\n\n        return model, branch\n\n    def get_download_links_from_huggingface(self, model, branch, text_only=False, specific_file=None, exclude_pattern=None):\n        session = self.session\n        page = f\"/api/models/{model}/tree/{branch}\"\n        cursor = b\"\"\n\n        links = []\n        sha256 = []\n        file_sizes = []\n        classifications = []\n        has_pytorch = False\n        has_pt = False\n        has_gguf = False\n        has_safetensors = False\n        is_lora = False","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/download-model.py#L56-L92","documentation":"Raised by download-model.py when parsing a 'model:branch' string and the branch component fails the regex ^[a-zA-Z0-9._-]+$. The script splits the model identifier on ':' and validates the second part before querying the Hugging Face tree API. It exists to prevent malformed branch names from producing confusing downstream HTTP errors from the HF API.","triggerScenarios":"Calling download-model.py (or DownloadModel.get_download_links_from_huggingface via its model normalization path) with an identifier like 'org/model:main branch' (space), 'org/model:refs/heads/main' (slash), 'model:' followed by unicode, or any branch containing '/', ':', '+', '~' or whitespace. Also triggered by a model name that itself contains extra ':' segments, e.g. 'a:b:model'.","commonSituations":"Passing a full git ref ('refs/heads/main' or 'refs/pr/123') instead of the short branch name; copying a URL fragment that includes slashes or spaces; shell quoting mistakes that append stray characters after the branch.","solutions":["Use a plain short branch name: 'user/model:main', 'user/model:dev', 'user/model:v1.0-rc1'.","If you meant a PR or full ref, strip it to the short name (refs/heads/main -> main).","Quote the whole argument in the shell so spaces/special characters are not appended: python download-model.py \"user/model:my-branch\".","Omit the branch entirely (defaults to 'main') when unsure: python download-model.py user/model."],"exampleFix":"# before\npython download-model.py meta-llama/Llama-3-8B-Instruct:refs/heads/main\n\n# after\npython download-model.py meta-llama/Llama-3-8B-Instruct:main","handlingStrategy":"validation","validationCode":"import re\n\ndef valid_model_spec(spec: str) -> bool:\n    parts = spec.split(\":\")\n    if len(parts) > 2:\n        return False\n    branch = parts[1] if len(parts) == 2 else None\n    if branch is None or re.fullmatch(r\"[a-zA-Z0-9._-]+\", branch):\n        return True\n    return False\n\nassert valid_model_spec(\"org/model:main\")\nassert not valid_model_spec(\"org/model:refs/heads/main\")","typeGuard":null,"tryCatchPattern":"try:\n    downloader.normalize_input(spec)\nexcept ValueError as e:\n    print(f\"Bad model spec {spec!r}: {e}\"); sys.exit(2)","preventionTips":["Always pass short branch names, never refs/heads/... or refs/pr/....","Shell-quote the model:branch argument.","Validate against ^[a-zA-Z0-9._-]+$ in wrapper scripts before invoking download-model.py."],"tags":["validation","cli","huggingface","input-validation"],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}