Sub topic 8
Sharing values between tasks
This chapter covers how one task in a Databricks job passes a small value to the tasks that run after it. It assumes you can write a notebook in Python, know how a multi-task job is wired with depends_on, and have seen job parameters. Task values build on both.
Why task values exist
Tasks in a job run as separate units. Each task has its own notebook session, so a Python variable in the ingest notebook does not exist in the transform notebook. Tasks can share large data through tables or files. For small facts about a run, such as a row count, a batch ID, or a status flag, writing a table is more work than the fact deserves.
Task values solve that. A task publishes a key and a value. A later task in the same run reads it by the publisher's task key. The values live in the run, not in your storage, and they disappear from view when you leave that run.
Task values differ from job parameters:
| Feature | Set by | Scope | Changes during the run |
|---|---|---|---|
| Job parameter | Caller or job definition | Every task in the run | No |
| Task value | A task, at run time | Tasks that depend on the publisher | Yes, set by the publisher |
Use a parameter for input known before the run starts. Use a task value for output that only exists once a task has done its work.
Setting and getting values in Python
The dbutils.jobs.taskValues utility has two calls, set and get.
# In the notebook for task "ingest"
row_count = spark.table("sales.raw_orders").count()
dbutils.jobs.taskValues.set(key="row_count", value=row_count)
# In a notebook for a downstream task
row_count = dbutils.jobs.taskValues.get(
taskKey="ingest", key="row_count", default=0, debugValue=0
)
print(f"Ingest produced {row_count} rows")The arguments to know:
keynames the value. Keys are scoped to the task that sets them, so two tasks can both userow_countwithout clashing.valuemust be JSON serializable. Numbers, strings, booleans, lists, and dictionaries work. A Spark DataFrame or a NumPy array does not.taskKeyongetis thetask_keyof the upstream task, not a notebook name.defaultis returned when the key was never set. It is only used if the upstream task ran without callingset.debugValueis returned when the notebook runs interactively, outside a job. Without it,getraises an error in a notebook you run by hand, because there is no upstream task to ask.
The distinction between default and debugValue matters. A missing key in a real run gives you default. A notebook run outside any job gives you debugValue.
Passing structured values
Because values are JSON, you can pass a small dictionary instead of several separate keys.
import json
summary = {"rows": 1250, "batch_id": "2026-12-04", "late_files": ["a.csv", "b.csv"]}
dbutils.jobs.taskValues.set(key="summary", value=summary)
summary = dbutils.jobs.taskValues.get(
taskKey="ingest", key="summary", default={}, debugValue={"rows": 0}
)
print(json.dumps(summary, indent=2))Keep values small. The platform limits the size of each value to a few kilobytes, and the total for a run is capped as well. If you find yourself serializing a list of thousands of file paths, write them to a table or a file and pass the table name or path instead.
Reading values without code
You do not need a notebook to consume a task value. Job settings accept dynamic value references, written with double curly braces:
{{tasks.ingest.values.row_count}}You can place a reference in a task parameter, in a notebook task's base parameters, or in a condition task operand. The platform resolves it just before the task starts. This is how a non-notebook task such as a Python wheel or a SQL task receives a value, since those task types have no dbutils call to make.
The next job definition uses a reference inside a condition task. It compares the value from ingest to zero, and transform runs only when the outcome is true.
[
{
"task_key": "check_rows",
"depends_on": [{"task_key": "ingest"}],
"condition_task": {
"op": "GREATER_THAN",
"left": "{{tasks.ingest.values.row_count}}",
"right": "0"
}
},
{
"task_key": "transform",
"depends_on": [{"task_key": "check_rows", "outcome": "true"}],
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
}
]Condition tasks are covered in their own chapter. Here, note only that the reference is a string that gets substituted, and that the comparison operands are text that the condition task parses.
How values flow through the graph
A task can only read values from tasks that finish before it. That means the publisher must be an upstream task in the dependency graph. Declare the dependency explicitly. Without depends_on, the two tasks may run in parallel, and the reader may start before the value exists.
The diagram below shows the flow.
A downstream task is not limited to its direct parent. In the chain above, publish can read a value set by ingest because ingest is an ancestor. Reading from a task that is not an ancestor is a design error, even if timing happens to work.
Common errors
These are the problems you are most likely to hit.
- Reading in the wrong place. Calling
getfor a key in a notebook run interactively withoutdebugValuefails. AdddebugValueto everygetso the notebook stays runnable by hand. - Using the notebook name as
taskKey. The argument is the task key from the job definition. If the task key isingestbut the notebook is calledload_orders, useingest. - Non-serializable values. Passing a DataFrame or a
Decimalraises a serialization error. Convert to a plain Python type first, for exampleint(...)orstr(...). - Missing dependency. If
transformdoes not declaredepends_onforingest, the value may not exist whentransformreads it. You then getdefault, which hides the bug. - A skipped publisher. If
ingestis skipped or fails, it never callsset. A task that still runs, for example one with arun_ifrule ofALL_DONE, will then getdefault. Choose defaults that are safe, and avoid defaults that look like real data. - Type surprises in references. A reference such as
{{tasks.ingest.values.row_count}}is substituted as text. A parameter that expects an integer needs to parse it.
Retries, repairs, and values
When a task retries, its notebook runs again from the start. If the notebook calls set again, the new value replaces the old one. After you repair a run, tasks that already succeeded keep the values they set, and the repaired tasks read those saved values. You do not need to rerun ingest to get its row_count back. Chapters on retries and repair runs cover the wider behavior.
A practical pattern
A common use is a status handshake between tasks. The first task publishes both a count and a flag. The second task reads both and decides what to do.
# In the notebook for task "ingest"
df = spark.table("sales.raw_orders")
count = df.count()
dbutils.jobs.taskValues.set(key="row_count", value=count)
dbutils.jobs.taskValues.set(key="has_data", value=count > 0)
# In the notebook for task "transform"
has_data = dbutils.jobs.taskValues.get(
taskKey="ingest", key="has_data", default=None, debugValue=True
)
if has_data is None:
raise RuntimeError("ingest did not publish has_data")
if not has_data:
dbutils.notebook.exit("no data, nothing to transform")The None default turns a missing publisher into a loud failure. The early exit lets the task finish cleanly when there is nothing to do. For a branch that skips whole tasks rather than ending one early, use a condition task as shown earlier.
Summary of rules
- Publish with
dbutils.jobs.taskValues.set, read withgetand the upstreamtask_key. - Values must be JSON serializable and small.
- Always pass
debugValueso notebooks run outside a job. - Declare
depends_onso the publisher finishes first. - Use
{{tasks.<task_key>.values.<key>}}where code is not available. - Pick defaults that cannot be mistaken for real results.
Exercise
A job has tasks ingest and transform, and transform has no depends_on. The transform notebook calls dbutils.jobs.taskValues.get(taskKey="ingest", key="row_count", default=0, debugValue=0). In one run, transform reports 0 rows even though ingest found 500. What is the likely cause, and what are two changes that fix it?
Show answer
Without depends_on, transform ran in parallel with ingest and read before ingest called set, so get returned the default of 0. Add depends_on: [{"task_key": "ingest"}] to transform so it starts only after ingest finishes. Also change the default to None and raise an error when it is None, so a missing value fails loudly instead of looking like zero rows.
Exercise
A Python wheel task needs the batch ID that the ingest task stored under the key batch_id. The wheel task cannot call dbutils. How do you pass the value to it?
Show answer
Put the reference {{tasks.ingest.values.batch_id}} in the wheel task's parameters in the job definition, and make the wheel task depend on ingest. The platform substitutes the value as text before the task starts, so the wheel reads it as an ordinary argument.