> For the complete documentation index, see [llms.txt](https://docs.aimlapi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aimlapi.com/solutions/openai/assistants/threads.md).

# Thread API

**Threads** serve as conversation containers that store Messages exchanged between a user and an Assistant, maintaining context and continuity across interactions.

This page provides API schemas for the following methods:

<table><thead><tr><th width="302.0833740234375"></th><th></th></tr></thead><tbody><tr><td><a href="#create-a-thread">Create a Thread</a></td><td><img src="/files/j1sGKuO2wS5fYyQZ5SGg" alt="" data-size="line"> <code>https://api.aimlapi.com/threads</code></td></tr><tr><td><a href="#retrieve-information-about-a-specific-thread-by-its-id">Retrieve information about a specific Thread by its ID</a></td><td><img src="/files/vAXd7V6xv3QmKrLeptB1" alt="" data-size="line"> <code>https://api.aimlapi.com/threads/{threadId}</code></td></tr><tr><td><a href="#modify-a-specific-thread-by-its-id">Modify a specific Thread by its ID</a></td><td><img src="/files/j1sGKuO2wS5fYyQZ5SGg" alt="" data-size="line"> <code>https://api.aimlapi.com/threads/{threadId}</code></td></tr><tr><td><a href="#delete-a-specific-thread-by-its-id">Delete a specific Thread by its ID</a></td><td><img src="/files/ejZ6RTFbVq5OB7Ni1f3E" alt="" data-size="line"> <code>https://api.aimlapi.com/threads/{threadId}</code></td></tr></tbody></table>

After each API schema, you'll find a short example demonstrating how to correctly call the described method in code using the OpenAI SDK.

{% hint style="warning" %}
Note that the method names in the API schema and the SDK often differ.\
Accordingly, when calling these methods via the REST API, you should use the names from the API schema, while for calls through the OpenAI SDK, use the names from the examples.
{% endhint %}

## API Schemas

### Create a Thread

#### Python + OpenAI SDK Example:

<pre class="language-python"><code class="lang-python"><strong>from openai import OpenAI
</strong>client = OpenAI()

thread = client.beta.threads.create(
  messages=[
    {
      "role": "user",
      "content": "Create 3 data visualizations based on the trends in this file.",
      "attachments": [
        {
          "file_id": file.id,
          "tools": [{"type": "code_interpreter"}]
        }
      ]
    }
  ]
)        
</code></pre>

***

### Retrieve information about a specific Thread by its ID

#### Python + OpenAI SDK Example:

```python
from openai import OpenAI
client = OpenAI()

my_thread = client.beta.threads.retrieve("thread_abc123")
print(my_thread)
```

***

### Modify a specific Thread by its ID

#### Python + OpenAI SDK Example:

```python
from openai import OpenAI
client = OpenAI()

my_updated_thread = client.beta.threads.update(
  "thread_abc123",
  metadata={
    "modified": "true",
    "user": "abc123"
  }
)
print(my_updated_thread)
```

***

### Delete a specific Thread by its ID

#### Python + OpenAI SDK Example:

```python
from openai import OpenAI
client = OpenAI()

response = client.beta.threads.delete("thread_abc123")
print(response)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aimlapi.com/solutions/openai/assistants/threads.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
