---
sidebar:
  hidden: true
title: roboto.domain.actions.invocation
---
## Module Contents

### Invocation

```python
class roboto.domain.actions.invocation.Invocation(
    record: roboto.domain.actions.invocation_record.InvocationRecord,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
)
```

`from roboto import Invocation`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L54-L659)

An invocation: one run of an action, started by a user or by a trigger.

An invocation records the inputs, parameter values, compute requirements, and container parameters it ran with, the history of statuses it entered, and the log lines its containers wrote.

Invocations are started by [`invoke()`](/docs/reference/python-sdk/roboto/domain/actions/action#roboto.domain.actions.action.Action.invoke), by a trigger, or in the Roboto web app. Get an `Invocation` from `from_id` or `query`. Each instance holds a copy of the invocation's record, so its status does not change as the invocation runs: `refresh` fetches the latest record, and `wait_for_terminal_status` polls until the invocation ends.

**Parameters**

- **record** (`roboto.domain.actions.invocation_record.InvocationRecord`)
- **roboto_client** (`Optional[roboto.http.RobotoClient]`)

**Properties**

- **Invocation.action** (`roboto.domain.actions.invocation_record.ActionProvenance`): Action the invocation ran.

  It is [`action`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationProvenance.action): the action's name, the organization that owns it, and its version's digest.

- **Invocation.compute_requirements** (`roboto.domain.actions.action_record.ComputeRequirements`): CPU, memory, and storage the invocation runs with: the action's, with any overrides the invoker gave.

- **Invocation.container_parameters** (`roboto.domain.actions.action_record.ContainerParameters`): Command, entry point, environment variables, and working directory the invocation's container starts with.

  They are the action's, with any overrides the invoker gave.

- **Invocation.created** (`datetime.datetime`): Time the invocation was created, in UTC.

- **Invocation.current_status** (`roboto.domain.actions.invocation_record.InvocationStatus`): Latest status in the invocation's status history.

  Statuses include [`Queued`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Queued), `Processing`, and `Completed`. When two statuses share a timestamp, the one with the higher value counts as the latest.

- **Invocation.data_source** (`roboto.domain.actions.invocation_record.InvocationDataSource`): Dataset the invocation reads from, or an unspecified data source when its invoker named none.

- **Invocation.executable** (`roboto.domain.actions.invocation_record.ExecutableProvenance`): Container image the invocation ran.

- **Invocation.id** (`str`): Unique ID of the invocation, beginning with `iv_`.

- **Invocation.input_data** (`roboto.domain.actions.invocation_record.InvocationInput | None`): Inputs as the invoker requested them, or `None` if it requested none.

  A file selector with `paths` and no `dataset_id` is relative to the invocation's data source. When the data source is a dataset, this returns a copy of such a selector with `dataset_id` set to that dataset's ID, so the selector names its files without the invocation; the record is not changed. When the data source is unspecified, selectors are returned as stored.

  An invocation whose record holds only file paths within its data source, in the deprecated `InvocationRecord.input_data`, returns a file selector for those paths in that dataset.

  Return type: `Optional[roboto.domain.actions.invocation_record.InvocationInput]`

- **Invocation.org_id** (`str`): ID of the organization that owns the invocation.

- **Invocation.parameter_values** (`dict[str, Any]`): Values of the action's parameters for this invocation, keyed by parameter name.

  Parameters the invoker left out have their defaults.

- **Invocation.reached_terminal_status** (`bool`): Whether the current status ends the invocation.

  The statuses that end it are [`Completed`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Completed), `Cancelled`, `Failed`, and `Deadly`.

- **Invocation.record** (`roboto.domain.actions.invocation_record.InvocationRecord`): Record of the invocation that this instance holds.

- **Invocation.resolved_input_data** (`roboto.domain.actions.invocation_record.InvocationInput | None`): Inputs the request resolved to, each named by its file, session, or topic ID.

  `None` if the invocation records none. Where `input_data` can select inputs by path or by query, this names each input by its ID, whatever number of datasets they span. See [`resolved_input_data`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationRecord.resolved_input_data).

  Return type: `Optional[roboto.domain.actions.invocation_record.InvocationInput]`

- **Invocation.source** (`roboto.domain.actions.invocation_record.SourceProvenance`): User or trigger that started the invocation.

- **Invocation.status_log** (`list[roboto.domain.actions.invocation_record.InvocationStatusRecord]`): Statuses the invocation has entered, each with the time it entered it.

- **Invocation.timeout** (`int`): Maximum time, in minutes, the invocation may run.

- **Invocation.upload_destination** (`roboto.domain.actions.invocation_record.InvocationUploadDestination | None`): Where Roboto uploads the files the action writes to its output directory.

  `None` if the invoker named no destination. Without one, Roboto uploads them to the dataset the inputs come from, when they all come from one.

  Return type: `Optional[roboto.domain.actions.invocation_record.InvocationUploadDestination]`

#### Invocation.cancel()

```python
def cancel() -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L384-L404)

Cancel the invocation, unless this instance's copy of its status is already terminal.

Roboto marks the invocation [`Cancelled`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Cancelled) and stops its containers. Cancelling an invocation that has already ended changes nothing. This instance's record is not updated; call `refresh` to see the new status.

**Usage**

Cancel a running invocation:

```python
invocation = Invocation.from_id("iv_12345")
if not invocation.reached_terminal_status:
    invocation.cancel()
```

**Returns**

- `None`

#### Invocation.from_id()

```python
@classmethod
def from_id(
    invocation_id: str,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
) -> Invocation
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L70-L102)

