> ## Documentation Index
> Fetch the complete documentation index at: https://chainlit-5-wd-prompt.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Thread

Checkout the documentation about [threads](/concepts/thread) for more information.

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

## Creating a thread

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

<ParamField path="participant_id" type="uuid">
  Id of the participant
</ParamField>

<ParamField path="environment" type="string">
  Environment of the thread
</ParamField>

<ParamField path="tags" type="list">
  The tags of the thread
</ParamField>

<ParamField path="metadata" type="dict">
  The metadata of the thread
</ParamField>

## Get a thread

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

```python
thread = await client.api.get_thread(id="<THREAD_UUID>")
```

<ParamField path="id" type="uuid" required>
  The id of the thread
</ParamField>

## 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.

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

print(threads["pageInfo"])
for thread in threads["data"]:
    print(thread.to_dict())
```

<ParamField path="first after" type="int">
  Pagination parameters, checkout [GraphQL
  doc](https://graphql.org/learn/pagination/) for more information
</ParamField>

<ParamField path="filter" type="dict">
  You can filter threads with the following parameters:

  <Expandable title="properties">
    <ResponseField name="attachmentsName" type="string" />

    <ResponseField name="attachmentsName" type="string" />

    <ResponseField name="createdAt" type="string" />

    <ResponseField name="afterCreatedAt" type="string" />

    <ResponseField name="beforeCreatedAt" type="string" />

    <ResponseField name="duration" type="numeric" />

    <ResponseField name="environment" type="string" />

    <ResponseField name="feedbacksValue" type="numeric" />

    <ResponseField name="participantsIdentifier" type="string" />

    <ResponseField name="search" type="string" />

    <ResponseField name="tokenCount" type="numeric" />
  </Expandable>
</ParamField>

## 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.

```python
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
```

<ParamField path="after" type="string">
  Pagination parameter, use the `pageInfo.endCursor` from the previous result.
</ParamField>

<ParamField path="filter" type="dict">
  You can filter threads with the following parameters:

  <Expandable title="properties">
    <ResponseField name="attachmentsName" type="string" />

    <ResponseField name="attachmentsName" type="string" />

    <ResponseField name="createdAt" type="string" />

    <ResponseField name="afterCreatedAt" type="string" />

    <ResponseField name="beforeCreatedAt" type="string" />

    <ResponseField name="duration" type="numeric" />

    <ResponseField name="environment" type="string" />

    <ResponseField name="feedbacksValue" type="numeric" />

    <ResponseField name="participantsIdentifier" type="string" />

    <ResponseField name="search" type="string" />

    <ResponseField name="tokenCount" type="numeric" />
  </Expandable>
</ParamField>

## Update thread

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

<ParamField path="id" type="uuid" required>
  The id of the thread
</ParamField>

<ParamField path="name" type="str">
  The name of the thread
</ParamField>

<ParamField path="metadata" type="dict">
  The metadata of the thread
</ParamField>

<ParamField path="participant_id" type="uuid">
  The participant id of the thread
</ParamField>

<ParamField path="environment" type="string">
  The environment of the thread
</ParamField>

<ParamField path="tags" type="list">
  The tags of the thread
</ParamField>

## Upsert thread

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

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

<ParamField path="thread_id" type="uuid" required>
  The id of the thread
</ParamField>

<ParamField path="name" type="str">
  The name of the thread
</ParamField>

<ParamField path="metadata" type="dict">
  The metadata of the thread
</ParamField>

<ParamField path="participant_id" type="uuid">
  The participant id of the thread
</ParamField>

<ParamField path="environment" type="string">
  The environment of the thread
</ParamField>

<ParamField path="tags" type="list">
  The tags of the thread
</ParamField>

## Delete thread

```python
await client.api.delete_thread(id="<THREAD_UUID>")
```

<ParamField path="id" type="uuid" required>
  The id of the thread
</ParamField>
