DVPPython Studio
Module 6: Shape trustworthy data / Build 1 of 4

Create a preference record

Represent a declared preference with an exact schema, controlled verdict and fresh ownership—not an invented training-quality guarantee.

Runs on your computer · 55–80 minutes · no paid services

Download practice filesFiles, commands & notes

Without JavaScript, use the step links and keep your files on your computer.

One useful idea

A preference record combines source references with a human judgment. Chosen and rejected name two distinct assets; prompt explains their context; evaluator and timestamp record supplied attribution. These are declared facts, not verified identities, rights or evaluation quality. The five supplied records are invented fixtures. A valid schema does not make data ready for RLHF/DPO training.

A dataclass generates a constructor and groups named fields. The kit uses @dataclass(frozen=True) because this value has scalar fields and should not be reassigned after validation. Its __post_init__ runs explicit guards. Type annotations alone do not run checks. Verdict is a string Enum with prefer_chosen and needs_review values; .value supplies JSON text. A review-needed comparison retains ordered assets but declares no winner.

The mapping has exactly nine keys: schema_version, record_id, prompt, chosen_id, rejected_id, verdict, reason, evaluator, created_at. Require an actual integer version 1, not bool; IDs are 1–64 ASCII letters/digits/underscore/hyphen and asset IDs differ. Prompt/reason are nonblank, at most 4,000/1,000 characters, preserving original Unicode, whitespace and newlines; other C0 controls and surrogate code points are refused. Timestamp is calendar-valid whole-second UTC in YYYY-MM-DDTHH:MM:SSZ. A canonical timestamp does not prove the event happened.

Preference.from_mapping validates that exact shape and constructs the frozen value. to_dict returns fresh scalar data with the Enum’s string value. JSON serialization belongs at a boundary; keep calculation/validation separate from print. Reject missing/extra fields rather than quietly fill or discard meaning. This is a versioned classroom contract, not a universal preference format.

Refresh first: Data-kit environment setup, Classes and runtime validation, JSON records.

Trace a finished example

from dataset_tools.core import preference_record
from dataset_tools.model import Preference

row = {"schema_version": 1, "record_id": "pair_1",
       "prompt": "Café at dusk — 海", "chosen_id": "asset_a",
       "rejected_id": "asset_b", "verdict": "prefer_chosen",
       "reason": "Cleaner horizon", "evaluator": "reviewer_1",
       "created_at": "2026-10-07T12:00:00Z"}
result = preference_record(row)
value = Preference.from_mapping(result)
print(value.verdict.value)
print(result["prompt"])
print(result is row)

The reference validates the mapping and returns a different dictionary. A reviewed dataclass reconstructs the same supplied preference and exposes its controlled Enum value. Unicode prompt text survives unchanged. False describes ownership, not whether the judgment is correct.

The finished implementation is in dataset_tools/core.py. Reading it is guided practice, not independent evidence.

Predict a review verdict

Does needs_review declare that chosen_id is a training winner?

Compare your answer · self-reviewed

No. Ordered assets remain available for review, but no winner or training eligibility is declared. A schema pass does not certify preference quality.

Find the type trap

Should schema_version=True pass because bool is an int subclass?

Compare your answer · self-reviewed

No. This contract requires an actual integer 1. An explicit type/version guard prevents a boolean from becoming a version number.

Recall original text

Should serialization strip the original prompt’s whitespace?

Compare your answer · self-reviewed

No. Validate nonblank bounded text but preserve the original. Source text and a normalization used for lookup are different evidence.

Try the idea in this browser

Runs in this browser · optional preparation · local project checks remain separate

Try a small function before opening your local files. Python downloads when you choose Run; if it cannot load, your code stays here and the local kit still works. The worker executes on your device, not on a DVP server. Only run code you trust: this is not a hostile-code security sandbox.

JavaScript loads the practice controls. Python starts only after Run.

Read the browser task briefs without running Python