Fetch an invocation by its ID.

Reading an invocation takes permission to read every dataset it was given: its data source, each dataset its input selectors name, the dataset of each file and topic it selects by ID, and each dataset that holds a file of a session it selects by ID. An invocation given no dataset takes only permission to read the invocation. The invocation's own credential, which its action uses, can always read it.

**Parameters**

- **invocation_id** (`str`): ID of the invocation, beginning with `iv_`.
- **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).

**Raises**

- [`RobotoNotFoundException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoNotFoundException): No invocation has this ID.
- [`RobotoUnauthorizedException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoUnauthorizedException): The caller may not read a dataset the invocation was given, such as one restricted to the people it is shared with.

**Returns**

- `Invocation`

**Usage**

Load an invocation and check its status:

```python
invocation = Invocation.from_id("iv_12345")
print(f"Status: {invocation.current_status}")
print(f"Created: {invocation.created}")
```

#### Invocation.get_logs()

```python
def get_logs(
    page_token: Optional[str] = None,
) -> collections.abc.Generator[roboto.domain.actions.invocation_record.LogRecord, None, None]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L406-L437)

Yield the lines the invocation's containers wrote to STDOUT and STDERR, fetching them a page at a time.

The containers are the setup container, the action's own container, and the output handler; each [`LogRecord`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.LogRecord) names the one that wrote it. An invocation that has written no logs yet yields nothing. To follow the logs of an invocation that is still running, use `stream_logs`. Reading the logs takes permission to read every dataset the invocation was given, as `from_id` does.

**Parameters**

- **page_token** (`Optional[str]`): Token from a previous page's response that resumes the listing after it. `None` starts at the first page.

**Returns**

- `collections.abc.Generator[roboto.domain.actions.invocation_record.LogRecord, None, None]`

**Usage**

Print every line the invocation wrote:

```python
invocation = Invocation.from_id("iv_12345")
for record in invocation.get_logs():
    print(record.process, record.log)
```

#### Invocation.is_queued_for_scheduling()

```python
def is_queued_for_scheduling() -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L439-L448)

Return whether the invocation is waiting for Roboto to start it.

True when the current status is [`Queued`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Queued) and the status history holds no `Deadly` entry.

**Returns**

- `bool`

#### Invocation.query()

