deepset-ai/haystack · error · ValueError
Invalid regex pattern '{mime_type}'.
Error message
Invalid regex pattern '{mime_type}'. What it means
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.
Source
Thrown at haystack/components/routers/document_type_router.py:105
if mime_type_meta_field is None and file_path_meta_field is None:
raise ValueError(
"At least one of 'mime_type_meta_field' or 'file_path_meta_field' must be provided to determine MIME "
"types."
)
self.mime_type_meta_field = mime_type_meta_field
self.file_path_meta_field = file_path_meta_field
if additional_mimetypes:
for mime, ext in additional_mimetypes.items():
mimetypes.add_type(mime, ext)
self._mime_type_patterns = []
for mime_type in mime_types:
try:
pattern = re.compile(mime_type)
except re.error as e:
raise ValueError(f"Invalid regex pattern '{mime_type}'.") from e
self._mime_type_patterns.append(pattern)
component.set_output_types(self, unclassified=list[Document], **dict.fromkeys(mime_types, list[Document]))
self.mime_types = mime_types
self.additional_mimetypes = additional_mimetypes
def run(self, documents: list[Document]) -> dict[str, list[Document]]:
"""
Categorize input documents into groups based on their MIME type.
MIME types can either be directly available in document metadata or derived from file paths using the
standard Python `mimetypes` module and custom mappings.
:param documents:
A list of documents to be categorized.
:returns:
A dictionary where the keys are MIME types (or `"unclassified"`) and the values are lists of documents.View on GitHub (pinned to e318778c9b)
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
Example fix
# before router = DocumentTypeRouter(mime_types=["text/["]) # after router = DocumentTypeRouter(mime_types=["text/html"])
Defensive patterns
Strategy: validation
Validate before calling
import re
for mt in mime_types:
re.compile(mt) # raises re.error before the router does Type guard
def is_valid_mime_regex(mime_type: str) -> bool:
import re
try:
re.compile(mime_type)
return True
except re.error:
return False Try / catch
try:
router = DocumentTypeRouter(mime_types=mime_types, mime_type_meta_field="meta.mimetype")
except ValueError as e:
if "Invalid regex pattern" in str(e):
mime_types = [mt for mt in mime_types if is_valid_mime_regex(mt)]
router = DocumentTypeRouter(mime_types=mime_types, mime_type_meta_field="meta.mimetype")
else:
raise Prevention
- 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
When it happens
Trigger: mime_types entries containing regex-special characters that form invalid patterns, e.g. 'text/[' or a stray '(' in a MIME type string.
Common situations: Hand-editing MIME type lists; copying patterns with unbalanced brackets; confusing glob-style patterns (e.g. 'image/*' is fine, but broken brackets are not).
Related errors
- Invalid MIME type or regex pattern '{mime_type}'.
- The list of mime types cannot be empty.
- At least one of 'mime_type_meta_field' or 'file_path_meta_fi
- The list of mime types cannot be empty.
- Tool execution requires at least one tool.
AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30).
Data as JSON: /api/errors/c164ab5e6896af13.
Report an issue: GitHub.