Repository navigation
Description
What
Add a new module mellea/stdlib/chunking.py with a ChunkingStrategy abstract base class and three built-in implementations.
Why
Streaming validation requires semantically meaningful chunks — not raw token deltas. The split() method takes the accumulated text so far and returns only the complete chunks, holding back any trailing incomplete fragment. Without this, requirements receive partial sentences or words that are not meaningful units for validation.
Implementation
New file mellea/stdlib/chunking.py:
class ChunkingStrategy(ABC):
@abstractmethod
def split(self, accumulated_text: str) -> list[str]: ...
class SentenceChunking(ChunkingStrategy): ...
class WordChunking(ChunkingStrategy): ...
class ParagraphChunking(ChunkingStrategy): ...Key contract: split() receives the full accumulated text since generation started (not a raw delta), and returns only complete chunks. Any trailing incomplete fragment is held back and included in the next call. This avoids requirements receiving partial sentences mid-stream.
String aliases "sentence", "word", and "paragraph" should be accepted wherever a ChunkingStrategy is expected, resolving to the corresponding class.
The interface is designed to be forward-compatible with MOT.stream_parsed_repr (Option C, future work) — when that lands, MOT-native chunking will produce the same list[str] output, making it a drop-in replacement at call sites with no requirement code changes.
Export the ABC and built-ins from mellea.stdlib.
Acceptance criteria
-
mellea/stdlib/chunking.pyexists withChunkingStrategyABC andsplit(accumulated_text: str) -> list[str]abstract method -
SentenceChunking,WordChunking, andParagraphChunkingimplementations exist - Each chunker holds back trailing incomplete fragments and only returns complete chunks
- String aliases
"sentence","word","paragraph"resolve to the correct class - Exported from
mellea.stdlib - Unit tests in
test/stdlib/test_chunking.pycovering:- Each chunker with typical input
- Empty string input
- Input with no complete chunk yet (returns empty list, fragment held back)
- Trailing fragment correctly held back across two calls
- Google-style docstring on the ABC and each built-in, including the accumulated-text contract
Part of #891
Activity
planetf1 commentedon Apr 24, 2026
Implementation notes:
SentenceChunkingneeds care with abbreviations and decimal numbers — naive splitting on.will break on "Dr. Smith scored 3.5". Consider using a regex that handles common abbreviations, orre.split(r'(?<=[.!?])\s+', text)as a starting point."sentence"→SentenceChunking()) should be a simple factory function or dict lookup, not a class method — keeps the ABC clean.feat(stdlib): add ChunkingStrategy ABC and built-in chunkers (generat…
feat(stdlib): flush trailing chunk fragment at end of stream
feat(stdlib): flush trailing chunk fragment at end of stream
feat(stdlib): add stream_with_chunking() with per-chunk validation (g…
feat(stdlib): add ChunkingStrategy ABC and built-in chunkers (generat…
feat(stdlib): add stream_with_chunking() with per-chunk validation (g…