```python
@classmethod
def query(
    spec: Optional[roboto.query.QuerySpecification] = None,
    owner_org_id: Optional[str] = None,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
) -> collections.abc.Generator[Invocation, None, None]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L105-L219)

Yield the invocations in an organization that match a query, fetching them a page at a time.

Conditions and sorts name fields of [`InvocationRecord`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationRecord), such as `last_status` or `created`, or one of `data_source_id`, `data_source_type`, and `input.session_id`.

**Parameters**

- **spec** (`Optional[roboto.query.QuerySpecification]`): Conditions and sort order of the query. `None` matches every invocation in the organization.
- **owner_org_id** (`Optional[str]`): Organization to query invocations in. Required when the caller belongs to more than one organization; otherwise the caller's only organization.
- **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).

**Raises**

- `ValueError`: `spec` names a field that is neither a field of `InvocationRecord` nor one of `data_source_id`, `data_source_type`, and `input.session_id`. Of a dotted field such as `provenance.action`, only the part before the first dot is checked. It surfaces on the first iteration.
- [`RobotoIllegalArgumentException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoIllegalArgumentException): The query filters or sorts on a field Roboto does not accept for invocations, filters `last_status` on a value that is neither a whole number nor a status name, or compares `last_status` with [`Like`](/docs/reference/python-sdk/roboto/query/conditions#roboto.query.conditions.Comparator.Like), `NotLike`, `Contains`, `NotContains` or `BeginsWith`. It surfaces on the first iteration.

**Returns**

- `collections.abc.Generator[Invocation, None, None]`

**Usage**

Query all invocations:

```python
for invocation in Invocation.query():
    print(f"Invocation: {invocation.id}")
```

Query invocations whose data source is a given dataset:

```python
from roboto.query import Comparator, Condition, QuerySpecification
spec = QuerySpecification(
    condition=Condition(
        field="data_source_id",
        comparator=Comparator.Equals,
        value="ds_12345",
    )
)
for invocation in Invocation.query(spec):
    print(invocation.id)
```

Query invocations whose input names a session by ID:

```python
spec = QuerySpecification(
    condition=Condition(
        field="input.session_id",
        comparator=Comparator.Equals,
        value="se_12345",
    )
)
on_session = list(Invocation.query(spec))
```

Query completed invocations. `last_status` also accepts the status name in any case, so `value="completed"` finds the same invocations. Greater-than and less-than comparators compare status numbers, so [`Cancelled`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Cancelled) (997), `Failed` (998) and `Deadly` (999) count as greater than `Completed` (5):

```python
from roboto.domain.actions import InvocationStatus
spec = QuerySpecification(
    condition=Condition(
        field="last_status",
        comparator=Comparator.Equals,
        value=InvocationStatus.Completed.value,
    )
)
completed = list(Invocation.query(spec))
```

Query the ten most recent invocations. Neither `limit` nor `max_results` caps an invocation query, so take the first ten from the generator:

```python
import itertools
from roboto.query import SortDirection
spec = QuerySpecification(sort_by="created", sort_direction=SortDirection.Descending)
recent = list(itertools.islice(Invocation.query(spec), 10))
```

#### Invocation.refresh()

```python
def refresh() -> Invocation
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L450-L463)

Replace this instance's record with the invocation's current record from Roboto.

Use it to see the status changes and other updates made since this instance was loaded.

**Returns**

- `Invocation`: This instance, with its record updated from Roboto's response.

**Usage**

```python
invocation = Invocation.from_id("iv_12345")
print(invocation.refresh().current_status)
```

#### Invocation.set_container_image_digest()

```python
def set_container_image_digest(digest: str) -> Invocation
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L465-L488)

Record the digest of the container image Roboto pulled to run the invocation.

Roboto calls this when it has pulled the invocation's container image. It refuses the request unless it comes from a Roboto administrator or with the credentials Roboto gives the running invocation.

**Parameters**

- **digest** (`str`): Digest of the pulled container image.

**Returns**

- `Invocation`: This instance, with its record updated from Roboto's response.

#### Invocation.set_logs_location()

