Today, we’re releasing the Python SDK for Liveblocks, bringing powerful back end
capabilities to one of the most popular modern languages. This is especially
important as Python continues to dominate the AI ecosystem, making it a natural
choice for building intelligent, collaborative applications and
[agent-driven software](/blog/ai-agents-are-becoming-native-users-of-software).

<InstallCommand
  options={[
    { label: "pip", command: "pip install liveblocks" },
    { label: "poetry", command: "poetry add liveblocks" },
    { label: "uv", command: "uv add liveblocks" },
  ]}
/>

## Python SDK

Our Python SDK introduces 85+ new methods for Liveblocks, allowing you to manage
rooms, update realtime storage, and
[build agent workflows](/blog/introducing-feeds-and-apis-for-agent-workflows)
directly in Python. Let’s look at how it can be used.

### Create a room

[Rooms](/docs/concepts#Rooms) are the foundation of Liveblocks—they’re
multiplayer spaces where people and agents can collaborate. In Python, you can
use [`create_room`](/docs/api-reference/liveblocks-python#create_room) to set
them up, and define permissions.

```python title="Create a room"
from liveblocks.models import CreateRoomRequestBody

result = client.create_room(
    body=CreateRoomRequestBody(
        id="my-room-id",
        default_accesses=[],
    ),
)
```

In your React front end, you can join this room using
[`RoomProvider`](/docs/api-reference/liveblocks-react#RoomProvider).

```tsx title="Join a room"
import { RoomProvider } from "@liveblocks/react";

export function Room({ children }) {
  return <RoomProvider id="my-room-id">{children}</RoomProvider>;
}
```

### Modify realtime storage

Your multiplayer room may use
[Liveblocks Storage](/docs/collaboration-features/multiplayer/sync-engine/liveblocks-storage)
to store realtime data, for example shapes on a collaborative canvas. You can
use
[`patch_storage_document`](/docs/api-reference/liveblocks-python#patch_storage_document)
to modify this data in realtime.

```python title="Use JSON Patch to modify realtime storage"
patch = [
    {
        "op": "add",
        "path": "/shapes/-",
        "value": {
            "id": "rect-1",
            "type": "rectangle",
            "x": 100,
            "y": 150,
            "width": 200,
            "height": 120,
        },
    }
]

result = client.patch_storage_document(
    room_id="my-room-id",
    body=patch,
)
```

Changes are displayed in realtime for all connected clients with
[`useStorage`](/docs/api-reference/liveblocks-react#useStorage).

```tsx title="Display realtime storage"
export function Canvas() {
  const shapes = useStorage((root) => root.shapes);

  // [..., { id: "rect-1", type: "rectangle", x: 100, y: 150, ... }]
  console.log(shapes);

  return (
    <div>
      {shapes.map((shape) => (
        <Shape key={shape.id} shape={shape} />
      ))}
    </div>
  );
}
```

### Use AI to modify realtime storage

By using
[`get_storage_document`](/docs/api-reference/liveblocks-python#get_storage_document),
you can get the current realtime document as JSON. In combination with
[`patch_storage_document`](/docs/api-reference/liveblocks-python#patch_storage_document),
you can then use this to generate a patch with AI.

```python title="Get and modify realtime storage with AI"
user_prompt = "Add a rectangle near the center of the canvas"

storage = client.get_storage_document(
    room_id="my-room-id",
    format_="json",
)

patch = generate_patch_with_ai(
    prompt=user_prompt,
    context=storage,
    instructions="""
    You are given the current storage document as JSON.
    Return a valid JSON patch array that updates the document.
    Only use operations like 'add', 'replace', or 'remove'.
    """
)

result = client.patch_storage_document(
    room_id="my-room-id",
    body=patch,
)
```

### Show AI presence in a document

By using [`setPresence`](/docs/api-reference/liveblocks-python#set_presence),
you can show AI presence in a document while it’s making changes.

```python title="Set presence inside a room"
from liveblocks.models import SetPresenceRequestBody

client.set_presence(
    room_id="my-room-id",
    body=SetPresenceRequestBody(
        user_id="ai-agent-123",
        user_info={
            "name": "AI agent",
            "avatar": "https://example.com/avatar.png",
        },
        data={
          "is_typing": True,
        },
    ),
)
```

In your front end, the AI’s avatar will appear in
[`AvatarStack`](/docs/api-reference/liveblocks-react-ui#AvatarStack).

```tsx title="AI avatar appears in the stack"
import { AvatarStack } from "@liveblocks/react-ui";

export function Header() {
  return <AvatarStack size="48px" />;
}
```

You can also build fully custom presence UI with
[`useOthers`](/docs/api-reference/liveblocks-react#useOthers).

### Display realtime AI workflow status

[Feeds](/docs/collaboration-features/ai-collaboration#Overview) is a new
primitive for storing realtime chat messages, AI activity logs, and more. You
can use
[`create_feed_message`](/docs/api-reference/liveblocks-python#create_feed_message)
to create a realtime message in a feed, for example to display the status of an
AI workflow, currently `Thinking…`.

```python title="Create a message in a feed"
from liveblocks.models import CreateFeedMessageRequestBody

result = client.create_feed_message(
    room_id="my-room-id",
    feed_id="my-feed-id",
    body=CreateFeedMessageRequestBody(
        data={
          "status": "Thinking…",
        },
    ),
)
print(result)
```

In your front end, you can show this message in your UI as soon as it’s created.

```tsx title="Show realtime AI workflow status"
import { useFeedMessages } from "@liveblocks/react/suspense";

export function AgentStatus() {
  const { messages } = useFeedMessages("my-feed-id");
  const latestMessage = messages[messages.length - 1];

  return <div>{latestMessage.data.status}</div>;
}
```

Sending new feed messages will update this UI in realtime.

### Lots more APIs

We’ve only briefly touched upon uses for our new Python SDK. There are around 80
more methods to explore, allowing you to:

- Authenticate users, manage permissions, create share menus.
- List rooms, fetch connected users, send realtime events.
- [Multiplayer](/docs/collaboration-features/multiplayer): Fetch and modify Yjs
  text document data.
- [Comments](/docs/collaboration-features/comments): Send emails when users have
  unread comments.
- [Notifications](/docs/collaboration-features/notifications): Trigger custom
  notifications in notification inboxes.
- [AI Copilots](/docs/collaboration-features/ai-copilots): Upload knowledge
  sources to your chats.

## Get started

To get started with the Python SDK, read our new API reference page.

<ButtonLink href="/docs/api-reference/liveblocks-python" size="lg">
  Get started now
</ButtonLink>

## Contributors

<Contributors gitHubUsernames={["nimeshnayaju", "ctnicholas"]} />