{"record":{"id":"c164ab5e6896af13","repo":"deepset-ai/haystack","slug":"invalid-regex-pattern-mime-type","errorCode":null,"errorMessage":"Invalid regex pattern '{mime_type}'.","messagePattern":"Invalid regex pattern '(.+?)'\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/routers/document_type_router.py","lineNumber":105,"sourceCode":"\n        if mime_type_meta_field is None and file_path_meta_field is None:\n            raise ValueError(\n                \"At least one of 'mime_type_meta_field' or 'file_path_meta_field' must be provided to determine MIME \"\n                \"types.\"\n            )\n        self.mime_type_meta_field = mime_type_meta_field\n        self.file_path_meta_field = file_path_meta_field\n\n        if additional_mimetypes:\n            for mime, ext in additional_mimetypes.items():\n                mimetypes.add_type(mime, ext)\n\n        self._mime_type_patterns = []\n        for mime_type in mime_types:\n            try:\n                pattern = re.compile(mime_type)\n            except re.error as e:\n                raise ValueError(f\"Invalid regex pattern '{mime_type}'.\") from e\n            self._mime_type_patterns.append(pattern)\n\n        component.set_output_types(self, unclassified=list[Document], **dict.fromkeys(mime_types, list[Document]))\n        self.mime_types = mime_types\n        self.additional_mimetypes = additional_mimetypes\n\n    def run(self, documents: list[Document]) -> dict[str, list[Document]]:\n        \"\"\"\n        Categorize input documents into groups based on their MIME type.\n\n        MIME types can either be directly available in document metadata or derived from file paths using the\n        standard Python `mimetypes` module and custom mappings.\n\n        :param documents:\n            A list of documents to be categorized.\n\n        :returns:\n            A dictionary where the keys are MIME types (or `\"unclassified\"`) and the values are lists of documents.","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/routers/document_type_router.py#L87-L123","documentation":"DocumentTypeRouter.__init__ compiles each entry in mime_types as a regular expression with re.compile; if a MIME type string is not a valid regex, the re.error is wrapped in this ValueError.","triggerScenarios":"mime_types entries containing regex-special characters that form invalid patterns, e.g. 'text/[' or a stray '(' in a MIME type string.","commonSituations":"Hand-editing MIME type lists; copying patterns with unbalanced brackets; confusing glob-style patterns (e.g. 'image/*' is fine, but broken brackets are not).","solutions":["Fix the MIME type string so it is a valid regex (escape special characters)","Use plain MIME types like 'application/pdf' which are valid regexes","Test your pattern with re.compile(pattern) in a REPL to see the regex error"],"exampleFix":"# before\nrouter = DocumentTypeRouter(mime_types=[\"text/[\"])\n# after\nrouter = DocumentTypeRouter(mime_types=[\"text/html\"])","handlingStrategy":"validation","validationCode":"import re\nfor mt in mime_types:\n    re.compile(mt)  # raises re.error before the router does","typeGuard":"def is_valid_mime_regex(mime_type: str) -> bool:\n    import re\n    try:\n        re.compile(mime_type)\n        return True\n    except re.error:\n        return False","tryCatchPattern":"try:\n    router = DocumentTypeRouter(mime_types=mime_types, mime_type_meta_field=\"meta.mimetype\")\nexcept ValueError as e:\n    if \"Invalid regex pattern\" in str(e):\n        mime_types = [mt for mt in mime_types if is_valid_mime_regex(mt)]\n        router = DocumentTypeRouter(mime_types=mime_types, mime_type_meta_field=\"meta.mimetype\")\n    else:\n        raise","preventionTips":["Treat mime_types entries as regexes; escape special characters","Prefer plain MIME type strings unless you need pattern matching","Test each pattern with re.compile in unit tests"],"tags":["python","regex","validation","mime-type"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}