AI Engineer Portal
Your personal operating system for career transition.
Exercise
Create a streaming document processor with generators
Create a Streaming Document Processor with Generators
Loading 10GB of documents into memory before processing them is the fastest path to an OOM kill. Generator pipelines let you process arbitrarily large corpora in constant memory by pulling one record at a time through each stage.
What to build
Implement a composable generator pipeline for document ingestion:
read_jsonl(path: Path) -> Iterator[dict]— yields one parsed dict per line, skipping malformed lines with a warning (do not abort).validate_and_clean(records: Iterator[dict]) -> Iterator[dict]— yields only records with non-emptyidandbodyfields (min 50 chars). Strips whitespace frombody.chunk_document(doc: dict, max_chars: int) -> Iterator[dict]— yields chunk dicts{doc_id, chunk_index, text, char_start}by splittingbodyatmax_charsboundaries.stream_chunks(docs: Iterator[dict], max_chars: int) -> Iterator[dict]— composeschunk_documentover a stream of docs usingyield from.write_jsonl(records: Iterator[dict], path: Path) -> int— writes each record as a JSONL line, returns total count written.run_pipeline(input_path: Path, output_path: Path, chunk_size: int) -> dict— wires all stages together, returns{"chunks_written": int, "parse_errors": int, "docs_skipped": int}.
Why this matters
Generator pipelines are the standard pattern for large-scale document ingestion. Understanding how to compose them lets you build ETL workflows that scale to any corpus size without changing the architecture.
Constraints
- No
list()wrapping of intermediate iterators — keep every stage lazy. - Failures at one stage must not abort downstream stages; capture counts instead.
- Use
yield frominstream_chunksfor clean composition.
Python Ai / medium / Step 3 of 6
General drill
Keep the solution explicit and reviewable.
Make the solution explicit, debuggable, and easy to explain.
Review where the boundary is, what gets validated, and what would be hard to debug later.
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.