Skip to content

feat(stdlib): add ChunkingStrategy ABC and built-in chunkers #899

Closed

Description

@planetf1

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.py exists with ChunkingStrategy ABC and split(accumulated_text: str) -> list[str] abstract method
  • SentenceChunking, WordChunking, and ParagraphChunking implementations 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.py covering:
    • 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

  1. added
    enhancement
    good first issue
    on Apr 22, 2026
  2. self-assigned this
    on Apr 22, 2026
  3. added this to the Release 0.5.0 milestone on Apr 22, 2026
  4. planetf1 commented on Apr 24, 2026

    @planetf1
    ContributorAuthor

    Implementation notes:

    • SentenceChunking needs 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, or re.split(r'(?<=[.!?])\s+', text) as a starting point.
    • The chunker must be stateful — it tracks the last split position so that when called again with more accumulated text, it only returns new complete chunks since the last call, not all chunks from the beginning. Without this, the orchestrator would re-validate already-seen chunks.
    • String alias resolution ("sentence" → SentenceChunking()) should be a simple factory function or dict lookup, not a class method — keeps the ABC clean.
  5. added 3 commits that reference this issue on Apr 27, 2026
    4b27e2e
    a4ac2bd
    61448a9
  6. added 2 commits that reference this issue on May 27, 2026
    9eef000
    1e5c665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementgood first issue

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions