---
title: selector | CodeWeaver Docs
description: API reference for codeweaver.engine.chunker.selector
url: "https://docs.knitli.com/api/engine/chunker/selector"
type: static
generatedAt: "2026-04-17T17:21:08.446Z"
---

# selector
       [Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[View in Markdown](/codeweaver/api/engine/chunker/selector.md)       [Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F)[Share on X](https://x.com/intent/tweet?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F&text=selector)[Share on Threads](https://threads.net/intent/post?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F&text=selector)[Share on Bluesky](https://bsky.app/intent/compose?text=selector%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F)[Share on Reddit](https://reddit.com/submit?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F&title=selector)[Share on Hacker News](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F&t=selector)[Share on Email](mailto:?subject=selector&body=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F)[Share on WhatsApp](https://wa.me/?text=selector%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F)[Share on Telegram](https://t.me/share/url?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fengine%2Fchunker%2Fselector%2F&text=selector)
# `codeweaver.engine.chunker.selector`
[Section titled “codeweaver.engine.chunker.selector”](#codeweaverenginechunkerselector)
Intelligent chunker selection based on file language and capabilities.

This module implements the ChunkerSelector which routes files to the appropriate chunker implementation based on language detection and capability analysis. It provides graceful degradation from semantic to delimiter-based chunking when parsing fails or languages are unsupported.

The selector creates fresh chunker instances per file to ensure isolation and prevent state contamination across chunking operations.

## Class: `ChunkerSelector`
[Section titled “Class: ChunkerSelector”](#class-chunkerselector)
Selects appropriate chunker based on file language and capabilities.

The selector uses language detection from file extensions to determine which chunking strategy to employ. It prefers semantic (AST-based) chunking for supported languages and gracefully falls back to delimiter-based chunking when semantic parsing fails or the language is unsupported.

Attributes: governor: ChunkGovernor instance providing resource limits and configuration

Examples: Basic usage with file discovery:

>>> from codeweaver.engine.chunker.base import ChunkGovernor >>> from codeweaver.core import DiscoveredFile >>> from pathlib import Path >>> >>> governor = ChunkGovernor(capabilities=(…)) >>> selector = ChunkerSelector(governor) >>> >>> # Select chunker for a Python file >>> file = DiscoveredFile.from_path(Path(“script.py”)) >>> chunker = selector.select_for_file(file) >>> # Returns SemanticChunker for Python >>> >>> # Process the file >>> chunks = chunker.chunk(file.path.read_text(), file_path=file.path)

Selection Algorithm:

 1. Detect language from file extension using SemanticSearchLanguage
 1. If language is in SemanticSearchLanguage enum:
 - Attempt to create SemanticChunker
 - On ParseError or NotImplementedError, log warning and fall back
 1. Fall back to DelimiterChunker
 1. Return fresh chunker instance (never reused across files)

### Method: `select_for_file`
[Section titled “Method: select_for_file”](#method-select_for_file)

**

```
select_for_file()
```

Select best chunker for given file (creates fresh instance).

Analyzes the file’s extension to determine the appropriate chunking strategy. Always creates a new chunker instance to ensure isolation between file operations.

Args: file: DiscoveredFile with path attribute for language detection

Returns: Fresh BaseChunker instance appropriate for file’s language. Returns SemanticChunker for supported languages or DelimiterChunker for unsupported languages.

Raises: FileTooLargeError: When the file size exceeds the configured `max_file_size_mb` limit in performance settings. The error includes the actual file size, the configured limit, and resolution suggestions. Callers should catch this before the generic `Exception` fallback to log structured size details. Configure via `governor.settings.performance.max_file_size_mb`.

Notes:

 - Each call creates a new chunker instance (no reuse)
 - Falls back to DelimiterChunker for unsupported languages
 - Falls back to delimiter chunking on semantic parse errors
 - Logs warnings when fallback occurs
 - Checks max_file_size_mb from settings before chunking

Examples: >>> from pathlib import Path >>> from codeweaver.core import DiscoveredFile >>> >>> file = DiscoveredFile.from_path(Path(“example.py”)) >>> chunker1 = selector.select_for_file(file) >>> chunker2 = selector.select_for_file(file) >>> assert chunker1 is not chunker2 # Fresh instances

### Method: `select_for_file_path`
[Section titled “Method: select_for_file_path”](#method-select_for_file_path)

**

```
select_for_file_path()
```

Select best chunker for given file path (convenience method).

Creates a DiscoveredFile from the path and delegates to select_for_file. This is a convenience method for when you only have a Path object.

Args: file_path: Path to the file (Path object or path-like)

Returns: Fresh BaseChunker instance appropriate for file’s language.

Examples: >>> from pathlib import Path >>> file_path = Path(“example.py”) >>> chunker = selector.select_for_file_path(file_path)

## Class: `GracefulChunker`
[Section titled “Class: GracefulChunker”](#class-gracefulchunker)
Wraps chunker with graceful degradation to fallback.

This wrapper implements a fallback pattern where a primary chunker is attempted first, and on any failure a fallback chunker is used instead. This enables robust chunking with seamless degradation from sophisticated strategies (semantic) to simpler ones (delimiter, text splitting).

Attributes: primary: First chunker to attempt fallback: Backup chunker to use on primary failure

Examples: Wrapping semantic chunker with delimiter fallback:

>>> from codeweaver.engine.chunker.semantic import SemanticChunker >>> from codeweaver.engine.chunker.delimiter import DelimiterChunker >>> >>> primary = SemanticChunker(governor, SemanticSearchLanguage.PYTHON) >>> fallback = DelimiterChunker(governor, LanguageFamily.C_LIKE) >>> chunker = GracefulChunker(primary, fallback) >>> >>> # This will try semantic first, fall back on error >>> chunks = chunker.chunk(content, file_path=path)

Error Handling:

 - Catches all exceptions from primary chunker
 - Logs warning with error details
 - Attempts fallback chunker
 - Propagates fallback chunker exceptions (no double-fallback)

### Method: `chunk`
[Section titled “Method: chunk”](#method-chunk)

**

```
chunk()
```

Try primary chunker, fall back on error.