Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions docs-website/reference/haystack-api/routers_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,22 @@ Categorizes files or byte streams by their MIME types, helping in context-based

FileTypeRouter supports both exact MIME type matching and regex patterns.

For file paths, MIME types come from extensions, while byte streams use metadata.
You can use regex patterns in the `mime_types` parameter to set broad categories
(such as 'audio/*' or 'text/*') or specific types.
MIME types without regex patterns are treated as exact matches.
For file paths, MIME types come from extensions; byte streams use metadata.
Each entry in `mime_types` is matched against a source's MIME type by exact equality first,
falling back to regex `fullmatch` if equality misses. So `"image/svg+xml"` routes
`image/svg+xml` streams correctly via the equality check (without `+` being interpreted as a
regex quantifier), and patterns like `"audio/.*"` keep matching every audio subtype.

### Usage example

```python
from haystack.components.routers import FileTypeRouter
from pathlib import Path

# For exact MIME type matching
router = FileTypeRouter(mime_types=["text/plain", "application/pdf"])
# Exact MIME matching — `+`-containing IANA types like image/svg+xml work correctly
router = FileTypeRouter(mime_types=["text/plain", "application/pdf", "image/svg+xml"])

# For flexible matching using regex, to handle all audio types
# Regex matching — catch every audio subtype
router_with_regex = FileTypeRouter(mime_types=[r"audio/.*", r"text/plain"])

sources = [Path("file.txt"), Path("document.pdf"), Path("song.mp3")]
Expand Down