First write lesson of the track. You need to create a task — but only if a task with that title doesn't already exist. Re-running must produce the same task, not a duplicate. What's the shape?
List the tasks first, look for the title, and only create if nothing matches?
Exactly. Read-then-write with an early return. The list check runs first; if a match is found, return its ID. If no match, create a new task and return its ID. The function's contract is 'give me the ID of a task with this title' — it never promises a new one:
for item in existing.get("items", []):
if item.get("title") == title:
return item["id"]The return inside the loop exits immediately when a match is found — so the create call runs only when the loop falls through?
Right. Early return is the cleanest way to express 'if found, stop; otherwise, continue.' Here's the full idempotent pattern:
def idempotent_create_task(title: str) -> str:
existing = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": 100})
for item in existing.get("items", []):
if item.get("title") == title:
print(f"Task '{title}' already exists with id={item['id']}")
return item["id"]
created = toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
print(f"Created new task '{title}' with id={created['id']}")
return created["id"]If I run this ten times with the same title, only the first call creates a task and the other nine short-circuit to the existing ID?
Exactly. That is idempotency: calling the function ten times leaves exactly one task in your list. Retries, reruns, manual executions — all safe. This is why idempotent writes are the foundation of reliable automation.
So now I can re-run any pipeline that creates this task without worrying about duplicates in my real task list?
Safe reruns forever. Every write in Week 4 starts from this pattern.
TL;DR: List existing records, return the matching ID if found, otherwise create and return the new ID.
item.get("title") == title defines uniqueness| Without | With |
|---|---|
| retry creates duplicate | retry finds existing, returns same ID |
| re-run doubles the task list | re-run is a no-op |
First write lesson of the track. You need to create a task — but only if a task with that title doesn't already exist. Re-running must produce the same task, not a duplicate. What's the shape?
List the tasks first, look for the title, and only create if nothing matches?
Exactly. Read-then-write with an early return. The list check runs first; if a match is found, return its ID. If no match, create a new task and return its ID. The function's contract is 'give me the ID of a task with this title' — it never promises a new one:
for item in existing.get("items", []):
if item.get("title") == title:
return item["id"]The return inside the loop exits immediately when a match is found — so the create call runs only when the loop falls through?
Right. Early return is the cleanest way to express 'if found, stop; otherwise, continue.' Here's the full idempotent pattern:
def idempotent_create_task(title: str) -> str:
existing = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": 100})
for item in existing.get("items", []):
if item.get("title") == title:
print(f"Task '{title}' already exists with id={item['id']}")
return item["id"]
created = toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {"title": title})
print(f"Created new task '{title}' with id={created['id']}")
return created["id"]If I run this ten times with the same title, only the first call creates a task and the other nine short-circuit to the existing ID?
Exactly. That is idempotency: calling the function ten times leaves exactly one task in your list. Retries, reruns, manual executions — all safe. This is why idempotent writes are the foundation of reliable automation.
So now I can re-run any pipeline that creates this task without worrying about duplicates in my real task list?
Safe reruns forever. Every write in Week 4 starts from this pattern.
TL;DR: List existing records, return the matching ID if found, otherwise create and return the new ID.
item.get("title") == title defines uniqueness| Without | With |
|---|---|
| retry creates duplicate | retry finds existing, returns same ID |
| re-run doubles the task list | re-run is a no-op |
Create a free account to get started. Paid plans unlock all tracks.