> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/gym/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/gym/_mcp/server.

# nemo_gym.rollout_correlation

## Module Contents

### Classes

| Name                                                                                 | Description                                                                    |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| [`RolloutContextMiddleware`](#nemo_gym-rollout_correlation-RolloutContextMiddleware) | Strip a rollout prefix and expose it to downstream Gym calls for this request. |

### Functions

| Name                                                                                             | Description                              |
| ------------------------------------------------------------------------------------------------ | ---------------------------------------- |
| [`current_rollout_id`](#nemo_gym-rollout_correlation-current_rollout_id)                         | -                                        |
| [`maybe_rollout_id_from_run_body`](#nemo_gym-rollout_correlation-maybe_rollout_id_from_run_body) | Build the capture key for a run request. |
| [`rollout_context`](#nemo_gym-rollout_correlation-rollout_context)                               | -                                        |

### Data

[`ROLLOUT_ID_PATTERN`](#nemo_gym-rollout_correlation-ROLLOUT_ID_PATTERN)

[`_ROLLOUT_ID`](#nemo_gym-rollout_correlation-_ROLLOUT_ID)

### API

```python
class nemo_gym.rollout_correlation.RolloutContextMiddleware(
    app: typing.Any
)
```

Strip a rollout prefix and expose it to downstream Gym calls for this request.

**`_PREFIX`**

---

```python
nemo_gym.rollout_correlation.RolloutContextMiddleware.__call__(
    scope: dict[str, typing.Any],
    receive: typing.Any,
    send: typing.Any
) -> None
```

async

```python
nemo_gym.rollout_correlation.current_rollout_id() -> typing.Optional[str]
```

```python
nemo_gym.rollout_correlation.maybe_rollout_id_from_run_body(
    body: pydantic.BaseModel | collections.abc.Mapping[str, typing.Any] | None
) -> typing.Optional[str]
```

Build the capture key for a run request.

An explicit `_ng_rollout_id` takes precedence.
Otherwise derive `"&#123;task&#125;-&#123;rollout&#125;"` from the task and rollout indices.
Re-dispatch attempts append `-a&#123;n&#125;`.
Writers and consumers must use this same identity.
Reused task and rollout indices produce a repeated capture key.
Use an explicit id when numbering restarts across dispatches.

```python
nemo_gym.rollout_correlation.rollout_context(
    rollout_id: typing.Optional[str]
) -> collections.abc.Iterator[None]
```

```python
nemo_gym.rollout_correlation.ROLLOUT_ID_PATTERN = re.compile('^[A-Za-z0-9][A-Za-z0-9._-]*$')
```

```python
nemo_gym.rollout_correlation._ROLLOUT_ID: ContextVar[Optional[str]] = ContextVar('nemo_gym_rollout_id', default=None)
```