Handle triggers and duplicate events
Filter event types and prevent repeat deliveries from creating repeat work. This module has 3 lessons and takes about 1h 50m. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 04 in the Learning LabSelect events deliberately
Return True only when event is a dictionary whose type equals request.created.
Systems can emit many event types. An automation for new enquiries should listen for request.created and ignore unrelated events. A filter is a deliberate entry rule.
Read a dictionary field with get so a missing type does not crash the filter. Return a boolean that the next step can use directly.
This filter does not verify who sent an event. A real webhook also needs the provider authentication and replay checks.
See it in action
def solve(event):
return isinstance(event, dict) and event.get("type") == "request.created"
print(solve({"type":"request.created"}))
This filter does not verify who sent an event. A real webhook also needs the provider authentication and replay checks.
Keep the first delivery
Return unique event id strings in first-seen order.
A provider may deliver the same event more than once. Use a stable event id to detect this. Two events with equal text may still be separate legitimate requests.
A set tracks ids already seen. Add an id only when accepting its first occurrence. Return the unique ids in first-seen order.
An in-memory set only lasts for the current run. Production deduplication needs durable storage and a uniqueness rule.
See it in action
def solve(event_ids):
seen, unique = set(), []
for event_id in event_ids:
if event_id not in seen:
seen.add(event_id)
unique.append(event_id)
return unique
print(solve(["e1","e1","e2"]))
An in-memory set only lasts for the current run. Production deduplication needs durable storage and a uniqueness rule.
Practical: Build an event inbox
Return new request.created event ids in order, skipping malformed, empty, unrelated, and duplicate events. Do not modify processed.
Combine filtering and deduplication before expensive processing. The inbox accepts only request.created events with a non-empty string id that is not already in the processed set.
Copy the processed set at the start, then update the copy as you accept items. This prevents duplicates inside the same batch as well as previously handled deliveries.
Accepting an event is not the same as completing it. A real system persists receipt and processing state separately.
Your experiment plan
Use the browser editor or the notebook. All records and AI responses are fictional. No network calls, credentials, or real messages are used.
- Create five events including a duplicate and an unrelated event.
- Run the example and predict its result before changing it.
- Complete the function in Practice and run the checks. Add your own normal, empty, and failure cases.
- Replay the batch with the accepted ids in processed and inspect the empty result.
- Save the notebook with your observations and explain one limitation of the simulation.
What to hand in
- An event inbox simulation and a replay test.
- A test table with inputs, expected outputs, actual outputs, and one revision.
Reflect on your results
Where would durable state live in a real system?
The automatic exercise checks the core code behaviour. Review observations and written deliverables yourself or with a mentor.
See it in action
def solve(events, processed):
seen, accepted = set(processed), []
for event in events:
if not isinstance(event, dict):
continue
event_id = event.get("id")
if event.get("type") == "request.created" and isinstance(event_id, str) and event_id.strip() and event_id not in seen:
accepted.append(event_id)
seen.add(event_id)
return accepted
print(solve([{"id":"e1","type":"request.created"},{"id":"e1","type":"request.created"},{"id":"e2","type":"request.updated"}], []))
Accepting an event is not the same as completing it. A real system persists receipt and processing state separately.
Free, open and run by a nonprofit.
This page is part of AI Automation, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.