Celery Pipelines with chain, group and chord: Transcribe, Translate in Parallel, Bundle, and Handle a Failed Branch
Transcribe once, translate into German, Hindi and Korean side by side, then bundle the SRTs. chain sets the order, group runs branches together, chord waits for all. Real timings: 19 s with four slots, 32.5 s with one. A failed Korean branch leaves your table saying TRANSLATING forever until on_error writes FAILED.
What you will learn
- Build a pipeline with chain, group and chord and read how results pass between steps
- Pass file keys between tasks instead of contents, and measure why
- Explain why a chord needs a result backend and free worker slots
- Make a failed branch, a hung step and a killed worker end with FAILED written in your own table
Before you start
- Celery and Redis for background tasks
- Safe to run twice
A real job is rarely one step. The subtitle app does everything in one task, and that's fine until you want the same video in three more languages. Here one task becomes a pipeline: transcribe a video, translate it into three languages at the same time, then bundle the subtitle files. Every step has a clock on it, so nothing here is a guess.
Think of a multi-course meal. The starter comes first, and nothing else leaves the kitchen before it. Then three chefs work on the mains at once, and they don't finish together. Dessert goes out only when the slowest chef is done. Chain is the order, group is the chefs working together, and chord is dessert waiting for all of them.
The whole pipeline
from celery import chain, chord, group
from app.tasks import bundle, mark_failed
from app.tasks import transcribe, translate
LANGS = ["de", "hi", "ko"]
def plain(project_id):
return chain(
transcribe.s(project_id),
chord(
group(translate.s(lang) for lang in LANGS),
bundle.s(project_id),
),
)
def guarded(project_id):
errback = mark_failed.s(project_id)
return plain(project_id).on_error(errback)
chain runs what's inside one after another, so transcribe goes first. group makes one translate task per language, German, Hindi and Korean, side by side. chord is a group plus one final task, and bundle runs only when every branch has finished. The .s on each one writes the call down, like a ticket; nothing runs until you call apply_async. This Celery code is the same in Flask and Django; only the wiring around it differs.
The result rides the arrow
@celery_app.task(name="transcribe")
def transcribe(project_id):
store.set_status(project_id, "TRANSCRIBING")
work(project_id, "transcribe")
return store.save(project_id, "transcript.txt", "Hi.")
@celery_app.task(name="translate")
def translate(transcript_key, lang):
project_id = transcript_key.split("/")[0]
store.set_status(project_id, "TRANSLATING")
work(project_id, lang)
text = store.read(transcript_key)
return store.save(project_id, f"{lang}.srt", text)
@celery_app.task(name="bundle")
def bundle(subtitle_keys, project_id):
work(project_id, "bundle")
key = store.zip_files(project_id, subtitle_keys)
store.set_status(project_id, "COMPLETED")
return key
Transcribe saves its text and returns one small string, the key of that file. Translate was only given the language, but it takes two arguments: the chain fills in the first with whatever came before, so every branch gets the key. Bundle receives a list, one key per branch. Hindi finished first and German last, but the list stays German, Hindi, Korean, and the zip shows the same order. The chord hands over one list in the order you wrote it.
Pass ids, not contents
A ticket carrying just the key was about 1,700 bytes. The same ticket carrying a 1 MB transcript was 1.3 MB, almost 800 times bigger, and every branch gets its own copy. The return value sits in the result backend too, one more megabyte, kept for 24 hours. The ticket rail is for tickets: keep the food in the kitchen, save the file, pass its key.
Somebody has to count
Hindi can finish on one worker and German on another, and those workers share nothing. So who knows that all three are done? The result backend. Inside Redis, every finished branch adds one to a counter: one of three, two of three, three of three, then it sends bundle. With the backend switched off, the send itself fails with NotImplementedError: starting chords requires a result backend. Nothing reaches the queue, which is good, because it fails loudly.
They really overlap, if there are slots
Transcribe runs alone for 3 seconds. All three branches start within a fraction of a second. German ends at 17 seconds, and bundle starts right then. With only one slot, the same code had no overlap at all and took 32.5 seconds instead of 19. A group only runs in parallel if there are free slots; otherwise it's tickets waiting in a line.
Korean fails. Now what?
A switch makes the Korean branch raise an error when it finishes. Hindi is done by then. German keeps going, nearly six more seconds, and nobody tells it anything: Celery doesn't cancel the other branches. The counter still reaches three of three, because a failure counts as finished. Only then does Celery look at the results and find the error. Bundle never runs, and a chord error is written on the bundle task, in the backend only. Your own table, 35 seconds in, still says TRANSLATING, and nothing in the code will ever change that. The page would spin forever.
Mark it FAILED
@celery_app.task(name="mark_failed")
def mark_failed(request, exc, traceback, project_id):
store.set_status(project_id, "FAILED", str(exc))
The guarded pipeline adds on_error(mark_failed.s(project_id)). When the chord error fires, mark_failed writes FAILED and the reason into the table. But Korean failed at 11 seconds and the status changed at 17: the errback fires when the slowest branch ends, not when the branch fails. Because it's on the chain, it covers every step. If transcribe fails, it's marked at 3 seconds and no branch starts. If bundle fails, it's marked at 19, and the German and Hindi files stay on disk. If the page needs the bad news sooner, the steps table already shows Korean failed at 11 seconds.
When nothing reports
- A branch hangs. German never finishes, and the chord waits for German. With a 25-second time limit, the soft limit stops it, which counts as a failure, and the project is marked failed at 28 seconds. Keep the time limits (see retries, backoff and time limits).
- A worker dies holding all three branches. Nobody reports anything, so there's no errback. Twenty seconds later the status still says TRANSLATING. With a visibility timeout of 15 seconds, the tickets came back at 97 seconds and the job completed at 113. With a two-hour timeout, that wait could be two hours. Let a scheduled sweep catch the rest (see Celery Beat).
Common mistakes
- Passing transcripts or audio bytes between tasks.
- Starting a chord without a result backend.
- Expecting parallel branches on a single slot.
- Trusting Celery's chord error to update your database.
- Thinking the errback hears a killed worker. It only hears reported failures.
Quick recap
| Tool | Does | Needs |
|---|---|---|
chain | Steps one after another, each result into the next | Nothing extra |
group | Steps side by side | Free worker slots |
chord | A group plus one task after all of them | A result backend |
on_error | Writes FAILED when a step fails | Time limits and a sweep for silent failures |
Try it yourself
Set FAIL_STEP to hi, the fastest branch. Predict the time on the marked-failed line, then run it.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.
