AI Engineer Portal
Your personal operating system for career transition.
Exercise
Implement a JSONL ingestion pipeline with error isolation
Implement a JSONL Ingestion Pipeline with Error Isolation
JSONL (JSON Lines) is the standard format for AI datasets, evaluation sets, and training data. An ingestion pipeline reads JSONL records, validates them, transforms them, and writes valid records to an output file — while isolating failures per record so a single malformed entry does not abort the whole run.
What to build
Implement an IngestionPipeline class that:
-
__init__(schema: type[BaseModel], transform: Callable[[BaseModel], dict])— accept a Pydantic model class for input validation and a transform function that converts a validated record to an output dict. -
run(input_path: str, output_path: str, overwrite: bool = False) -> IngestionResult— read the JSONL file atinput_path, validate each line againstschema, applytransform, write valid records tooutput_path, and return anIngestionResult. -
IngestionResult— a dataclass with:total_lines: int— total lines read (including blank lines)processed: int— successfully validated and transformedfailed: int— lines that failed parsing or validationskipped: int— blank or whitespace-only linesfailures: list[dict]— each withline_number,raw, anderror
-
Idempotency — if
output_pathalready exists andoverwrite=False, raiseFileExistsErrorwith a clear message. Ifoverwrite=True, overwrite it. -
Atomic write — write to a temp file first, then rename to
output_path. This prevents partial output files from being left on disk if the process is interrupted.
Test schema and transform
class RawArticle(BaseModel):
id: str = Field(min_length=1)
title: str = Field(min_length=1)
body: str = Field(min_length=50)
source: str
def transform_article(article: RawArticle) -> dict:
return {
"id": article.id,
"title": article.title.strip(),
"body_length": len(article.body),
"source": article.source,
}
Why this matters
The difference between an idempotent pipeline and one that cannot be safely rerun is a detail that matters enormously at 3 AM when you need to reprocess a dataset. The atomic write prevents the worst case: a half-written output file that looks valid but contains garbage after the interrupt point.
Constraints
- Use
pathlib.Pathfor file operations, not bareopen(). - The temp file should be in the same directory as
output_pathto ensure the rename is atomic on the same filesystem. - Empty lines and whitespace-only lines count as
skipped, notfailed.
Data Transformation / hard / Step 16 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.
This is the end of the current mini-sequence.
- - 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.