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

# Roleplay Actions

> Have your avatar perform a custom action from a prompt or a video clip.

## Overview

Roleplay actions let your avatar break out of standard talking gestures to perform a specific action mid-call—like celebrating, dancing, or sipping a drink—before seamlessly resuming the conversation. Actions are triggered either from a text prompt or from a pre-registered video clip.

There are two ways to ask for one:

* **On demand**: a text description of what the avatar should do. We generate the action from the avatar's current look.
* **Registered**: a video clip you register when the session starts, then trigger by name.

| | On demand | Registered |
| - | - | - |
| **Best for** | Open-ended actions where the user can ask the avatar to do anything in real time. | A specific, curated action. Great when your UI offers a set of action buttons for the user to pick from. |
| **Latency** | Slower: takes a few seconds to generate before the action can start. | Starts within a few hundred milliseconds. |
| **Predictability** | Generated from the prompt, so results are more dynamic and less predictable. | Exact: always matches your video clip. |
| **Setup** | None: prompts are sent on the fly during the call. | You create the clips ahead of time and register them at session start. See [clip requirements](#clip-requirements). |

> [!WARNING]
>
> Roleplay actions are available on LiveKit only.

## On-demand actions

Publish a data message on the LiveKit topic `ls.roleplay-action` with a `prompt`.

> [!NOTE]
>
> Only one roleplay action runs at a time. Any roleplay action you send while one is in progress is ignored until that action is over.

```json theme={null}
{
  "prompt": "The person picks up a cup of coffee, sips it, and puts the cup back down",
  "duration": 5,
}
```

<table>
  <colgroup>
    <col />

    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Field</th>
      <th>Required</th>
      <th>Default</th>
      <th>What it does</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`prompt`</td>
      <td>yes</td>
      <td>—</td>
      <td>What the avatar should do, up to 1000 characters. The action is generated from the avatar's current look.</td>
    </tr>

    <tr>
      <td>`duration`</td>
      <td>no</td>
      <td>`5`</td>
      <td>Length in seconds, between 1.5 and 15.</td>
    </tr>

    <tr>
      <td>`generate_audio`</td>
      <td>no</td>
      <td>`false`</td>
      <td>`true` generates a soundtrack for the action. `false` makes it silent.</td>
    </tr>

    <tr>
      <td>`end_frame`</td>
      <td>no</td>
      <td>None</td>
      <td>An `https://` URL, or a base64 image, of the look the avatar should end on. That image becomes the avatar's look after the action. Without it, the avatar returns to the look it had when you sent the prompt. Prefer a URL; inline images must stay small enough to fit in a data message. See [changing the avatar's look](#changing-the-avatars-look).</td>
    </tr>
  </tbody>
</table>

An on-demand action takes a few seconds to generate. If the avatar is talking when it is ready, it waits until the avatar finishes, then starts.

> [!TIP]
>
> When you send the request, have the avatar acknowledge it and keep talking while the action generates, for example *"Sure, I can dance for you. I have been taking dancing lessons for this exact purpose."* After the action plays, have the avatar speak again and refer to what it just did, for example *"I hope you liked my dance!"* You can queue the response audio while the action is playing.

```javascript JavaScript theme={null}
const payload = new TextEncoder().encode(
  JSON.stringify({
    prompt: "The person picks up a cup of coffee, sips it, and puts the cup back down",
    duration: 5,
  })
);
await room.localParticipant.publishData(payload, {
  topic: "ls.roleplay-action",
  reliable: true,
});
```

```python Python theme={null}
import json
from livekit import rtc

async def request_roleplay_action(room: rtc.Room, prompt: str, duration: float = 5) -> None:
    await room.local_participant.publish_data(
        json.dumps({"prompt": prompt, "duration": duration}).encode(),
        topic="ls.roleplay-action",
        reliable=True,
    )
```

## Registered actions

### Register clips at session start

Pass your video clips as `roleplay_actions` when creating the `AvatarSession`. You can register up to 100 actions. Each key is the name you will use to trigger the action, and each value is an object with `video_url` and an optional `new_image_url`.

```python theme={null}
avatar = lemonslice.AvatarSession(
    agent_image_url="https://example.com/avatar.jpg",
    roleplay_actions={
        "thinking": {
            "video_url": "https://example.com/clips/thinking.mp4",
        },
        "to_work": {
            "video_url": "https://example.com/clips/to-work.mp4",
            "new_image_url": "https://example.com/images/working.jpg",
        },
    },
)
session_id = await avatar.start(session, room=ctx.room)
```

<table>
  <colgroup>
    <col />

    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Field</th>
      <th>Required</th>
      <th>Default</th>
      <th>What it does</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>key</td>
      <td>yes</td>
      <td>—</td>
      <td>The action name. Lowercase letters, digits, `_` and `-`, up to 64 characters. You use this name to trigger the action.</td>
    </tr>

    <tr>
      <td>`video_url`</td>
      <td>yes</td>
      <td>—</td>
      <td>An `https://` URL to the video. The video must be 30 seconds or less.</td>
    </tr>

    <tr>
      <td>`new_image_url`</td>
      <td>no</td>
      <td>None</td>
      <td>An `https://` URL to the image the avatar should *become* after the action. See [changing the avatar's look](#changing-the-avatars-look).</td>
    </tr>
  </tbody>
</table>

### Clip requirements

Generate each clip before the session starts. It should show the same character as the avatar's image, performing the action.

* **Length:** 30 seconds or less. Longer clips are skipped.
* **Dimensions:** a 2:3 portrait of 416 × 672 pixels or 756 × 1220 pixels.
* **Format:** MP4 (H.264) is the safest choice.
* **Audio:** optional. If the clip has audio, it plays with the action. Without audio, the action is silent.
* **Start:** the first frame should match the avatar's current reference image.
* **End:** the last frame should match the current reference image, or the new image set by `new_image_url`.
* **Size:** up to 256 MB per clip.

### Trigger a registered action

Publish a data message on the LiveKit topic `ls.roleplay-action` with the action name.

> [!NOTE]
>
> Only one roleplay action runs at a time. Any roleplay action you send while one is in progress is ignored until that action is over.

```json theme={null}
{ "action": "thinking", "interrupt": false }
```

<table>
  <colgroup>
    <col />

    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Field</th>
      <th>Required</th>
      <th>Default</th>
      <th>What it does</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`action`</td>
      <td>yes</td>
      <td>—</td>
      <td>A name from `roleplay_actions`.</td>
    </tr>

    <tr>
      <td>`interrupt`</td>
      <td>no</td>
      <td>`false`</td>
      <td>`false` waits until the avatar finishes what it is saying, then performs the action. `true` interrupts the avatar and performs it now.</td>
    </tr>
  </tbody>
</table>

```javascript JavaScript theme={null}
const payload = new TextEncoder().encode(
  JSON.stringify({ action: "thinking", interrupt: false })
);
await room.localParticipant.publishData(payload, {
  topic: "ls.roleplay-action",
  reliable: true,
});
```

```python Python theme={null}
import json
from livekit import rtc

async def trigger_roleplay_action(room: rtc.Room, action: str, interrupt: bool = False) -> None:
    await room.local_participant.publish_data(
        json.dumps({"action": action, "interrupt": interrupt}).encode(),
        topic="ls.roleplay-action",
        reliable=True,
    )
```

## Events

LemonSlice publishes `roleplay_status` events as JSON data messages on the LiveKit topic `lemonslice`. Use them to follow an action from request to finish.

```json theme={null}
{ "type": "roleplay_status", "status": "started", "action": "thinking" }
```

| `status` | When | Applies to | Extra fields |
| - | - | - | - |
| `generating` | Generation has started. | On demand | `prompt`: the text you sent. |
| `ready` | The action is generated and waiting to start. | On demand | `prompt`: the text you sent. `action`: an id for this action, such as `prompt_a1b2c3d4`. Later events for this action use the same id. |
| `started` | The action is accepted. It has started, or it is waiting for the avatar to finish talking. | Both | `action`: the name you registered, or the on-demand id from `ready`. |
| `failed` | The action will not play: it could not be generated, could not start, or another action is in progress. | Both | `error`: why it will not play. `action`: the registered name, or the on-demand id once one exists. |
| `done` | The action has finished. The avatar is back in conversation, and you can send the next roleplay action. | Both | `action`: the name you registered, or the on-demand id from `ready`. |

## Avatar Lifecycle during a Roleplay Action

1. **Accepted.** If the avatar is talking, the action waits until it finishes what it's saying, then starts. For a registered action, set `interrupt: true` to skip the wait: the avatar stops talking and performs the action right away.
2. **Performed.** The avatar performs the action. If your agent responds during the action, the response is held until the action ends. Only the latest response is kept.
3. **Done.** When the action ends, the avatar speaks the held response, if there is one, and you can send the next roleplay action.

## Changing the avatar's look

A roleplay action can also change the avatar's look, like walking into the garden or putting on a hat. Give us an image of the new look, and the avatar keeps that look after the action ends.

* **On demand:** set `end_frame` to the new image. The action is generated to end on it.
* **Registered:** set `new_image_url` to the new image, next to `video_url`. Your clip should end on that image.

If you leave these out, the avatar goes back to the look it had before the action.

### Example: from the house to the garden

Say your avatar starts in a living room, and you want it to walk outside and keep talking from the garden. You need an image of the avatar standing in the garden, such as `garden.jpg`.

On demand, describe the move and end on the garden image:

```json theme={null}
{
  "prompt": "The person stands up, walks out the back door, and stops in the garden facing the camera",
  "duration": 10,
  "end_frame": "https://example.com/images/garden.jpg"
}
```

Registered, use a clip of the avatar walking from the living room into the garden, and set the garden image as `new_image_url`:

```json theme={null}
{
  "roleplay_actions": {
    "to_garden": {
      "video_url": "https://example.com/clips/house-to-garden.mp4",
      "new_image_url": "https://example.com/images/garden.jpg"
    }
  }
}
```

Then trigger it with `{ "action": "to_garden" }`. Either way, once the action ends, the avatar keeps talking from the garden for the rest of the session, until another action changes its look.


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