Checkout the documentation about threads for more information.

Note that only the get_thread and export_threads methods return the thread steps.

Creating a thread

thread = await client.api.create_thread(
    participant_id="<PARTICIPANT_UUID>",
    environment="production",
    tags=["tag1", "tag2"],
    metadata={"key": "value"},
)
participant_id
uuid

Id of the participant

environment
string

Environment of the thread

tags
list

The tags of the thread

metadata
dict

The metadata of the thread

Get a thread

Use the thread id to get a thread. The returned thread contains the list of steps.

thread = await client.api.get_thread(id="<THREAD_UUID>")
id
uuid
required

The id of the thread

List threads

You can list threads, with optional filters. It returns a paginated list of threads.

The returned thread list doesn’t contain the thread steps. If you need the steps, you should use the export_threads method.

threads = await client.api.list_threads(first=None, after=None, filters=None)

print(threads["pageInfo"])
for thread in threads["data"]:
    print(thread.to_dict())
first after
int

Pagination parameters, checkout GraphQL doc for more information

filter
dict

You can filter threads with the following parameters:

Exporting threads

This method is optimized to export a large number of threads. It returns a paginated list of threads with their steps. You can optionally filter the threads.

from literalai.thread import DateTimeFilter, ThreadFilter

filters = ThreadFilter(
    createdAt=DateTimeFilter(operator="gt", value="2023-12-14"),
)

page = 1
result = await client.api.export_threads(page=page, filters=filters)

while result.hasNextPage:
    page += 1
    result = await client.api.export_threads(page=page, filters=filters)

    # Do something with the thread list in result.data
after
string

Pagination parameter, use the pageInfo.endCursor from the previous result.

filter
dict

You can filter threads with the following parameters:

Update thread

updated_thread = await client.update_thread(
    id="<THREAD_UUID>",
    name=None,
    metadata=None,
    participant_id=None,
    environment=None,
    tags=None,
)
id
uuid
required

The id of the thread

name
str

The name of the thread

metadata
dict

The metadata of the thread

participant_id
uuid

The participant id of the thread

environment
string

The environment of the thread

tags
list

The tags of the thread

Upsert thread

It will either create a new thread if the thread_id does not exist or update the existing one.

thread = await client.upsert_thread(
    thead_id="<THREAD_UUID>",
    name=None,
    metadata=None,
    participant_id=None,
    environment=None,
    tags=None
)
thread_id
uuid
required

The id of the thread

name
str

The name of the thread

metadata
dict

The metadata of the thread

participant_id
uuid

The participant id of the thread

environment
string

The environment of the thread

tags
list

The tags of the thread

Delete thread

await client.api.delete_thread(id="<THREAD_UUID>")
id
uuid
required

The id of the thread