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

# bioptimus.observability.context

Correlation-ID context propagation for the SDK pipeline.

Carries lightweight, thread- and task-local context (a `trace_id` for a whole run, plus `run_id`, `slide_id`, and the current pipeline `stage`) down the execution chain without threading parameters through every function. Structured logging reads this context so every log line is automatically tagged with `run -> slide -> request` correlation fields.

The context lives in a single `contextvars.ContextVar`, so it is isolated per thread and per `asyncio` task. That makes it safe under the SDK's `ThreadPoolExecutor` fan-out (one slide per worker) and its `asyncio` tile dispatch (one task per request).

***

#### new\_trace\_id

```python theme={null}
def new_trace_id() -> str
```

Generates a new, time-sortable correlation ID.

Produces a 26-character ULID-style identifier: a 48-bit millisecond timestamp followed by 80 bits of randomness, encoded with Crockford base32. The leading timestamp makes IDs lexicographically sortable by creation time.

**Returns**:

A 26-character uppercase Crockford base32 string.

***

#### get\_context

```python theme={null}
def get_context() -> dict[str, str]
```

Returns a copy of the currently bound correlation context.

**Returns**:

A mapping of the bound context fields (for example `trace_id`,
`run_id`, `slide_id`, `stage`). Empty when nothing is bound.

***

#### bind\_context

```python theme={null}
@contextmanager
def bind_context(**fields: str | None) -> Iterator[None]
```

Binds correlation fields for the duration of the `with` block.

Merges the given fields onto the current context, yields, then restores the previous context on exit (including on error). Fields whose value is `None` are ignored, which makes optional binding ergonomic.

<ParamField body="**fields">
  Correlation fields to bind, for example `trace_id`, `run_id`, `slide_id`, or `stage`.
</ParamField>

**Returns**:

A context manager that yields `None`; the merged context is active
only inside the `with` block and is restored on exit.