Guided preference schema

Reuse the disclosed reviewed Preference/Verdict support, or write equivalent validation. Write your own preference_record wrapper; the finished task function is not supplied. Use only invented in-memory rows here. No file, CSV read, JSONL stream, byte receipt or provenance operation runs. Preserve source data and reconcile accepted/quarantined rows; needs_review is unresolved, not a training winner. This is browser preparation, not local project or Portfolio II evidence. Implement preference_record in practice.py. Reviewed Preference/Verdict or the small identifier, text and utc_timestamp validators may be reused; disclose that support. Enforce exact schema/type/identity/verdict/calendar rules and return fresh serialized data. Do not import the finished preference_record task or claim reviewed helper use is independent implementation of those helpers.

Independent preference schema

Reuse the disclosed reviewed Preference/Verdict support, or write equivalent validation. Write your own preference_record wrapper; the finished task function is not supplied. Use only invented in-memory rows here. No file, CSV read, JSONL stream, byte receipt or provenance operation runs. Preserve source data and reconcile accepted/quarantined rows; needs_review is unresolved, not a training winner. This is browser preparation, not local project or Portfolio II evidence. Implement preference_record in practice.py. Reviewed Preference/Verdict or the small identifier, text and utc_timestamp validators may be reused; disclose that support. Enforce exact schema/type/identity/verdict/calendar rules and return fresh serialized data. Do not import the finished preference_record task or claim reviewed helper use is independent implementation of those helpers.

Change it, then build your own

One controlled change

Change the prompt to Unicode with a newline and verdict to needs_review. Then try identical asset IDs, a boolean version and an invalid UTC date. Predict which cases preserve data versus refuse it.

Your independent task

Implement preference_record in practice.py. Reviewed Preference/Verdict or the small identifier, text and utc_timestamp validators may be reused; disclose that support. Enforce exact schema/type/identity/verdict/calendar rules and return fresh serialized data. Do not import the finished preference_record task or claim reviewed helper use is independent implementation of those helpers.

What success looks like

The build1 group checks changed inputs, fresh ownership, Unicode/serialization, all declared refusal boundaries and the frozen reviewed model. Your function may use disclosed model support. Passing it is not proof of dataset eligibility, reviewer identity or independent portfolio completion.

Hint 1 · a question

List the nine keys and which are source references versus judgments. Which invalid cases must not become default values?

Hint 2 · a concept cue

Validate the exact schema and supplied scalar values, then construct a fresh result. A reviewed model can supply that validation if you disclose it.

Hint 3 · a localized example

Verdict.NEEDS_REVIEW.value is the string "needs_review". Enum membership and frozen fields do not by themselves verify that a preference is useful training data.

Need the complete worked solution?

Open dataset_tools/core.py from the kit. Trace it, close it, then try fresh inputs in your own files. Treat the attempt as guided; seeing the solution does not award a practical pass.

Course help is guidance, not independent evidence. With JavaScript, opening help records guidance locally; otherwise note it in your README. Reset does not erase that history.

Repair a failed check

If a bool version passes, inspect the type guard. If an unknown verdict survives, construct the controlled Enum or validate its allowed text. If changing a returned reason rewrites the source, return fresh ownership. If a date-shaped but impossible date passes, validate the calendar as well as its text shape.

NotImplementedError means a practice stub is still unfinished. Read the failing test name and the last error line. Change one behavior, rerun that build, then rerun all implemented builds.

Show it works on new inputs

Author five new invented records, including one review-needed comparison and Unicode/newline text. Show changed-input serialization plus identical-ID and invalid-date refusals. Explain supplied facts, unverified judgments and any reviewed helper assistance.

Self-review: name the input, result, refused case and reason. Your local test output and explanation are separate from a quiz score; this page does not certify a pass.

Keep the idea

An explicit record preserves meaning and ownership. Schema validity is one boundary, not a training-quality certificate.