Transient failures — rate limits, network blips, 5xx responses — are the most common way automations break. A retry wrapper turns a one-shot failure into a brief pause before trying again. What's the loop shape?
for attempt in range(max_attempts) with a try/except inside?
Exactly. The try block makes the call and returns on success. The except block catches the exception. On every attempt except the last, the loop continues. On the last attempt, you re-raise so the caller can see the real error:
for attempt in range(1, max_attempts + 1):
try:
return toolset.execute_action(action, params)
except Exception:
if attempt == max_attempts:
raiseSo the caller gets the happy result on success, or the final exception if every attempt fails?
Right. Transient failures are silent — you retry and move on. Terminal failures propagate so nothing upstream pretends everything worked. Here's the function with attempt tracking:
def retry_with_backoff(action: str, params: dict, max_attempts: int) -> dict:
for attempt in range(1, max_attempts + 1):
try:
result = toolset.execute_action(getattr(Action, action), params)
print(f"Succeeded on attempt {attempt}/{max_attempts}")
return result
except Exception as exc:
print(f"Attempt {attempt}/{max_attempts} failed: {exc}")
if attempt == max_attempts:
raise
return {}getattr(Action, action) — that lets me pass the action name as a string and still use the enum?
Yes. The wrapper is generic across every action. Pass "GMAIL_FETCH_EMAILS" and the function resolves it to Action.GMAIL_FETCH_EMAILS at runtime. One wrapper, every API.
Combined with yesterday's idempotent creates, this means I can retry writes safely — each retry either finds the existing record or creates it exactly once?
Retry plus idempotency is the core pair of production automation. You can now retry writes without fear of duplicates. Week 4 builds on exactly that combination.
TL;DR: for attempt in range(1, max_attempts + 1) with try/except — return on success, re-raise on the last failure.
| Pair | Effect |
|---|---|
| retry + non-idempotent | duplicates on every transient failure |
| retry + idempotent | at most one record, ever |
Transient failures — rate limits, network blips, 5xx responses — are the most common way automations break. A retry wrapper turns a one-shot failure into a brief pause before trying again. What's the loop shape?
for attempt in range(max_attempts) with a try/except inside?
Exactly. The try block makes the call and returns on success. The except block catches the exception. On every attempt except the last, the loop continues. On the last attempt, you re-raise so the caller can see the real error:
for attempt in range(1, max_attempts + 1):
try:
return toolset.execute_action(action, params)
except Exception:
if attempt == max_attempts:
raiseSo the caller gets the happy result on success, or the final exception if every attempt fails?
Right. Transient failures are silent — you retry and move on. Terminal failures propagate so nothing upstream pretends everything worked. Here's the function with attempt tracking:
def retry_with_backoff(action: str, params: dict, max_attempts: int) -> dict:
for attempt in range(1, max_attempts + 1):
try:
result = toolset.execute_action(getattr(Action, action), params)
print(f"Succeeded on attempt {attempt}/{max_attempts}")
return result
except Exception as exc:
print(f"Attempt {attempt}/{max_attempts} failed: {exc}")
if attempt == max_attempts:
raise
return {}getattr(Action, action) — that lets me pass the action name as a string and still use the enum?
Yes. The wrapper is generic across every action. Pass "GMAIL_FETCH_EMAILS" and the function resolves it to Action.GMAIL_FETCH_EMAILS at runtime. One wrapper, every API.
Combined with yesterday's idempotent creates, this means I can retry writes safely — each retry either finds the existing record or creates it exactly once?
Retry plus idempotency is the core pair of production automation. You can now retry writes without fear of duplicates. Week 4 builds on exactly that combination.
TL;DR: for attempt in range(1, max_attempts + 1) with try/except — return on success, re-raise on the last failure.
| Pair | Effect |
|---|---|
| retry + non-idempotent | duplicates on every transient failure |
| retry + idempotent | at most one record, ever |
Create a free account to get started. Paid plans unlock all tracks.