Default fallbacks and validation
Callers will send bad data. They will ask for "sentmient" with a typo. They will ask for the same task twice. They will send an empty list by accident. A good API does not return a 400 for every fat-fingered request. It validates gently, drops unknown values, and falls back to a sensible default.
def _validate_tasks(tasks: list[str]) -> list[str]:
seen = []
for t in tasks:
t = t.lower().strip()
if t in VALID_TASKS and t not in seen:
seen.append(t)
if not seen:
seen = ["sentiment", "summary"]
return seenThree things happen here. Normalise to lowercase. Drop anything not in VALID_TASKS. De-duplicate while preserving order. Fall back to a safe pair if nothing valid remains. Silent, forgiving, predictable.
Matching exercise: Match each input to what _validate_tasks returns
Loading practice…
Checkpoint: Selective dispatch checkpoint
Loading practice…