```python
def set_logs_location(
    logs: roboto.domain.actions.invocation_record.LogsLocation,
) -> Invocation
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L490-L510)

Record the S3 location where Roboto stores the invocation's log files.

Roboto calls this when it schedules the invocation. Only Roboto administrators may call it. To read the logs, use `get_logs` or `stream_logs`.

**Parameters**

- **logs** (`roboto.domain.actions.invocation_record.LogsLocation`): Bucket and key prefix of the log files.

**Returns**

- `Invocation`: This instance, with its record updated from Roboto's response.

#### Invocation.stream_logs()

```python
def stream_logs(
    last_read: Optional[str] = None,
) -> collections.abc.Generator[roboto.domain.actions.invocation_record.LogRecord, None, Optional[str]]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L512-L564)

Yield the log lines written after `last_read`, then return a token that resumes after the last of them.

Use it to follow the logs of an invocation that is still running. The generator's return value, which is the `value` of the `StopIteration` it raises when exhausted, is the token to pass as `last_read` on the next call, so that call yields only lines written since. It is `None` when the invocation has no logs to read. Reading the logs takes permission to read every dataset the invocation was given, as `from_id` does.

**Parameters**

- **last_read** (`Optional[str]`): Token a previous call returned. `None` starts at the first line.

**Returns**

- `collections.abc.Generator[roboto.domain.actions.invocation_record.LogRecord, None, Optional[str]]`

**Usage**

Print the invocation's logs as it writes them, until it ends:

```python
import time
invocation = Invocation.from_id("iv_12345")
last_read = None
while True:
    ended = invocation.refresh().reached_terminal_status
    logs = invocation.stream_logs(last_read)
    try:
        while True:
            print(next(logs).log)
    except StopIteration as stop:
        last_read = stop.value
    if ended:
        break
    time.sleep(2)
```

#### Invocation.to_dict()

```python
def to_dict() -> dict[str, Any]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L566-L577)

Return the record this instance holds as a JSON-compatible dictionary.

**Returns**

- `dict[str, Any]`: [`InvocationRecord`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationRecord) of the invocation, as a JSON-compatible dictionary.

**Usage**

```python
invocation = Invocation.from_id("iv_12345")
invocation.to_dict()["invocation_id"]
# 'iv_12345'
```

#### Invocation.update_status()

```python
def update_status(
    next_status: roboto.domain.actions.invocation_record.InvocationStatus,
    detail: Optional[str] = None,
) -> Invocation
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L579-L621)

Record that the invocation entered `next_status`.

Roboto calls this when the invocation moves to a new status. It refuses the request unless it comes from a Roboto administrator or with the credentials Roboto gives the running invocation, and it leaves the status unchanged when the invocation cannot move to `next_status` from its current status (see [`can_transition_to()`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.can_transition_to)).

A third failure is recorded as [`Deadly`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Deadly), which is not retried: when `next_status` is `Failed` and this instance's status history already holds two `Failed` entries, `Deadly` is sent instead.

**Parameters**

- **next_status** (`roboto.domain.actions.invocation_record.InvocationStatus`): Status the invocation entered.
- **detail** (`Optional[str]`): Human-readable explanation of the status, such as why the invocation failed. `None` or an empty string records the text `"None"`.

**Returns**

- `Invocation`: This instance, with its record updated from Roboto's response.

#### Invocation.wait_for_terminal_status()

```python
def wait_for_terminal_status(
    timeout: float = 60 * 5,
    poll_interval: roboto.waiters.Interval = 5,
) -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/actions/invocation.py#L623-L659)

Wait until the invocation's status is terminal, refreshing this instance's record as it polls.

When this returns, `current_status` is [`Completed`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationStatus.Completed), `Cancelled`, `Failed`, or `Deadly`.

**Parameters**

- **timeout** (`float`): Maximum time to wait, in seconds.
- **poll_interval** (`roboto.waiters.Interval`): Seconds to wait between polls, or a function that takes the number of polls made so far and returns the seconds to wait before the next.

**Raises**

- [`roboto.waiters.TimeoutError`](/docs/reference/python-sdk/roboto/waiters#roboto.waiters.TimeoutError): The invocation did not end within `timeout`. It subclasses the built-in `TimeoutError`.

**Returns**

- `None`

**Usage**

Wait up to ten minutes for an invocation to end:

```python
invocation = Invocation.from_id("iv_12345")
invocation.wait_for_terminal_status(timeout=600)
print(invocation.current_status)
```
