---
sidebar:
  label: roboto.action_runtime.invocation_context
  order: 4
title: roboto.action_runtime.invocation_context
---
## Module Contents

### ActionRuntime

```python
class roboto.action_runtime.invocation_context.ActionRuntime(*args, **kwargs)
```

`from roboto import ActionRuntime`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L509-L522)

Bases: [`InvocationContext`](/docs/reference/python-sdk/roboto/action_runtime/invocation_context#roboto.action_runtime.invocation_context.InvocationContext)

Deprecated. Context of a running invocation, under its former name.

Use [`InvocationContext`](/docs/reference/python-sdk/roboto/action_runtime/invocation_context#roboto.action_runtime.invocation_context.InvocationContext) instead, which takes the same arguments. Building one logs a deprecation warning.

### InvocationContext

```python
class roboto.action_runtime.invocation_context.InvocationContext(
    dataset_id: str,
    input_dir: pathlib.Path,
    invocation_id: str,
    org_id: str,
    output_dir: pathlib.Path,
    input_data_manifest_file: Optional[pathlib.Path] = None,
    parameters_file: Optional[pathlib.Path] = None,
    secrets_file: Optional[pathlib.Path] = None,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
    dry_run: bool = False,
    log_level: Optional[str] = None,
)
```

`from roboto import InvocationContext`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L66-L506)

Context of a running invocation, which an action's code uses to read its inputs, parameters, and secrets.

It also fetches the invocation, its dataset, and its organization from Roboto, and sets tags and metadata on the files the action writes to its output directory.

Inside Roboto, build one with `from_env`, which reads the environment variables Roboto sets for an action's code. To run the action's code elsewhere, such as in a unit test, build one directly instead, for example `InvocationContext(dataset_id="ds_12345", input_dir=pathlib.Path("input"), invocation_id="iv_12345", org_id="og_12345", output_dir=pathlib.Path("output"))`.

**Parameters**

- **dataset_id** (`str`): ID of the dataset the invocation was invoked on.
- **input_dir** (`pathlib.Path`): Directory Roboto downloads the invocation's selected files into, when it downloads them.
- **invocation_id** (`str`): ID of the invocation.
- **org_id** (`str`): ID of the organization the invocation belongs to.
- **output_dir** (`pathlib.Path`): Directory for the action's output files.
- **input_data_manifest_file** (`Optional[pathlib.Path]`): Path to a JSON file holding the invocation's input manifest, which `get_input` reads when neither `ROBOTO_ACTION_INPUTS` nor `ROBOTO_ACTION_INPUTS_URL` is set. `None` makes `get_input` raise in that case.
- **parameters_file** (`Optional[pathlib.Path]`): Path to a JSON file of the invocation's parameter values. `None` treats every parameter as unset.
- **secrets_file** (`Optional[pathlib.Path]`): Path to a JSON file of the values of the parameters that name secrets. `None` makes `get_secret_parameter` raise.
- **roboto_client** (`Optional[roboto.http.RobotoClient]`): Client to make requests to Roboto with. Defaults to one configured from the environment, by [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).
- **dry_run** (`bool`): Whether the invocation is a dry run, in which the action should skip side effects.
- **log_level** (`Optional[str]`): Name of the log level requested for the invocation: `ERROR`, `WARNING`, `INFO` or `DEBUG`. `None` requests `INFO`.

**Properties**

- **InvocationContext.dataset** (`roboto.domain.datasets.Dataset`): Dataset the invocation was invoked on, fetched from Roboto on first access and cached.

  Use it to set tags or metadata on the dataset while the action runs, as in `context.dataset.put_tags(["tagged_by_action"])`.

  Accessing it raises [`ActionRuntimeException`](/docs/reference/python-sdk/roboto/action_runtime/exceptions#roboto.action_runtime.exceptions.ActionRuntimeException) when the invocation names no dataset, as when the action runs locally, runs from a scheduled trigger, or is invoked from the CLI with query-based input data. It raises `RobotoNotFoundException` when no dataset has `dataset_id`.

- **InvocationContext.dataset_id** (`str`): ID of the dataset the invocation was invoked on.

  When the invocation names no dataset, it is a placeholder that matches no dataset, and `dataset` raises.

- **InvocationContext.file_changeset_manager** (`roboto.action_runtime.file_changeset.FilesChangesetFileManager`): Manager of the tags and metadata to set on the files the action writes to its output directory.

  Roboto applies them after it uploads the output directory. Name each file by its path relative to `output_dir`, as in `context.file_changeset_manager.put_tags("result.txt", ["tagged_by_action"])`. To tag a file already in Roboto, use the file's own methods instead, as in `context.dataset.get_file_by_path("result.txt").put_tags(["tagged_by_action"])`.

  Built on first access. Accessing it raises `ValueError` when `ROBOTO_FILE_METADATA_CHANGESET_FILE` is unset, as it usually is outside Roboto.

- **InvocationContext.input_dir** (`pathlib.Path`): Directory Roboto downloads the invocation's selected files into, when it downloads them.

- **InvocationContext.invocation** (`roboto.domain.actions.Invocation`): Invocation, fetched from Roboto on first access and cached.

  Accessing it raises `RobotoNotFoundException` when no invocation has `invocation_id`.

- **InvocationContext.invocation_id** (`str`): ID of the invocation.

- **InvocationContext.is_dry_run** (`bool`): Whether the invocation is a dry run, in which the action should skip side effects.

- **InvocationContext.log_level** (`int`): Log level requested for the invocation, as a `logging` constant such as `logging.DEBUG`.

  `logging.INFO` when no level was requested, or when the requested name is not one of `ERROR`, `WARNING`, `INFO` and `DEBUG`, in capitals.

  > **Note**
  >
  > Roboto does not apply the level. Pass it to `logging.getLogger().setLevel()` for it to take effect.

- **InvocationContext.org** (`roboto.domain.orgs.Org`): Organization the invocation belongs to, fetched from Roboto on first access and cached.

  Accessing it raises `RobotoNotFoundException` when no organization has `org_id`.

- **InvocationContext.org_id** (`str`): ID of the organization the invocation belongs to.

- **InvocationContext.output_dir** (`pathlib.Path`): Directory for the action's output files.

  After the action's code exits with code 0, Roboto uploads every file in it to the invocation's upload destination ([`upload_destination`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationRecord.upload_destination)).

- **InvocationContext.roboto_client** (`roboto.http.RobotoClient`): Client this context makes requests to Roboto with.

#### InvocationContext.from_env()

```python
@classmethod
def from_env() -> InvocationContext
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L118-L181)

Build an InvocationContext from the environment variables Roboto sets for an action's code.

It requires `ROBOTO_DATASET_ID`, `ROBOTO_INVOCATION_ID`, `ROBOTO_ORG_ID`, `ROBOTO_INPUT_DIR` and `ROBOTO_OUTPUT_DIR`, and also reads `ROBOTO_ACTION_INPUTS_MANIFEST`, `ROBOTO_ACTION_PARAMETERS_FILE`, `ROBOTO_ACTION_RUNTIME_CONFIG_DIR` (whose `secrets.json` is the secrets file), `ROBOTO_DRY_RUN` and `ROBOTO_LOG_LEVEL`. Roboto sets all the required ones when it runs the action. The client comes from [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).

**Raises**

- [`ActionRuntimeException`](/docs/reference/python-sdk/roboto/action_runtime/exceptions#roboto.action_runtime.exceptions.ActionRuntimeException): A required environment variable is unset, or the input or output directory does not exist.

**Returns**

- `InvocationContext`

**Usage**

```python
from roboto import InvocationContext
context = InvocationContext.from_env()
```

#### InvocationContext.get_input()

```python
def get_input() -> roboto.action_runtime.action_input.ActionInput
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L337-L389)

Return the files, sessions, and topics the invocation was invoked on.

Reads the invocation's input manifest from the first of these that is set:

1. `ROBOTO_ACTION_INPUTS`, which holds the manifest itself.
2. `ROBOTO_ACTION_INPUTS_URL`, from which it downloads the manifest.
3. `input_data_manifest_file`, given when the context was built.

It reads the two environment variables even on a context built directly. When the manifest file is missing or empty, it returns an empty [`ActionInput`](/docs/reference/python-sdk/roboto/action_runtime/action_input/action_input#roboto.action_runtime.action_input.action_input.ActionInput), and logs a warning for a missing one.

**Raises**

- [`ActionRuntimeException`](/docs/reference/python-sdk/roboto/action_runtime/exceptions#roboto.action_runtime.exceptions.ActionRuntimeException): Neither environment variable is set, and the context has no `input_data_manifest_file`.
- `pydantic.ValidationError`: The manifest is not a valid input manifest.

**Returns**

- `roboto.action_runtime.action_input.ActionInput`

#### InvocationContext.get_optional_parameter()

```python
def get_optional_parameter(
    name: str,
    default_value: Optional[str] = None,
) -> Optional[str]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L391-L427)

Return the value of an action parameter, or `default_value` when the invocation did not set it.

A value that is not a string is converted with `str()`. A value that names a secret returns the secret's value from the secrets file instead, as `get_secret_parameter` does. A context with no parameters file returns `default_value` for every parameter.

**Parameters**

- **name** (`str`): Name of the parameter.
- **default_value** (`Optional[str]`): Value to return when the invocation did not set the parameter.

**Raises**

- [`ActionRuntimeException`](/docs/reference/python-sdk/roboto/action_runtime/exceptions#roboto.action_runtime.exceptions.ActionRuntimeException): The parameters file cannot be read or does not hold a JSON object, or the value names a secret that `get_secret_parameter` cannot read.

**Returns**

- `Optional[str]`

**Usage**

Read `model_version`, or `"latest"` when the invocation did not set it:

```python
import roboto
context = roboto.InvocationContext.from_env()
model_version = context.get_optional_parameter("model_version", "latest")
```

#### InvocationContext.get_parameter()

```python
def get_parameter(name: str) -> str
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L429-L447)

Return the value of an action parameter, which the invocation must set.

Reads the value as `get_optional_parameter` does, and its failures also apply.

**Parameters**

- **name** (`str`): Name of the parameter.

**Raises**

- [`ActionRuntimeException`](/docs/reference/python-sdk/roboto/action_runtime/exceptions#roboto.action_runtime.exceptions.ActionRuntimeException): The invocation did not set the parameter, or the context has no parameters file.

**Returns**

- `str`

#### InvocationContext.get_secret_parameter()

```python
def get_secret_parameter(name: str) -> str
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L449-L465)

Return the value of the secret an action parameter names, read from the secrets file.

**Parameters**

- **name** (`str`): Name of the parameter.

**Raises**

- [`ActionRuntimeException`](/docs/reference/python-sdk/roboto/action_runtime/exceptions#roboto.action_runtime.exceptions.ActionRuntimeException): The secrets file holds no value, or an empty one, for the parameter; the context has no secrets file; or the file cannot be read or does not hold a JSON object.

**Returns**

- `str`

### log

```python
roboto.action_runtime.invocation_context.log
```

`from roboto.action_runtime.invocation_context import log`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/action_runtime/invocation_context.py#L32-L32)
