> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cognee.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust SDK PipelineContext

> PipelineContext in cognee-rust: the run identity and current data item every task can read through its TaskContext.

## What is PipelineContext

`PipelineContext` tells a running task which pipeline run it belongs to, who started it, which dataset it works on and which data item is being processed. Tasks use it to attribute what they write, for example to stamp provenance on DataPoints.

## How tasks reach it

Every task runs with a `TaskContext` ([`cognee-core`](https://github.com/topoteretes/cognee-rs/blob/main/crates/core/), `task_context.rs`). It carries the shared resources of the run:

| Field | What it holds |
| - | - |
| `database`, `graph_db`, `vector_db` | The relational, graph and vector stores |
| `thread_pool` | The executor for CPU-bound work |
| `cancellation`, `progress` | Tokens the task checks for cancellation and uses to report progress |
| `exec_status` | The per-item status tracker used for deduplication and resume |
| `pipeline_watcher` | An optional watcher that receives the fields a task publishes with `publish_payload_field`. Lifecycle events go only to the watcher passed to `execute` |
| `pipeline_ctx` | The `PipelineContext`, when the task runs inside a pipeline executor |

`TaskContext::pipeline()` returns the `PipelineContext`. It panics when none is set, so call it only from tasks that always run inside a pipeline executor; otherwise read the `pipeline_ctx` option.

A Rust task always receives the `TaskContext` and reads what it needs from it.

## Fields

| Field | Type | What it holds |
| - | - | - |
| `pipeline_id` | `Uuid` | The pipeline's id |
| `pipeline_name` | `String` | The pipeline's name |
| `user_id` | `Option<Uuid>` | The user running the pipeline |
| `tenant_id` | `Option<Uuid>` | The run's tenant. `None` in single-user deployments, where telemetry reports `"Single User Tenant"` |
| `dataset_id` | `Option<Uuid>` | The dataset being processed |
| `current_data` | `Option<Arc<dyn Value>>` | The data item being processed (see below) |
| `run_id` | `Option<Uuid>` | A random id for this run, set by the executor. `None` outside `execute()` |
| `user_email` | `Option<String>` | The user's email, when known |
| `provenance_visited` | `Arc<Mutex<HashSet<Uuid>>>` | The DataPoints already stamped in this run, shared by every task |

`user_label()` gives the value used as a DataPoint's `source_user`: the email when set, else the user id, else nothing.

### current\_data

`current_data` is the **original** value that entered the pipeline for this data item, not the previous task's output. The executor sets it once per item, and every task in the chain sees the same value, batch tasks included.

Before cognee-rs [#120](https://github.com/topoteretes/cognee-rs/pull/120) (August 2026), `current_data` held the previous task's output, and batch tasks saw `None`. A custom task written against that behaviour must use its input argument instead.

Because the value stays reachable until the item's whole chain finishes, a pipeline whose input holds a large buffer (such as `DataInput::Binary`) keeps it in memory longer. With `Pipeline::with_concurrency` above 1, more items overlap. Stream large payloads instead of putting them in the input value.

### Provenance stamping

`provenance_visited` makes sure a DataPoint that passes through several tasks is stamped once, with the first task's name.

## Setting the context

The caller builds the `PipelineContext` and puts it on the `TaskContext` with `TaskContextBuilder::pipeline_context`. Its fields are public and it has no constructor, so you write all of them:

```rust theme={null}
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use cognee_core::{PipelineContext, TaskContextBuilder};

let pipeline_ctx = PipelineContext {
    pipeline_id: pipeline.id,
    pipeline_name: pipeline.name.clone().unwrap_or_default(),
    user_id: Some(user_id),
    tenant_id: None,
    dataset_id: Some(dataset_id),
    current_data: None,
    run_id: None,
    user_email: None,
    provenance_visited: Arc::new(Mutex::new(HashSet::new())),
};

let (_cancel, ctx) = TaskContextBuilder::new()
    .thread_pool(thread_pool)
    .database(database)
    .graph_db(graph_db)
    .vector_db(vector_db)
    .pipeline_context(pipeline_ctx)
    .build()?;
```

Leave `current_data` and `run_id` unset: `execute` fills them in. At the start of each run it also clears `provenance_visited`, so a reused context does not carry stamps from an earlier run. The user and dataset ids decide the run's attribution: `execute` derives the run's `pipeline_id` from the user id, the pipeline name and the dataset id, and runs in the dataset's scope when a dataset is set. See [How pipelines run](https://docs.cognee.ai/rust/core-concepts/building-blocks/pipelines.md#how-pipelines-run).

## Custom pipeline state

`PipelineContext` has no free-form map for your own values. A task is a `Send + Sync + 'static` closure, so it takes its settings by capturing them:

```rust theme={null}
use cognee_core::Task;

let multiplier = 10_i64;
let score = Task::sync_typed(move |n: &i64, _ctx| Ok(Box::new(n * multiplier)));
```

To pass values *out* of a run, a task calls `TaskContext::publish_payload_field(key, value)` with a JSON value:

```rust theme={null}
ctx.publish_payload_field("pages", serde_json::json!(12)).await;
```

The field goes to the watcher set on the `TaskContext` (`TaskContextBuilder::pipeline_watcher`), not to the one passed to `execute`, and the call does nothing when that watcher is unset or the task runs outside `execute`. `DbPipelineWatcher` stores each field in the `pipeline_run_payload_fields` table, one row per run and key, where a later value for the same key replaces the earlier one. `PipelineRunRepository::get_payload(run_id)` reads them back as a JSON map.

## Examples and details

> [accordion] Accessing user and dataset in a task
>
> Read the identity fields when a task writes its own records or applies per-user logic:
>
> ```rust theme={null}
> use std::sync::Arc;
> use cognee_core::{Task, TaskContext};
>
> let tag = Task::async_fn_typed(|text: &String, ctx: Arc<TaskContext>| {
>     let text = text.clone();
>     Box::pin(async move {
>         let run = ctx.pipeline();
>         let owner = run.user_label().unwrap_or_default();
>         let dataset = run.dataset_id.map(|id| id.to_string()).unwrap_or_default();
>         Ok(Box::new(format!("{owner}/{dataset}: {text}")))
>     })
> });
> ```
>
> The stores on the `TaskContext` already point at the run's dataset: when `dataset_id` is set, `execute` runs every task in that dataset's scope, so under per-dataset storage `graph_db` and `vector_db` reach the dataset's own databases.

> [accordion] State persists across chained tasks
>
> Every task of a run sees the same `PipelineContext` values: the same user, dataset, run id and `provenance_visited` set. Only `current_data` changes, once per data item. Tasks receive the context behind an `Arc` and cannot change it, so data flows from task to task through task outputs. For settings several tasks share, capture clones of one `Arc`:
>
> ```rust theme={null}
> struct Settings { min: i64, prefix: String }
> let settings = Arc::new(Settings { min: 4, prefix: "item_".into() });
>
> let s = Arc::clone(&settings);
> let clamp = Task::sync_typed(move |n: &i64, _ctx| Ok(Box::new((*n).max(s.min))));
> let s = Arc::clone(&settings);
> let label = Task::sync_typed(move |n: &i64, _ctx| Ok(Box::new(format!("{}{n}", s.prefix))));
> ```

> [accordion] Making the context optional
>
> `TaskContext::pipeline()` panics without a `PipelineContext`. A task that may also run outside a pipeline executor, in a test or called directly, reads the option instead:
>
> ```rust theme={null}
> let name = ctx
>     .pipeline_ctx
>     .as_ref()
>     .map(|p| p.pipeline_name.as_str())
>     .unwrap_or("standalone");
> ```

## Related

> [card] [Tasks](https://docs.cognee.ai/rust/core-concepts/building-blocks/tasks.md)
>
> The units of work that read the context

> [card] [Pipelines](https://docs.cognee.ai/rust/core-concepts/building-blocks/pipelines.md)
>
> The runs the context describes

> [card] [DataPoints](https://docs.cognee.ai/rust/core-concepts/building-blocks/datapoints.md)
>
> Where provenance is stamped


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.