AI Engineer Portal
Your personal operating system for career transition.
Exercise
Build a data pipeline with generator composition
Build a Data Pipeline with Generator Composition
Document ingestion pipelines need to process large corpora efficiently. Loading an entire dataset into memory before processing creates unnecessary memory pressure and prevents you from streaming results to storage as they are produced. Python generators solve both problems — but only if you compose them correctly.
What to build
Implement a complete document ingestion pipeline using generator composition:
read_jsonl(path: str) -> Iterator[dict]— reads a JSONL file line by line, skipping blank lines and logging (not aborting) parse errors.validate_doc(records: Iterator[dict]) -> Iterator[dict]— yields only records that have non-empty"id"and"body"fields (minimum 50 chars). Log skipped records.normalize_doc(records: Iterator[dict]) -> Iterator[dict]— strips whitespace frombody, lowercases the"source"field if present, adds"char_count": int.chunk_doc(records: Iterator[dict], max_chars: int = 500) -> Iterator[dict]— splits each document body into chunks of at mostmax_chars, yielding one chunk dict per chunk with fields:"doc_id","chunk_index","text","metadata".write_jsonl(records: Iterator[dict], path: str) -> int— writes records to a JSONL file and returns the count of records written.run_pipeline(input_path: str, output_path: str, chunk_size: int = 500) -> dict— composes all stages and returns{"docs_read", "docs_valid", "chunks_written"}.
Why this matters
A pipeline that processes one document at a time uses O(1) memory regardless of corpus size. A 10GB JSONL file and a 10MB file should use the same peak memory. Generator composition is the idiomatic Python way to achieve this, and it is used in virtually every production data ingestion pipeline.
Constraints
- Each stage must be a generator function — no materializing lists between stages.
run_pipelinemust work by chaining the generators together, not calling them sequentially.- The pipeline should process at least one test document end-to-end.
Data Transformation / hard / Step 10 of 16
Transform and summarize data safely
Think like an evaluation or trace pipeline: group clearly, preserve the data story, and make edge cases visible instead of clever.
- - Handles missing or noisy records predictably
- - Produces summary output that is easy to inspect
- - Would be safe to rerun in a script workflow
- - Do grouped metrics stay readable?
- - Would malformed rows be debuggable?
- - Did I choose names that explain the transformation?
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.