Skip to main content

OpenAI API

Giới thiệu

Bạn có thể tương tác với API thông qua các yêu cầu HTTP từ bất kỳ ngôn ngữ nào, thông qua các ràng buộc Python chính thức của chúng tôi, thư viện Node.js chính thức của chúng tôi hoặc thư viện do cộng đồng duy trì.

Để cài đặt các ràng buộc Python chính thức, hãy chạy lệnh sau:

Để cài đặt thư viện Node.js chính thức, hãy chạy lệnh sau trong thư mục dự án Node.js của bạn:

npm install openai@^4.0.0

Xác thực

Khóa API

API OpenAI sử dụng khóa API để xác thực. Bạn có thể tạo khóa API ở cấp tài khoản người dùng hoặc dịch vụ. Tài khoản dịch vụ được gắn với một cá nhân "bot" và nên được sử dụng để cung cấp quyền truy cập cho các hệ thống sản xuất. Mỗi khóa API có thể được giới hạn theo một trong những điều sau,

  1. Khóa dự án - Cung cấp quyền truy cập vào một dự án duy nhất (tùy chọn ưu tiên); truy cập khóa API dự án bằng cách chọn dự án cụ thể mà bạn muốn tạo khóa.
  2. Khóa người dùng - Khóa kế thừa của chúng tôi. Cung cấp quyền truy cập vào tất cả các tổ chức và tất cả các dự án mà người dùng đã được thêm vào; truy cập Khóa API để xem các khóa có sẵn của bạn. Chúng tôi khuyên bạn nên chuyển sang khóa dự án để có các biện pháp bảo mật tốt nhất, mặc dù quyền truy cập qua phương pháp này hiện vẫn được hỗ trợ.

Hãy nhớ rằng khóa API của bạn là một bí mật! Không chia sẻ nó với người khác hoặc hiển thị nó trong bất kỳ mã phía máy khách nào (trình duyệt, ứng dụng). Yêu cầu sản xuất phải được định tuyến qua máy chủ phụ trợ của riêng bạn, nơi khóa API của bạn có thể được tải an toàn từ biến môi trường hoặc dịch vụ quản lý khóa.

Tất cả các yêu cầu API phải bao gồm khóa API của bạn trong tiêu đề HTTP như sau:Authorization

Authorization: Bearer OPENAI_API_KEY

Tổ chức và dự án (tùy chọn)

Đối với người dùng thuộc nhiều tổ chức hoặc đang truy cập dự án của họ thông qua khóa API người dùng cũ, bạn có thể chuyển tiêu đề để chỉ định tổ chức và dự án nào được sử dụng cho yêu cầu API. Mức sử dụng từ các yêu cầu API này sẽ được tính là mức sử dụng cho tổ chức và dự án được chỉ định.

Để truy nhập trong một tổ chức, hãy bỏ qua tiêu đềDefault project``OpenAI-Project

Ví dụ về lệnh curl:

1 2 3 4 curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Organization: org-R5LrxYr03PtejHtgc8MQgFnj" \ -H "OpenAI-Project: $PROJECT_ID"

Ví dụ với gói Python:openai

1 2 3 4 5 6 from openai import OpenAI client = OpenAI( organization='org-R5LrxYr03PtejHtgc8MQgFnj', project='$PROJECT_ID', )

Ví dụ với gói Node.js:openai

1 2 3 4 5 6 import OpenAI from "openai"; const openai = new OpenAI({ organization: "org-R5LrxYr03PtejHtgc8MQgFnj", project: "$PROJECT_ID", });

Bạn có thể tìm thấy ID tổ chức trên trang Thiết đặt tổ chức của mình. ID dự án có thể được tìm thấy trên trang Cài đặt chung của bạn bằng cách chọn dự án cụ thể.

Đưa ra yêu cầu

Bạn có thể dán lệnh bên dưới vào thiết bị đầu cuối để chạy yêu cầu API đầu tiên. Đảm bảo thay thế bằng khóa API bí mật của bạn. Nếu bạn đang sử dụng khóa người dùng cũ và bạn có nhiều dự án, bạn cũng sẽ cần chỉ định ID dự án. Để cải thiện bảo mật, chúng tôi khuyên bạn nên chuyển sang khóa dựa trên dự án.$OPENAI_API_KEY

1 2 3 4 5 6 7 8 curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'

Yêu cầu này truy vấn mô hình (mà dưới mui xe trỏ đến một gpt-4o-minigpt-4o-mini Biến thể mô hình) để hoàn thành văn bản bắt đầu bằng lời nhắc "Nói đây là một bài kiểm tra". Bạn sẽ nhận được phản hồi tương tự như sau:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 { "id": "chatcmpl-abc123", "object": "chat.completion", "created": 1677858242, "model": "gpt-4o-mini", "usage": { "prompt_tokens": 13, "completion_tokens": 7, "total_tokens": 20 }, "choices": [ { "message": { "role": "assistant", "content": "\n\nThis is a test!" }, "logprobs": null, "finish_reason": "stop", "index": 0 } ] }

Bây giờ bạn đã tạo hoàn thành cuộc trò chuyện đầu tiên của mình, hãy chia nhỏ đối tượng phản hồi. Chúng ta có thể thấy is có nghĩa là API đã trả về toàn bộ quá trình hoàn thành trò chuyện do mô hình tạo ra mà không gặp phải bất kỳ giới hạn nào. Trong danh sách lựa chọn, chúng tôi chỉ tạo một thư duy nhất nhưng bạn có thể đặt tham số để tạo nhiều lựa chọn tin nhắn.finish_reason``stop``n

Streaming

API OpenAI cung cấp khả năng truyền phản hồi trở lại máy khách để cho phép kết quả một phần cho các yêu cầu nhất định. Để đạt được điều này, chúng tôi tuân theo tiêu chuẩn Sự kiện do máy chủ gửi. Các thư viện NodePython chính thức của chúng tôi bao gồm các trình trợ giúp để phân tích cú pháp các sự kiện này đơn giản hơn.

Tính năng phát trực tuyến được hỗ trợ cho cả API Hoàn thành cuộc trò chuyệnAPI Trợ lý. Phần này tập trung vào cách hoạt động của tính năng phát trực tuyến đối với Hoàn tất cuộc trò chuyện. Tìm hiểu thêm về cách hoạt động của tính năng phát trực tuyến trong API Trợ lý tại đây.

Trong Python, một yêu cầu phát trực tuyến trông giống như:

1 2 3 4 5 6 7 8 9 10 11 12 from openai import OpenAI client = OpenAI() stream = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Say this is a test"}], stream=True, ) for chunk in stream: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="")

Trong Node / Typescript, một yêu cầu phát trực tuyến trông giống như:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import OpenAI from "openai"; const openai = new OpenAI(); async function main() { const stream = await openai.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "Say this is a test" }], stream: true, }); for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || ""); } } main();

Phân tích cú pháp sự kiện do máy chủ gửi

Phân tích cú pháp các sự kiện do Server gửi là không tầm thường và nên được thực hiện một cách thận trọng. Các chiến lược đơn giản như tách theo một dòng mới có thể dẫn đến lỗi phân tích cú pháp. Chúng tôi khuyên bạn nên sử dụng các thư viện máy khách hiện có khi có thể.

Tạo giọng nói

trụ https://api.openai.com/v1/audio/speech

Tạo âm thanh từ văn bản đầu vào.

Nội dung yêu cầu

Văn bản để tạo âm thanh cho. Độ dài tối đa là 4096 ký tự.

Định dạng để âm thanh trong. Các định dạng được hỗ trợ là , , , , , và .mp3``opus``aac``flac``wav``pcm

Tốc độ của âm thanh được tạo ra. Chọn một giá trị từ đến . là mặc định.0.25``4.0``1.0

Trở lại

1 2 3 4 5 6 7 8 9 curl https://api.openai.com/v1/audio/speech \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "tts-1", "input": "The quick brown fox jumped over the lazy dog.", "voice": "alloy" }' \ --output speech.mp3

Tạo bản chép lời

trụ https://api.openai.com/v1/audio/transcriptions

Phiên âm âm thanh sang ngôn ngữ nhập.

Nội dung yêu cầu

Đối tượng tệp âm thanh (không phải tên tệp) để phiên âm, ở một trong các định dạng sau: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav hoặc webm.

ID của mô hình để sử dụng. Chỉ (được cung cấp bởi mô hình Whisper V2 mã nguồn mở của chúng tôi) hiện có sẵn.whisper-1

Ngôn ngữ của âm thanh đầu vào. Cung cấp ngôn ngữ đầu vào ở định dạng ISO-639-1 sẽ cải thiện độ chính xác và độ trễ.

Văn bản tùy chọn để hướng dẫn kiểu của mô hình hoặc tiếp tục phân đoạn âm thanh trước đó. Lời nhắc phải khớp với ngôn ngữ âm thanh.

Định dạng của đầu ra bản chép lời, ở một trong các tùy chọn sau: , , , , hoặc .json``text``srt``verbose_json``vtt

Nhiệt độ lấy mẫu, từ 0 đến 1. Các giá trị cao hơn như 0,8 sẽ làm cho đầu ra ngẫu nhiên hơn, trong khi các giá trị thấp hơn như 0,2 sẽ làm cho nó tập trung và xác định hơn. Nếu được đặt thành 0, mô hình sẽ sử dụng xác suất nhật ký để tự động tăng nhiệt độ cho đến khi đạt đến ngưỡng nhất định.

Dấu thời gian chi tiết để điền cho phiên âm này. phải được đặt để sử dụng độ chi tiết của dấu thời gian. Một hoặc cả hai tùy chọn này được hỗ trợ: , hoặc . Lưu ý: Không có độ trễ bổ sung cho dấu thời gian phân đoạn, nhưng việc tạo dấu thời gian từ sẽ làm tăng thêm độ trễ.response_format``verbose_json``word``segment

Trở lại

1 2 3 4 5 curl https://api.openai.com/v1/audio/transcriptions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: multipart/form-data" \ -F file="@/path/to/file/audio.mp3" \ -F model="whisper-1"
1 2 3 { "text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger. This is a place where you can get to do that." }

Tạo bản dịch

trụ https://api.openai.com/v1/audio/translations

Dịch âm thanh sang tiếng Anh.

Nội dung yêu cầu

Đối tượng tệp âm thanh (không phải tên tệp) dịch, ở một trong các định dạng sau: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav hoặc webm.

ID của mô hình để sử dụng. Chỉ (được cung cấp bởi mô hình Whisper V2 mã nguồn mở của chúng tôi) hiện có sẵn.whisper-1

Văn bản tùy chọn để hướng dẫn kiểu của mô hình hoặc tiếp tục phân đoạn âm thanh trước đó. Lời nhắc phải bằng tiếng Anh.

Định dạng của đầu ra bản chép lời, ở một trong các tùy chọn sau: , , , , hoặc .json``text``srt``verbose_json``vtt

Nhiệt độ lấy mẫu, từ 0 đến 1. Các giá trị cao hơn như 0,8 sẽ làm cho đầu ra ngẫu nhiên hơn, trong khi các giá trị thấp hơn như 0,2 sẽ làm cho nó tập trung và xác định hơn. Nếu được đặt thành 0, mô hình sẽ sử dụng xác suất nhật ký để tự động tăng nhiệt độ cho đến khi đạt đến ngưỡng nhất định.

Trở lại

1 2 3 4 5 curl https://api.openai.com/v1/audio/translations \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: multipart/form-data" \ -F file="@/path/to/file/german.m4a" \ -F model="whisper-1"
1 2 3 { "text": "Hello, my name is Wolfgang and I come from Germany. Where are you heading today?" }

Đối tượng phiên âm (JSON)

Đại diện cho phản hồi phiên âm được trả về bởi mô hình, dựa trên đầu vào được cung cấp.

1 2 3 { "text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger. This is a place where you can get to do that." }

Đối tượng phiên âm (Verbose JSON)

Đại diện cho phản hồi phiên âm json dài dòng được trả về bởi model, dựa trên đầu vào được cung cấp.

Ngôn ngữ của âm thanh đầu vào.

Thời lượng của âm thanh đầu vào.

Các từ được trích xuất và dấu thời gian tương ứng của chúng.

Các phân đoạn của văn bản được phiên âm và các chi tiết tương ứng của chúng.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 { "task": "transcribe", "language": "english", "duration": 8.470000267028809, "text": "The beach was a popular spot on a hot summer day. People were swimming in the ocean, building sandcastles, and playing beach volleyball.", "segments": [ { "id": 0, "seek": 0, "start": 0.0, "end": 3.319999933242798, "text": " The beach was a popular spot on a hot summer day.", "tokens": [ 50364, 440, 7534, 390, 257, 3743, 4008, 322, 257, 2368, 4266, 786, 13, 50530 ], "temperature": 0.0, "avg_logprob": -0.2860786020755768, "compression_ratio": 1.2363636493682861, "no_speech_prob": 0.00985979475080967 }, ... ] }

Tạo hoàn tất cuộc trò chuyện

trụ https://api.openai.com/v1/chat/completions

Tạo phản hồi mẫu cho cuộc trò chuyện đã cho.

Nội dung yêu cầu

Sửa đổi khả năng các mã thông báo được chỉ định xuất hiện trong quá trình hoàn thành.

Chấp nhận một đối tượng JSON ánh xạ mã thông báo (được chỉ định bởi ID mã thông báo của chúng trong trình mã thông báo) thành giá trị thiên vị liên quan từ -100 đến 100. Về mặt toán học, độ lệch được thêm vào nhật ký được tạo bởi mô hình trước khi lấy mẫu. Hiệu ứng chính xác sẽ khác nhau đối với mỗi kiểu máy, nhưng các giá trị từ -1 đến 1 sẽ làm giảm hoặc tăng khả năng lựa chọn; Các giá trị như -100 hoặc 100 sẽ dẫn đến lệnh cấm hoặc lựa chọn độc quyền mã thông báo có liên quan.

Có trả về xác suất nhật ký của mã thông báo đầu ra hay không. Nếu đúng, trả về xác suất nhật ký của mỗi mã thông báo đầu ra được trả về trong of .content``message

Một số nguyên từ 0 đến 20 chỉ định số lượng mã thông báo có khả năng trả về nhất tại mỗi vị trí mã thông báo, mỗi vị trí có xác suất nhật ký liên quan. phải được đặt thành nếu tham số này được sử dụng.logprobs``true

Số lượng mã thông báo tối đa có thể được tạo trong quá trình hoàn thành trò chuyện.

Tổng độ dài của mã thông báo đầu vào và mã thông báo được tạo bị giới hạn bởi độ dài ngữ cảnh của mô hình. Ví dụ về mã Python để đếm mã thông báo.

Có bao nhiêu lựa chọn hoàn thành cuộc trò chuyện để tạo cho mỗi tin nhắn đầu vào. Lưu ý rằng bạn sẽ bị tính phí dựa trên số lượng mã thông báo được tạo trên tất cả các lựa chọn. Giữ như để giảm thiểu chi phí.n``1

An object specifying the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same and parameters should return the same result. Determinism is not guaranteed, and you should refer to the response parameter to monitor changes in the backend.seed``system_fingerprint

Specifies the latency tier to use for processing the request. This parameter is relevant for customers subscribed to the scale tier service:

  • If set to 'auto', the system will utilize scale tier credits until they are exhausted.
  • If set to 'default', the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee.

When this parameter is set, the response body will include the utilized.service_tier

Up to 4 sequences where the API will stop generating further tokens.

If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a message. Example Python code.data: [DONE]

Options for streaming response. Only set this when you set .stream: true

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

We generally recommend altering this or but not both.top_p

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or but not both.temperature

A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.

Controls which (if any) tool is called by the model. means the model will not call any tool and instead generates a message. means the model can pick between generating a message or calling one or more tools. means the model must call one or more tools. Specifying a particular tool via forces the model to call that tool.none``auto``required``{"type": "function", "function": {"name": "my_function"}}

none is the default when no tools are present. is the default if tools are present.auto

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Deprecated in favor of .tool_choice

Controls which (if any) function is called by the model. means the model will not call a function and instead generates a message. means the model can pick between generating a message or calling a function. Specifying a particular function via forces the model to call that function.none``auto``{"name": "my_function"}

none is the default when no functions are present. is the default if functions are present.auto

Deprecated in favor of .tools

A list of functions the model may generate JSON inputs for.

Returns

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices": [{ "index": 0, "message": { "role": "assistant", "content": "\n\nHello there, how may I assist you today?", }, "logprobs": null, "finish_reason": "stop" }], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } }

The chat completion object

Represents a chat completion response returned by model, based on the provided input.

A unique identifier for the chat completion.

A list of chat completion choices. Can be more than one if is greater than 1.n

The Unix timestamp (in seconds) of when the chat completion was created.

The model used for the chat completion.

The service tier used for processing the request. This field is only included if the parameter is specified in the request.service_tier

This fingerprint represents the backend configuration that the model runs with.

Can be used in conjunction with the request parameter to understand when backend changes have been made that might impact determinism.seed

The object type, which is always .chat.completion

Usage statistics for the completion request.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices": [{ "index": 0, "message": { "role": "assistant", "content": "\n\nHello there, how may I assist you today?", }, "logprobs": null, "finish_reason": "stop" }], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } }

The chat completion chunk object

Represents a streamed chunk of a chat completion response returned by model, based on the provided input.

A unique identifier for the chat completion. Each chunk has the same ID.

A list of chat completion choices. Can contain more than one elements if is greater than 1. Can also be empty for the last chunk if you set .n``stream_options: {"include_usage": true}

The Unix timestamp (in seconds) of when the chat completion was created. Each chunk has the same timestamp.

The model to generate the completion.

The service tier used for processing the request. This field is only included if the parameter is specified in the request.service_tier

This fingerprint represents the backend configuration that the model runs with. Can be used in conjunction with the request parameter to understand when backend changes have been made that might impact determinism.seed

The object type, which is always .chat.completion.chunk

An optional field that will only be present when you set in your request. When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request.stream_options: {"include_usage": true}

1 2 3 4 5 6 7 {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]} {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]} .... {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}

Embeddings

Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.

Related guide: Embeddings

Create embeddings

post https://api.openai.com/v1/embeddings

Creates an embedding vector representing the input text.

Request body

Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for ), cannot be an empty string, and any array must be 2048 dimensions or less. Example Python code for counting tokens.text-embedding-ada-002

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

The format to return the embeddings in. Can be either or floatbase64.

The number of dimensions the resulting output embeddings should have. Only supported in and later models.text-embedding-3

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Returns

1 2 3 4 5 6 7 8 curl https://api.openai.com/v1/embeddings \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "input": "The food was delicious and the waiter...", "model": "text-embedding-ada-002", "encoding_format": "float" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "object": "list", "data": [ { "object": "embedding", "embedding": [ 0.0023064255, -0.009327292, .... (1536 floats total for ada-002) -0.0028842222, ], "index": 0 } ], "model": "text-embedding-ada-002", "usage": { "prompt_tokens": 8, "total_tokens": 8 } }

The embedding object

Represents an embedding vector returned by embedding endpoint.

The index of the embedding in the list of embeddings.

The embedding vector, which is a list of floats. The length of vector depends on the model as listed in the embedding guide.

The object type, which is always "embedding".

1 2 3 4 5 6 7 8 9 10 { "object": "embedding", "embedding": [ 0.0023064255, -0.009327292, .... (1536 floats total for ada-002) -0.0028842222, ], "index": 0 }

Create fine-tuning job

post https://api.openai.com/v1/fine\_tuning/jobs

Creates a fine-tuning job which begins the process of creating a new model from a given dataset.

Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.

Learn more about fine-tuning

Request body

The ID of an uploaded file that contains training data.

See upload file for how to upload a file.

Your dataset must be formatted as a JSONL file. Additionally, you must upload your file with the purpose .fine-tune

The contents of the file should differ depending on if the model uses the chat or completions format.

See the fine-tuning guide for more details.

The hyperparameters used for the fine-tuning job.

A string of up to 18 characters that will be added to your fine-tuned model name.

For example, a of "custom-model-name" would produce a model name like .suffix``ft:gpt-3.5-turbo:openai:custom-model-name:7p4lURel

The ID of an uploaded file that contains validation data.

If you provide this file, the data is used to generate validation metrics periodically during fine-tuning. These metrics can be viewed in the fine-tuning results file. The same data should not be present in both train and validation files.

Your dataset must be formatted as a JSONL file. You must upload your file with the purpose .fine-tune

See the fine-tuning guide for more details.

A list of integrations to enable for your fine-tuning job.

The seed controls the reproducibility of the job. Passing in the same seed and job parameters should produce the same results, but may differ in rare cases. If a seed is not specified, one will be generated for you.

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/fine_tuning/jobs \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "training_file": "file-BK7bzQj3FfZFXr7DbL6xJwfo", "model": "gpt-3.5-turbo" }'
1 2 3 4 5 6 7 8 9 10 11 12 { "object": "fine_tuning.job", "id": "ftjob-abc123", "model": "gpt-3.5-turbo-0125", "created_at": 1614807352, "fine_tuned_model": null, "organization_id": "org-123", "result_files": [], "status": "queued", "validation_file": null, "training_file": "file-abc123", }

List fine-tuning jobs

get https://api.openai.com/v1/fine\_tuning/jobs

List your organization's fine-tuning jobs

Query parameters

Identifier for the last job from the previous pagination request.

Number of fine-tuning jobs to retrieve.

Returns

1 2 curl https://api.openai.com/v1/fine_tuning/jobs?limit=2 \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 { "object": "list", "data": [ { "object": "fine_tuning.job.event", "id": "ft-event-TjX0lMfOniCZX64t9PUQT5hn", "created_at": 1689813489, "level": "warn", "message": "Fine tuning process stopping due to job cancellation", "data": null, "type": "message" }, { ... }, { ... } ], "has_more": true }

List fine-tuning events

get https://api.openai.com/v1/fine\_tuning/jobs/{fine\_tuning\_job\_id}/events

Get status updates for a fine-tuning job.

Path parameters

The ID of the fine-tuning job to get events for.

Query parameters

Identifier for the last event from the previous pagination request.

Number of events to retrieve.

Returns

A list of fine-tuning event objects.

1 2 curl https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/events \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 { "object": "list", "data": [ { "object": "fine_tuning.job.event", "id": "ft-event-ddTJfwuMVpfLXseO0Am0Gqjm", "created_at": 1692407401, "level": "info", "message": "Fine tuning job successfully completed", "data": null, "type": "message" }, { "object": "fine_tuning.job.event", "id": "ft-event-tyiGuB72evQncpH87xe505Sv", "created_at": 1692407400, "level": "info", "message": "New fine-tuned model created: ft:gpt-3.5-turbo:openai::7p4lURel", "data": null, "type": "message" } ], "has_more": true }

List fine-tuning checkpoints

get https://api.openai.com/v1/fine\_tuning/jobs/{fine\_tuning\_job\_id}/checkpoints

List checkpoints for a fine-tuning job.

Path parameters

The ID of the fine-tuning job to get checkpoints for.

Query parameters

Identifier for the last checkpoint ID from the previous pagination request.

Number of checkpoints to retrieve.

Returns

1 2 curl https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/checkpoints \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 { "object": "list" "data": [ { "object": "fine_tuning.job.checkpoint", "id": "ftckpt_zc4Q7MP6XxulcVzj4MZdwsAB", "created_at": 1519129973, "fine_tuned_model_checkpoint": "ft:gpt-3.5-turbo-0125:my-org:custom-suffix:96olL566:ckpt-step-2000", "metrics": { "full_valid_loss": 0.134, "full_valid_mean_token_accuracy": 0.874 }, "fine_tuning_job_id": "ftjob-abc123", "step_number": 2000, }, { "object": "fine_tuning.job.checkpoint", "id": "ftckpt_enQCFmOTGj3syEpYVhBRLTSy", "created_at": 1519129833, "fine_tuned_model_checkpoint": "ft:gpt-3.5-turbo-0125:my-org:custom-suffix:7q8mpxmy:ckpt-step-1000", "metrics": { "full_valid_loss": 0.167, "full_valid_mean_token_accuracy": 0.781 }, "fine_tuning_job_id": "ftjob-abc123", "step_number": 1000, }, ], "first_id": "ftckpt_zc4Q7MP6XxulcVzj4MZdwsAB", "last_id": "ftckpt_enQCFmOTGj3syEpYVhBRLTSy", "has_more": true }

Retrieve fine-tuning job

get https://api.openai.com/v1/fine\_tuning/jobs/{fine\_tuning\_job\_id}

Path parameters

The ID of the fine-tuning job.

Returns

1 2 curl https://api.openai.com/v1/fine_tuning/jobs/ft-AF1WoRqd3aJAHsqc9NY7iL8F \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 { "object": "fine_tuning.job", "id": "ftjob-abc123", "model": "davinci-002", "created_at": 1692661014, "finished_at": 1692661190, "fine_tuned_model": "ft:davinci-002:my-org:custom_suffix:7q8mpxmy", "organization_id": "org-123", "result_files": [ "file-abc123" ], "status": "succeeded", "validation_file": null, "training_file": "file-abc123", "hyperparameters": { "n_epochs": 4, "batch_size": 1, "learning_rate_multiplier": 1.0 }, "trained_tokens": 5768, "integrations": [], "seed": 0, "estimated_finish": 0 }

Cancel fine-tuning

post https://api.openai.com/v1/fine\_tuning/jobs/{fine\_tuning\_job\_id}/cancel

Immediately cancel a fine-tune job.

Path parameters

The ID of the fine-tuning job to cancel.

Returns

1 2 curl -X POST https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/cancel \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { "object": "fine_tuning.job", "id": "ftjob-abc123", "model": "gpt-3.5-turbo-0125", "created_at": 1689376978, "fine_tuned_model": null, "organization_id": "org-123", "result_files": [], "hyperparameters": { "n_epochs": "auto" }, "status": "cancelled", "validation_file": "file-abc123", "training_file": "file-abc123" }

Training format for chat models

The per-line training example of a fine-tuning input file for chat models

A list of tools the model may generate JSON inputs for.

A list of functions the model may generate JSON inputs for.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 { "messages": [ { "role": "user", "content": "What is the weather in San Francisco?" }, { "role": "assistant", "tool_calls": [ { "id": "call_id", "type": "function", "function": { "name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}" } } ] } ], "parallel_tool_calls": false, "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and country, eg. San Francisco, USA" }, "format": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location", "format"] } } } ] }

Training format for completions models

The per-line training example of a fine-tuning input file for completions models

The input prompt for this training example.

The desired completion for this training example.

1 2 3 4 { "prompt": "What is the answer to 2+2", "completion": "4" }

The fine-tuning job object

The object represents a fine-tuning job that has been created through the API.fine_tuning.job

The object identifier, which can be referenced in the API endpoints.

The Unix timestamp (in seconds) for when the fine-tuning job was created.

For fine-tuning jobs that have , this will contain more information on the cause of the failure.failed

The name of the fine-tuned model that is being created. The value will be null if the fine-tuning job is still running.

The Unix timestamp (in seconds) for when the fine-tuning job was finished. The value will be null if the fine-tuning job is still running.

The hyperparameters used for the fine-tuning job. See the fine-tuning guide for more details.

The base model that is being fine-tuned.

The object type, which is always "fine_tuning.job".

The organization that owns the fine-tuning job.

The compiled results file ID(s) for the fine-tuning job. You can retrieve the results with the Files API.

The current status of the fine-tuning job, which can be either , , , , , or .validating_files``queued``running``succeeded``failed``cancelled

The total number of billable tokens processed by this fine-tuning job. The value will be null if the fine-tuning job is still running.

The file ID used for training. You can retrieve the training data with the Files API.

The file ID used for validation. You can retrieve the validation results with the Files API.

A list of integrations to enable for this fine-tuning job.

The seed used for the fine-tuning job.

The Unix timestamp (in seconds) for when the fine-tuning job is estimated to finish. The value will be null if the fine-tuning job is not running.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 { "object": "fine_tuning.job", "id": "ftjob-abc123", "model": "davinci-002", "created_at": 1692661014, "finished_at": 1692661190, "fine_tuned_model": "ft:davinci-002:my-org:custom_suffix:7q8mpxmy", "organization_id": "org-123", "result_files": [ "file-abc123" ], "status": "succeeded", "validation_file": null, "training_file": "file-abc123", "hyperparameters": { "n_epochs": 4, "batch_size": 1, "learning_rate_multiplier": 1.0 }, "trained_tokens": 5768, "integrations": [], "seed": 0, "estimated_finish": 0 }

The fine-tuning job event object

Fine-tuning job event object

1 2 3 4 5 6 7 { "object": "fine_tuning.job.event", "id": "ftevent-abc123" "created_at": 1677610602, "level": "info", "message": "Created fine-tuning job" }

The fine-tuning job checkpoint object

The object represents a model checkpoint for a fine-tuning job that is ready to use.fine_tuning.job.checkpoint

The checkpoint identifier, which can be referenced in the API endpoints.

The Unix timestamp (in seconds) for when the checkpoint was created.

The name of the fine-tuned checkpoint model that is created.

The step number that the checkpoint was created at.

Metrics at the step number during the fine-tuning job.

The name of the fine-tuning job that this checkpoint was created from.

The object type, which is always "fine_tuning.job.checkpoint".

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 { "object": "fine_tuning.job.checkpoint", "id": "ftckpt_qtZ5Gyk4BLq1SfLFWp3RtO3P", "created_at": 1712211699, "fine_tuned_model_checkpoint": "ft:gpt-3.5-turbo-0125:my-org:custom_suffix:9ABel2dg:ckpt-step-88", "fine_tuning_job_id": "ftjob-fpbNQ3H1GrMehXRf8cO97xTN", "metrics": { "step": 88, "train_loss": 0.478, "train_mean_token_accuracy": 0.924, "valid_loss": 10.112, "valid_mean_token_accuracy": 0.145, "full_valid_loss": 0.567, "full_valid_mean_token_accuracy": 0.944 }, "step_number": 88 }

Batch

Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.

Related guide: Batch

Create batch

post https://api.openai.com/v1/batches

Creates and executes a batch from an uploaded file of requests

Request body

The ID of an uploaded file that contains requests for the new batch.

See upload file for how to upload a file.

Your input file must be formatted as a JSONL file, and must be uploaded with the purpose . The file can contain up to 50,000 requests, and can be up to 100 MB in size.batch

The endpoint to be used for all requests in the batch. Currently , , and are supported. Note that batches are also restricted to a maximum of 50,000 embedding inputs across all requests in the batch./v1/chat/completions``/v1/embeddings``/v1/completions``/v1/embeddings

The time frame within which the batch should be processed. Currently only is supported.24h

Optional custom metadata for the batch.

Returns

The created Batch object.

1 2 3 4 5 6 7 8 curl https://api.openai.com/v1/batches \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "input_file_id": "file-abc123", "endpoint": "/v1/chat/completions", "completion_window": "24h" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 { "id": "batch_abc123", "object": "batch", "endpoint": "/v1/chat/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "validating", "output_file_id": null, "error_file_id": null, "created_at": 1711471533, "in_progress_at": null, "expires_at": null, "finalizing_at": null, "completed_at": null, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 0, "completed": 0, "failed": 0 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job", } }

Retrieve batch

get https://api.openai.com/v1/batches/{batch\_id}

Retrieves a batch.

Path parameters

The ID of the batch to retrieve.

Returns

The Batch object matching the specified ID.

1 2 3 curl https://api.openai.com/v1/batches/batch_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 { "id": "batch_abc123", "object": "batch", "endpoint": "/v1/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "completed", "output_file_id": "file-cvaTdG", "error_file_id": "file-HOWS94", "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": 1711493133, "completed_at": 1711493163, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 100, "completed": 95, "failed": 5 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job", } }

Cancel batch

post https://api.openai.com/v1/batches/{batch\_id}/cancel

Cancels an in-progress batch. The batch will be in status for up to 10 minutes, before changing to , where it will have partial results (if any) available in the output file.cancelling``cancelled

Path parameters

The ID of the batch to cancel.

Returns

The Batch object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/batches/batch_abc123/cancel \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -X POST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 { "id": "batch_abc123", "object": "batch", "endpoint": "/v1/chat/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "cancelling", "output_file_id": null, "error_file_id": null, "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": null, "completed_at": null, "failed_at": null, "expired_at": null, "cancelling_at": 1711475133, "cancelled_at": null, "request_counts": { "total": 100, "completed": 23, "failed": 1 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job", } }

List batch

get https://api.openai.com/v1/batches

List your organization's batches.

Query parameters

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Returns

A list of paginated Batch objects.

1 2 3 curl https://api.openai.com/v1/batches?limit=2 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 { "object": "list", "data": [ { "id": "batch_abc123", "object": "batch", "endpoint": "/v1/chat/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "completed", "output_file_id": "file-cvaTdG", "error_file_id": "file-HOWS94", "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": 1711493133, "completed_at": 1711493163, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 100, "completed": 95, "failed": 5 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly job", } }, { ... }, ], "first_id": "batch_abc123", "last_id": "batch_abc456", "has_more": true }

The batch object

The object type, which is always .batch

The OpenAI API endpoint used by the batch.

The ID of the input file for the batch.

The time frame within which the batch should be processed.

The current status of the batch.

The ID of the file containing the outputs of successfully executed requests.

The ID of the file containing the outputs of requests with errors.

The Unix timestamp (in seconds) for when the batch was created.

The Unix timestamp (in seconds) for when the batch started processing.

The Unix timestamp (in seconds) for when the batch will expire.

The Unix timestamp (in seconds) for when the batch started finalizing.

The Unix timestamp (in seconds) for when the batch was completed.

The Unix timestamp (in seconds) for when the batch failed.

The Unix timestamp (in seconds) for when the batch expired.

The Unix timestamp (in seconds) for when the batch started cancelling.

The Unix timestamp (in seconds) for when the batch was cancelled.

The request counts for different statuses within the batch.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 { "id": "batch_abc123", "object": "batch", "endpoint": "/v1/completions", "errors": null, "input_file_id": "file-abc123", "completion_window": "24h", "status": "completed", "output_file_id": "file-cvaTdG", "error_file_id": "file-HOWS94", "created_at": 1711471533, "in_progress_at": 1711471538, "expires_at": 1711557933, "finalizing_at": 1711493133, "completed_at": 1711493163, "failed_at": null, "expired_at": null, "cancelling_at": null, "cancelled_at": null, "request_counts": { "total": 100, "completed": 95, "failed": 5 }, "metadata": { "customer_id": "user_123456789", "batch_description": "Nightly eval job", } }

The request input object

The per-line object of the batch input file

A developer-provided per-request id that will be used to match outputs to inputs. Must be unique for each request in a batch.

The HTTP method to be used for the request. Currently only is supported.POST

The OpenAI API relative URL to be used for the request. Currently , , and are supported./v1/chat/completions``/v1/embeddings``/v1/completions

{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo", "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"}]}}

The request output object

The per-line object of the batch output and error files

A developer-provided per-request id that will be used to match outputs to inputs.

For requests that failed with a non-HTTP error, this will contain more information on the cause of the failure.

{"id": "batch_req_wnaDys", "custom_id": "request-2", "response": {"status_code": 200, "request_id": "req_c187b3", "body": {"id": "chatcmpl-9758Iw", "object": "chat.completion", "created": 1711475054, "model": "gpt-3.5-turbo", "choices": [{"index": 0, "message": {"role": "assistant", "content": "2 + 2 equals 4."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 24, "completion_tokens": 15, "total_tokens": 39}, "system_fingerprint": null}}, "error": null}

Upload file

post https://api.openai.com/v1/files

Upload a file that can be used across various endpoints. Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB.

The Assistants API supports files up to 2 million tokens and of specific file types. See the Assistants Tools guide for details.

The Fine-tuning API only supports files. The input also has certain required formats for fine-tuning chat or completions models..jsonl

The Batch API only supports files up to 100 MB in size. The input also has a specific required format..jsonl

Please contact us if you need to increase these storage limits.

Request body

The File object (not file name) to be uploaded.

The intended purpose of the uploaded file.

Use "assistants" for Assistants and Message files, "vision" for Assistants image file inputs, "batch" for Batch API, and "fine-tune" for Fine-tuning.

Returns

The uploaded File object.

1 2 3 4 curl https://api.openai.com/v1/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F purpose="fine-tune" \ -F file="@mydata.jsonl"
1 2 3 4 5 6 7 8 { "id": "file-abc123", "object": "file", "bytes": 120000, "created_at": 1677610602, "filename": "mydata.jsonl", "purpose": "fine-tune", }

List files

get https://api.openai.com/v1/files

Returns a list of files that belong to the user's organization.

Query parameters

Only return files with the given purpose.

Returns

1 2 curl https://api.openai.com/v1/files \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 { "data": [ { "id": "file-abc123", "object": "file", "bytes": 175, "created_at": 1613677385, "filename": "salesOverview.pdf", "purpose": "assistants", }, { "id": "file-abc123", "object": "file", "bytes": 140, "created_at": 1613779121, "filename": "puppy.jsonl", "purpose": "fine-tune", } ], "object": "list" }

Retrieve file

get https://api.openai.com/v1/files/{file\_id}

Returns information about a specific file.

Path parameters

The ID of the file to use for this request.

Returns

The File object matching the specified ID.

1 2 curl https://api.openai.com/v1/files/file-abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 { "id": "file-abc123", "object": "file", "bytes": 120000, "created_at": 1677610602, "filename": "mydata.jsonl", "purpose": "fine-tune", }

Delete file

delete https://api.openai.com/v1/files/{file\_id}

Delete a file.

Path parameters

The ID of the file to use for this request.

Returns

1 2 3 curl https://api.openai.com/v1/files/file-abc123 \ -X DELETE \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 { "id": "file-abc123", "object": "file", "deleted": true }

Retrieve file content

get https://api.openai.com/v1/files/{file\_id}/content

Returns the contents of the specified file.

Path parameters

The ID of the file to use for this request.

Returns

1 2 curl https://api.openai.com/v1/files/file-abc123/content \ -H "Authorization: Bearer $OPENAI_API_KEY" > file.jsonl

The file object

The object represents a document that has been uploaded to OpenAI.File

The file identifier, which can be referenced in the API endpoints.

The size of the file, in bytes.

The Unix timestamp (in seconds) for when the file was created.

The object type, which is always .file

The intended purpose of the file. Supported values are , , , , , and .assistants``assistants_output``batch``batch_output``fine-tune``fine-tune-results``vision

Deprecated. The current status of the file, which can be either , , or .uploaded``processed``error

Deprecated. For details on why a fine-tuning training file failed validation, see the field on .error``fine_tuning.job

1 2 3 4 5 6 7 8 { "id": "file-abc123", "object": "file", "bytes": 120000, "created_at": 1677610602, "filename": "salesOverview.pdf", "purpose": "assistants", }

Uploads

Allows you to upload large files in multiple parts.

Create upload

post https://api.openai.com/v1/uploads

Creates an intermediate Upload object that you can add Parts to. Currently, an Upload can accept at most 8 GB in total and expires after an hour after you create it.

Once you complete the Upload, we will create a File object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object.

For certain s, the correct must be specified. Please refer to documentation for the supported MIME types for your use case:purpose``mime_type

For guidance on the proper filename extensions for each purpose, please follow the documentation on creating a File.

Request body

The name of the file to upload.

The number of bytes in the file you are uploading.

The MIME type of the file.

This must fall within the supported MIME types for your file purpose. See the supported MIME types for assistants and vision.

Returns

The Upload object with status .pending

1 2 3 4 5 6 7 8 curl https://api.openai.com/v1/uploads \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "purpose": "fine-tune", "filename": "training_examples.jsonl", "bytes": 2147483648, "mime_type": "text/jsonl" }'
1 2 3 4 5 6 7 8 9 10 { "id": "upload_abc123", "object": "upload", "bytes": 2147483648, "created_at": 1719184911, "filename": "training_examples.jsonl", "purpose": "fine-tune", "status": "pending", "expires_at": 1719127296 }

Add upload part

post https://api.openai.com/v1/uploads/{upload\_id}/parts

Adds a Part to an Upload object. A Part represents a chunk of bytes from the file you are trying to upload.

Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB.

It is possible to add multiple Parts in parallel. You can decide the intended order of the Parts when you complete the Upload.

Path parameters

Request body

The chunk of bytes for this Part.

Returns

1 2 curl https://api.openai.com/v1/uploads/upload_abc123/parts -F data="aHR0cHM6Ly9hcGkub3BlbmFpLmNvbS92MS91cGxvYWRz..."
1 2 3 4 5 6 { "id": "part_def456", "object": "upload.part", "created_at": 1719185911, "upload_id": "upload_abc123" }

Complete upload

post https://api.openai.com/v1/uploads/{upload\_id}/complete

Completes the Upload.

Within the returned Upload object, there is a nested File object that is ready to use in the rest of the platform.

You can specify the order of the Parts by passing in an ordered list of the Part IDs.

The number of bytes uploaded upon completion must match the number of bytes initially specified when creating the Upload object. No Parts may be added after an Upload is completed.

Path parameters

Request body

The ordered list of Part IDs.

The optional md5 checksum for the file contents to verify if the bytes uploaded matches what you expect.

Returns

The Upload object with status with an additional property containing the created usable File object.completed``file

1 2 3 4 curl https://api.openai.com/v1/uploads/upload_abc123/complete -d '{ "part_ids": ["part_def456", "part_ghi789"] }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "upload_abc123", "object": "upload", "bytes": 2147483648, "created_at": 1719184911, "filename": "training_examples.jsonl", "purpose": "fine-tune", "status": "completed", "expires_at": 1719127296, "file": { "id": "file-xyz321", "object": "file", "bytes": 2147483648, "created_at": 1719186911, "filename": "training_examples.jsonl", "purpose": "fine-tune", } }

Cancel upload

post https://api.openai.com/v1/uploads/{upload\_id}/cancel

Cancels the Upload. No Parts may be added after an Upload is cancelled.

Path parameters

Returns

The Upload object with status .cancelled

curl https://api.openai.com/v1/uploads/upload_abc123/cancel
1 2 3 4 5 6 7 8 9 10 { "id": "upload_abc123", "object": "upload", "bytes": 2147483648, "created_at": 1719184911, "filename": "training_examples.jsonl", "purpose": "fine-tune", "status": "cancelled", "expires_at": 1719127296 }

The upload object

The Upload object can accept byte chunks in the form of Parts.

The Upload unique identifier, which can be referenced in API endpoints.

The Unix timestamp (in seconds) for when the Upload was created.

The name of the file to be uploaded.

The intended number of bytes to be uploaded.

The status of the Upload.

The Unix timestamp (in seconds) for when the Upload was created.

The object type, which is always "upload".

The object represents a document that has been uploaded to OpenAI.File

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "upload_abc123", "object": "upload", "bytes": 2147483648, "created_at": 1719184911, "filename": "training_examples.jsonl", "purpose": "fine-tune", "status": "completed", "expires_at": 1719127296, "file": { "id": "file-xyz321", "object": "file", "bytes": 2147483648, "created_at": 1719186911, "filename": "training_examples.jsonl", "purpose": "fine-tune", } }

The upload part object

The upload Part represents a chunk of bytes we can add to an Upload object.

The upload Part unique identifier, which can be referenced in API endpoints.

The Unix timestamp (in seconds) for when the Part was created.

The ID of the Upload object that this Part was added to.

The object type, which is always .upload.part

1 2 3 4 5 6 { "id": "part_def456", "object": "upload.part", "created_at": 1719186911, "upload_id": "upload_abc123" }

Images

Given a prompt and/or an input image, the model will generate a new image.

Related guide: Image generation

Create image

post https://api.openai.com/v1/images/generations

Creates an image given a prompt.

Request body

A text description of the desired image(s). The maximum length is 1000 characters for and 4000 characters for .dall-e-2``dall-e-3

The model to use for image generation.

The number of images to generate. Must be between 1 and 10. For , only is supported.dall-e-3``n=1

The quality of the image that will be generated. creates images with finer details and greater consistency across the image. This param is only supported for .hd``dall-e-3

The format in which the generated images are returned. Must be one of or . URLs are only valid for 60 minutes after the image has been generated.url``b64_json

The size of the generated images. Must be one of , , or for . Must be one of , , or for models.256x256``512x512``1024x1024``dall-e-2``1024x1024``1792x1024``1024x1792``dall-e-3

The style of the generated images. Must be one of or . Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for .vivid``natural``dall-e-3

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Returns

Returns a list of image objects.

1 2 3 4 5 6 7 8 9 curl https://api.openai.com/v1/images/generations \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "dall-e-3", "prompt": "A cute baby sea otter", "n": 1, "size": "1024x1024" }'
1 2 3 4 5 6 7 8 9 10 11 { "created": 1589478378, "data": [ { "url": "https://..." }, { "url": "https://..." } ] }

Create image edit

post https://api.openai.com/v1/images/edits

Creates an edited or extended image given an original image and a prompt.

Request body

The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.

A text description of the desired image(s). The maximum length is 1000 characters.

An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as .image``image

The model to use for image generation. Only is supported at this time.dall-e-2

The number of images to generate. Must be between 1 and 10.

The size of the generated images. Must be one of , , or .256x256``512x512``1024x1024

The format in which the generated images are returned. Must be one of or . URLs are only valid for 60 minutes after the image has been generated.url``b64_json

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Returns

Returns a list of image objects.

1 2 3 4 5 6 7 curl https://api.openai.com/v1/images/edits \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F image="@otter.png" \ -F mask="@mask.png" \ -F prompt="A cute baby sea otter wearing a beret" \ -F n=2 \ -F size="1024x1024"
1 2 3 4 5 6 7 8 9 10 11 { "created": 1589478378, "data": [ { "url": "https://..." }, { "url": "https://..." } ] }

Create image variation

post https://api.openai.com/v1/images/variations

Creates a variation of a given image.

Request body

The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.

The model to use for image generation. Only is supported at this time.dall-e-2

The number of images to generate. Must be between 1 and 10. For , only is supported.dall-e-3``n=1

The format in which the generated images are returned. Must be one of or . URLs are only valid for 60 minutes after the image has been generated.url``b64_json

The size of the generated images. Must be one of , , or .256x256``512x512``1024x1024

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Returns

Returns a list of image objects.

1 2 3 4 5 curl https://api.openai.com/v1/images/variations \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F image="@otter.png" \ -F n=2 \ -F size="1024x1024"
1 2 3 4 5 6 7 8 9 10 11 { "created": 1589478378, "data": [ { "url": "https://..." }, { "url": "https://..." } ] }

The image object

Represents the url or the content of an image generated by the OpenAI API.

The base64-encoded JSON of the generated image, if is .response_format``b64_json

The URL of the generated image, if is (default).response_format``url

The prompt that was used to generate the image, if there was any revision to the prompt.

1 2 3 4 { "url": "...", "revised_prompt": "..." }

Models

List and describe the various models available in the API. You can refer to the Models documentation to understand what models are available and the differences between them.

List models

get https://api.openai.com/v1/models

Lists the currently available models, and provides basic information about each one such as the owner and availability.

Returns

1 2 curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 { "object": "list", "data": [ { "id": "model-id-0", "object": "model", "created": 1686935002, "owned_by": "organization-owner" }, { "id": "model-id-1", "object": "model", "created": 1686935002, "owned_by": "organization-owner", }, { "id": "model-id-2", "object": "model", "created": 1686935002, "owned_by": "openai" }, ], "object": "list" }

Retrieve model

get https://api.openai.com/v1/models/{model}

Retrieves a model instance, providing basic information about the model such as the owner and permissioning.

Path parameters

The ID of the model to use for this request

Returns

The model object matching the specified ID.

1 2 curl https://api.openai.com/v1/models/gpt-3.5-turbo-instruct \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 6 { "id": "gpt-3.5-turbo-instruct", "object": "model", "created": 1686935002, "owned_by": "openai" }

Delete a fine-tuned model

delete https://api.openai.com/v1/models/{model}

Delete a fine-tuned model. You must have the Owner role in your organization to delete a model.

Path parameters

Returns

1 2 3 curl https://api.openai.com/v1/models/ft:gpt-3.5-turbo:acemeco:suffix:abc123 \ -X DELETE \ -H "Authorization: Bearer $OPENAI_API_KEY"
1 2 3 4 5 { "id": "ft:gpt-3.5-turbo:acemeco:suffix:abc123", "object": "model", "deleted": true }

The model object

Describes an OpenAI model offering that can be used with the API.

The model identifier, which can be referenced in the API endpoints.

The Unix timestamp (in seconds) when the model was created.

The object type, which is always "model".

The organization that owns the model.

1 2 3 4 5 6 { "id": "davinci", "object": "model", "created": 1686935002, "owned_by": "openai" }

Moderations

Given some input text, outputs if the model classifies it as potentially harmful across several categories.

Related guide: Moderations

Create moderation

post https://api.openai.com/v1/moderations

Classifies if text is potentially harmful.

Request body

The input text to classify

Two content moderations models are available: and .text-moderation-stable``text-moderation-latest

The default is which will be automatically upgraded over time. This ensures you are always using our most accurate model. If you use , we will provide advanced notice before updating the model. Accuracy of may be slightly lower than for .text-moderation-latest``text-moderation-stable``text-moderation-stable``text-moderation-latest

Returns

1 2 3 4 5 6 curl https://api.openai.com/v1/moderations \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "input": "I want to kill them." }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 { "id": "modr-XXXXX", "model": "text-moderation-005", "results": [ { "flagged": true, "categories": { "sexual": false, "hate": false, "harassment": false, "self-harm": false, "sexual/minors": false, "hate/threatening": false, "violence/graphic": false, "self-harm/intent": false, "self-harm/instructions": false, "harassment/threatening": true, "violence": true, }, "category_scores": { "sexual": 1.2282071e-06, "hate": 0.010696256, "harassment": 0.29842457, "self-harm": 1.5236925e-08, "sexual/minors": 5.7246268e-08, "hate/threatening": 0.0060676364, "violence/graphic": 4.435014e-06, "self-harm/intent": 8.098441e-10, "self-harm/instructions": 2.8498655e-11, "harassment/threatening": 0.63055265, "violence": 0.99011886, } } ] }

The moderation object

Represents if a given text input is potentially harmful.

The unique identifier for the moderation request.

The model used to generate the moderation results.

A list of moderation objects.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 { "id": "modr-XXXXX", "model": "text-moderation-005", "results": [ { "flagged": true, "categories": { "sexual": false, "hate": false, "harassment": false, "self-harm": false, "sexual/minors": false, "hate/threatening": false, "violence/graphic": false, "self-harm/intent": false, "self-harm/instructions": false, "harassment/threatening": true, "violence": true, }, "category_scores": { "sexual": 1.2282071e-06, "hate": 0.010696256, "harassment": 0.29842457, "self-harm": 1.5236925e-08, "sexual/minors": 5.7246268e-08, "hate/threatening": 0.0060676364, "violence/graphic": 4.435014e-06, "self-harm/intent": 8.098441e-10, "self-harm/instructions": 2.8498655e-11, "harassment/threatening": 0.63055265, "violence": 0.99011886, } } ] }

post https://api.openai.com/v1/assistants

Create an assistant with a model and instructions.

Request body

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

The name of the assistant. The maximum length is 256 characters.

The description of the assistant. The maximum length is 512 characters.

The system instructions that the assistant uses. The maximum length is 256,000 characters.

A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types , , or .code_interpreter``file_search``function

A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 8 9 10 curl "https://api.openai.com/v1/assistants" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", "name": "Math Tutor", "tools": [{"type": "code_interpreter"}], "model": "gpt-4-turbo" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "asst_abc123", "object": "assistant", "created_at": 1698984975, "name": "Math Tutor", "description": null, "model": "gpt-4-turbo", "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", "tools": [ { "type": "code_interpreter" } ], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

get https://api.openai.com/v1/assistants

Returns a list of assistants.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl "https://api.openai.com/v1/assistants?order=desc&limit=20" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 { "object": "list", "data": [ { "id": "asst_abc123", "object": "assistant", "created_at": 1698982736, "name": "Coding Tutor", "description": null, "model": "gpt-4-turbo", "instructions": "You are a helpful assistant designed to make me better at coding!", "tools": [], "tool_resources": {}, "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }, { "id": "asst_abc456", "object": "assistant", "created_at": 1698982718, "name": "My Assistant", "description": null, "model": "gpt-4-turbo", "instructions": "You are a helpful assistant designed to make me better at coding!", "tools": [], "tool_resources": {}, "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }, { "id": "asst_abc789", "object": "assistant", "created_at": 1698982643, "name": null, "description": null, "model": "gpt-4-turbo", "instructions": null, "tools": [], "tool_resources": {}, "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" } ], "first_id": "asst_abc123", "last_id": "asst_abc789", "has_more": false }

get https://api.openai.com/v1/assistants/{assistant\_id}

Retrieves an assistant.

Path parameters

The ID of the assistant to retrieve.

Returns

The assistant object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/assistants/asst_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "asst_abc123", "object": "assistant", "created_at": 1699009709, "name": "HR Helper", "description": null, "model": "gpt-4-turbo", "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies.", "tools": [ { "type": "file_search" } ], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

post https://api.openai.com/v1/assistants/{assistant\_id}

Modifies an assistant.

Path parameters

The ID of the assistant to modify.

Request body

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

The name of the assistant. The maximum length is 256 characters.

The description of the assistant. The maximum length is 512 characters.

The system instructions that the assistant uses. The maximum length is 256,000 characters.

A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types , , or .code_interpreter``file_search``function

A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 8 9 curl https://api.openai.com/v1/assistants/asst_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", "tools": [{"type": "file_search"}], "model": "gpt-4-turbo" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 { "id": "asst_123", "object": "assistant", "created_at": 1699009709, "name": "HR Helper", "description": null, "model": "gpt-4-turbo", "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", "tools": [ { "type": "file_search" } ], "tool_resources": { "file_search": { "vector_store_ids": [] } }, "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

delete https://api.openai.com/v1/assistants/{assistant\_id}

Delete an assistant.

Path parameters

The ID of the assistant to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/assistants/asst_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -X DELETE
1 2 3 4 5 { "id": "asst_abc123", "object": "assistant.deleted", "deleted": true }

Represents an that can call the model and use tools.assistant

The identifier, which can be referenced in API endpoints.

The object type, which is always .assistant

The Unix timestamp (in seconds) for when the assistant was created.

The name of the assistant. The maximum length is 256 characters.

The description of the assistant. The maximum length is 512 characters.

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

The system instructions that the assistant uses. The maximum length is 256,000 characters.

A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types , , or .code_interpreter``file_search``function

A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "asst_abc123", "object": "assistant", "created_at": 1698984975, "name": "Math Tutor", "description": null, "model": "gpt-4-turbo", "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", "tools": [ { "type": "code_interpreter" } ], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

Create threads that assistants can interact with.

Related guide: Assistants

post https://api.openai.com/v1/threads

Create a thread.

Request body

A list of messages to start the thread with.

A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/threads \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -d ''
1 2 3 4 5 6 7 { "id": "thread_abc123", "object": "thread", "created_at": 1699012949, "metadata": {}, "tool_resources": {} }

get https://api.openai.com/v1/threads/{thread\_id}

Retrieves a thread.

Path parameters

The ID of the thread to retrieve.

Returns

The thread object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 { "id": "thread_abc123", "object": "thread", "created_at": 1699014083, "metadata": {}, "tool_resources": { "code_interpreter": { "file_ids": [] } } }

post https://api.openai.com/v1/threads/{thread\_id}

Modifies a thread.

Path parameters

The ID of the thread to modify. Only the can be modified.metadata

Request body

A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

The modified thread object matching the specified ID.

1 2 3 4 5 6 7 8 9 10 curl https://api.openai.com/v1/threads/thread_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "metadata": { "modified": "true", "user": "abc123" } }'
1 2 3 4 5 6 7 8 9 10 { "id": "thread_abc123", "object": "thread", "created_at": 1699014083, "metadata": { "modified": "true", "user": "abc123" }, "tool_resources": {} }

delete https://api.openai.com/v1/threads/{thread\_id}

Delete a thread.

Path parameters

The ID of the thread to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/threads/thread_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -X DELETE
1 2 3 4 5 { "id": "thread_abc123", "object": "thread.deleted", "deleted": true }

Represents a thread that contains messages.

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread

The Unix timestamp (in seconds) for when the thread was created.

A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 { "id": "thread_abc123", "object": "thread", "created_at": 1698107661, "metadata": {} }

Create messages within threads

Related guide: Assistants

post https://api.openai.com/v1/threads/{thread\_id}/messages

Create a message.

Path parameters

The ID of the thread to create a message for.

Request body

The role of the entity that is creating the message. Allowed values include:

  • user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
  • assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.

A list of files attached to the message, and the tools they should be added to.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 6 7 8 curl https://api.openai.com/v1/threads/thread_abc123/messages \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "role": "user", "content": "How does AI work? Explain it in simple terms." }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "msg_abc123", "object": "thread.message", "created_at": 1713226573, "assistant_id": null, "thread_id": "thread_abc123", "run_id": null, "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "attachments": [], "metadata": {} }

get https://api.openai.com/v1/threads/{thread\_id}/messages

Returns a list of messages for a given thread.

Path parameters

The ID of the thread the messages belong to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Filter messages by the run ID that generated them.

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/messages \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 { "object": "list", "data": [ { "id": "msg_abc123", "object": "thread.message", "created_at": 1699016383, "assistant_id": null, "thread_id": "thread_abc123", "run_id": null, "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "attachments": [], "metadata": {} }, { "id": "msg_abc456", "object": "thread.message", "created_at": 1699016383, "assistant_id": null, "thread_id": "thread_abc123", "run_id": null, "role": "user", "content": [ { "type": "text", "text": { "value": "Hello, what is AI?", "annotations": [] } } ], "attachments": [], "metadata": {} } ], "first_id": "msg_abc123", "last_id": "msg_abc456", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}

Retrieve a message.

Path parameters

The ID of the thread to which this message belongs.

The ID of the message to retrieve.

Returns

The message object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "msg_abc123", "object": "thread.message", "created_at": 1699017614, "assistant_id": null, "thread_id": "thread_abc123", "run_id": null, "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "attachments": [], "metadata": {} }

post https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}

Modifies a message.

Path parameters

The ID of the thread to which this message belongs.

The ID of the message to modify.

Request body

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 6 7 8 9 10 curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "metadata": { "modified": "true", "user": "abc123" } }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 { "id": "msg_abc123", "object": "thread.message", "created_at": 1699017614, "assistant_id": null, "thread_id": "thread_abc123", "run_id": null, "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "file_ids": [], "metadata": { "modified": "true", "user": "abc123" } }

delete https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}

Deletes a message.

Path parameters

The ID of the thread to which this message belongs.

The ID of the message to delete.

Returns

1 2 3 4 curl -X DELETE https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 { "id": "msg_abc123", "object": "thread.message.deleted", "deleted": true }

Represents a message within a thread.

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread.message

The Unix timestamp (in seconds) for when the message was created.

The thread ID that this message belongs to.

The status of the message, which can be either , , or .in_progress``incomplete``completed

On an incomplete message, details about why the message is incomplete.

The Unix timestamp (in seconds) for when the message was completed.

The Unix timestamp (in seconds) for when the message was marked as incomplete.

The entity that produced the message. One of or .user``assistant

The content of the message in array of text and/or images.

If applicable, the ID of the assistant that authored this message.

The ID of the run associated with the creation of this message. Value is when messages are created manually using the create message or create thread endpoints.null

A list of files attached to the message, and the tools they were added to.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "msg_abc123", "object": "thread.message", "created_at": 1698983503, "thread_id": "thread_abc123", "role": "assistant", "content": [ { "type": "text", "text": { "value": "Hi! How can I help you today?", "annotations": [] } } ], "assistant_id": "asst_abc123", "run_id": "run_abc123", "attachments": [], "metadata": {} }

Represents an execution run on a thread.

Related guide: Assistants

post https://api.openai.com/v1/threads/{thread\_id}/runs

Create a run.

Path parameters

The ID of the thread to run.

Request body

The ID of the assistant to use to execute this run.

The ID of the Model to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used.

Overrides the instructions of the assistant. This is useful for modifying the behavior on a per-run basis.

Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions.

Adds additional messages to the thread before creating the run.

Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

If , returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a message.true``data: [DONE]

The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status . See for more info.incomplete``incomplete_details

The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status . See for more info.incomplete``incomplete_details

Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run.

Controls which (if any) tool is called by the model. means the model will not call any tools and instead generates a message. is the default value and means the model can pick between generating a message or calling one or more tools. means the model must call one or more tools before responding to the user. Specifying a particular tool like or forces the model to call that tool.none``auto``required``{"type": "file_search"}``{"type": "function", "function": {"name": "my_function"}}

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/threads/thread_abc123/runs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "assistant_id": "asst_abc123" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 { "id": "run_abc123", "object": "thread.run", "created_at": 1699063290, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "queued", "started_at": 1699063290, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699063291, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "metadata": {}, "usage": null, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

post https://api.openai.com/v1/threads/runs

Create a thread and run it in one request.

Request body

The ID of the assistant to use to execute this run.

The ID of the Model to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used.

Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis.

Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.

A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the tool requires a list of file IDs, while the tool requires a list of vector store IDs.code_interpreter``file_search

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

If , returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a message.true``data: [DONE]

The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status . See for more info.incomplete``incomplete_details

The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status . See for more info.incomplete``incomplete_details

Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run.

Controls which (if any) tool is called by the model. means the model will not call any tools and instead generates a message. is the default value and means the model can pick between generating a message or calling one or more tools. means the model must call one or more tools before responding to the user. Specifying a particular tool like or forces the model to call that tool.none``auto``required``{"type": "file_search"}``{"type": "function", "function": {"name": "my_function"}}

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 8 9 10 11 12 curl https://api.openai.com/v1/threads/runs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "assistant_id": "asst_abc123", "thread": { "messages": [ {"role": "user", "content": "Explain deep learning to a 5 year old."} ] } }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 { "id": "run_abc123", "object": "thread.run", "created_at": 1699076792, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "queued", "started_at": null, "expires_at": 1699077392, "cancelled_at": null, "failed_at": null, "completed_at": null, "required_action": null, "last_error": null, "model": "gpt-4-turbo", "instructions": "You are a helpful assistant.", "tools": [], "tool_resources": {}, "metadata": {}, "temperature": 1.0, "top_p": 1.0, "max_completion_tokens": null, "max_prompt_tokens": null, "truncation_strategy": { "type": "auto", "last_messages": null }, "incomplete_details": null, "usage": null, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

get https://api.openai.com/v1/threads/{thread\_id}/runs

Returns a list of runs belonging to a thread.

Path parameters

The ID of the thread the run belongs to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 { "object": "list", "data": [ { "id": "run_abc123", "object": "thread.run", "created_at": 1699075072, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699075072, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699075073, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "tool_resources": { "code_interpreter": { "file_ids": [ "file-abc123", "file-abc456" ] } }, "metadata": {}, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }, { "id": "run_abc456", "object": "thread.run", "created_at": 1699063290, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699063290, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699063291, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "tool_resources": { "code_interpreter": { "file_ids": [ "file-abc123", "file-abc456" ] } }, "metadata": {}, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true } ], "first_id": "run_abc123", "last_id": "run_abc456", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}

Retrieves a run.

Path parameters

The ID of the thread that was run.

The ID of the run to retrieve.

Returns

The run object matching the specified ID.

1 2 3 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 { "id": "run_abc123", "object": "thread.run", "created_at": 1699075072, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699075072, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699075073, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "metadata": {}, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

post https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}

Modifies a run.

Path parameters

The ID of the thread that was run.

The ID of the run to modify.

Request body

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

The modified run object matching the specified ID.

1 2 3 4 5 6 7 8 9 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "metadata": { "user_id": "user_abc123" } }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 { "id": "run_abc123", "object": "thread.run", "created_at": 1699075072, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699075072, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699075073, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "tool_resources": { "code_interpreter": { "file_ids": [ "file-abc123", "file-abc456" ] } }, "metadata": { "user_id": "user_abc123" }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

post https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/submit\_tool\_outputs

When a run has the and is , this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.status: "requires_action"``required_action.type``submit_tool_outputs

Path parameters

Request body

Returns

The modified run object matching the specified ID.

1 2 3 4 5 6 7 8 9 10 11 12 curl https://api.openai.com/v1/threads/thread_123/runs/run_123/submit_tool_outputs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "tool_outputs": [ { "tool_call_id": "call_001", "output": "70 degrees and sunny." } ] }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 { "id": "run_123", "object": "thread.run", "created_at": 1699075592, "assistant_id": "asst_123", "thread_id": "thread_123", "status": "queued", "started_at": 1699075592, "expires_at": 1699076192, "cancelled_at": null, "failed_at": null, "completed_at": null, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], "metadata": {}, "usage": null, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

post https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/cancel

Cancels a run that is .in_progress

Path parameters

The ID of the thread to which this run belongs.

The ID of the run to cancel.

Returns

The modified run object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/cancel \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v2" \ -X POST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 { "id": "run_abc123", "object": "thread.run", "created_at": 1699076126, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "cancelling", "started_at": 1699076126, "expires_at": 1699076726, "cancelled_at": null, "failed_at": null, "completed_at": null, "last_error": null, "model": "gpt-4-turbo", "instructions": "You summarize books.", "tools": [ { "type": "file_search" } ], "tool_resources": { "file_search": { "vector_store_ids": ["vs_123"] } }, "metadata": {}, "usage": null, "temperature": 1.0, "top_p": 1.0, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

Represents an execution run on a thread.

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread.run

The Unix timestamp (in seconds) for when the run was created.

The ID of the thread that was executed on as a part of this run.

The ID of the assistant used for execution of this run.

The status of the run, which can be either , , , , , , , , or .queued``in_progress``requires_action``cancelling``cancelled``failed``completed``incomplete``expired

Details on the action required to continue the run. Will be if no action is required.null

The last error associated with this run. Will be if there are no errors.null

The Unix timestamp (in seconds) for when the run will expire.

The Unix timestamp (in seconds) for when the run was started.

The Unix timestamp (in seconds) for when the run was cancelled.

The Unix timestamp (in seconds) for when the run failed.

The Unix timestamp (in seconds) for when the run was completed.

Details on why the run is incomplete. Will be if the run is not incomplete.null

The model that the assistant used for this run.

The instructions that the assistant used for this run.

The list of tools that the assistant used for this run.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Usage statistics related to the run. This value will be if the run is not in a terminal state (i.e. , , etc.).null``in_progress``queued

The sampling temperature used for this run. If not set, defaults to 1.

The nucleus sampling value used for this run. If not set, defaults to 1.

The maximum number of prompt tokens specified to have been used over the course of the run.

The maximum number of completion tokens specified to have been used over the course of the run.

Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run.

Controls which (if any) tool is called by the model. means the model will not call any tools and instead generates a message. is the default value and means the model can pick between generating a message or calling one or more tools. means the model must call one or more tools before responding to the user. Specifying a particular tool like or forces the model to call that tool.none``auto``required``{"type": "file_search"}``{"type": "function", "function": {"name": "my_function"}}

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 { "id": "run_abc123", "object": "thread.run", "created_at": 1698107661, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699073476, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699073498, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "tools": [{"type": "file_search"}, {"type": "code_interpreter"}], "metadata": {}, "incomplete_details": null, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto", "parallel_tool_calls": true }

Represents the steps (model and tool calls) taken during the run.

Related guide: Assistants

get https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/steps

Returns a list of run steps belonging to a run.

Path parameters

The ID of the thread the run and run steps belong to.

The ID of the run the run steps belong to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/steps \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 { "object": "list", "data": [ { "id": "step_abc123", "object": "thread.run.step", "created_at": 1699063291, "run_id": "run_abc123", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "type": "message_creation", "status": "completed", "cancelled_at": null, "completed_at": 1699063291, "expired_at": null, "failed_at": null, "last_error": null, "step_details": { "type": "message_creation", "message_creation": { "message_id": "msg_abc123" } }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 } } ], "first_id": "step_abc123", "last_id": "step_abc456", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/steps/{step\_id}

Retrieves a run step.

Path parameters

The ID of the thread to which the run and run step belongs.

The ID of the run to which the run step belongs.

The ID of the run step to retrieve.

Returns

The run step object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/steps/step_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 { "id": "step_abc123", "object": "thread.run.step", "created_at": 1699063291, "run_id": "run_abc123", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "type": "message_creation", "status": "completed", "cancelled_at": null, "completed_at": 1699063291, "expired_at": null, "failed_at": null, "last_error": null, "step_details": { "type": "message_creation", "message_creation": { "message_id": "msg_abc123" } }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 } }

Represents a step in execution of a run.

The identifier of the run step, which can be referenced in API endpoints.

The object type, which is always .thread.run.step

The Unix timestamp (in seconds) for when the run step was created.

The ID of the assistant associated with the run step.

The ID of the thread that was run.

The ID of the run that this run step is a part of.

The type of run step, which can be either or .message_creation``tool_calls

The status of the run step, which can be either , , , , or .in_progress``cancelled``failed``completed``expired

The details of the run step.

The last error associated with this run step. Will be if there are no errors.null

The Unix timestamp (in seconds) for when the run step expired. A step is considered expired if the parent run is expired.

The Unix timestamp (in seconds) for when the run step was cancelled.

The Unix timestamp (in seconds) for when the run step failed.

The Unix timestamp (in seconds) for when the run step completed.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Usage statistics related to the run step. This value will be while the run step's status is .null``in_progress

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 { "id": "step_abc123", "object": "thread.run.step", "created_at": 1699063291, "run_id": "run_abc123", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "type": "message_creation", "status": "completed", "cancelled_at": null, "completed_at": 1699063291, "expired_at": null, "failed_at": null, "last_error": null, "step_details": { "type": "message_creation", "message_creation": { "message_id": "msg_abc123" } }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 } }

Vector stores are used to store files for use by the tool.file_search

Related guide: File Search

post https://api.openai.com/v1/vector\_stores

Create a vector store.

Request body

A list of File IDs that the vector store should use. Useful for tools like that can access files.file_search

The name of the vector store.

The expiration policy for a vector store.

The chunking strategy used to chunk the file(s). If not set, will use the strategy. Only applicable if is non-empty.auto``file_ids

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/vector_stores \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" -d '{ "name": "Support FAQ" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "id": "vs_abc123", "object": "vector_store", "created_at": 1699061776, "name": "Support FAQ", "bytes": 139920, "file_counts": { "in_progress": 0, "completed": 3, "failed": 0, "cancelled": 0, "total": 3 } }

get https://api.openai.com/v1/vector\_stores

Returns a list of vector stores.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/vector_stores \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 { "object": "list", "data": [ { "id": "vs_abc123", "object": "vector_store", "created_at": 1699061776, "name": "Support FAQ", "bytes": 139920, "file_counts": { "in_progress": 0, "completed": 3, "failed": 0, "cancelled": 0, "total": 3 } }, { "id": "vs_abc456", "object": "vector_store", "created_at": 1699061776, "name": "Support FAQ v2", "bytes": 139920, "file_counts": { "in_progress": 0, "completed": 3, "failed": 0, "cancelled": 0, "total": 3 } } ], "first_id": "vs_abc123", "last_id": "vs_abc456", "has_more": false }

get https://api.openai.com/v1/vector\_stores/{vector\_store\_id}

Retrieves a vector store.

Path parameters

The ID of the vector store to retrieve.

Returns

1 2 3 4 curl https://api.openai.com/v1/vector_stores/vs_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 { "id": "vs_abc123", "object": "vector_store", "created_at": 1699061776 }

post https://api.openai.com/v1/vector\_stores/{vector\_store\_id}

Modifies a vector store.

Path parameters

The ID of the vector store to modify.

Request body

The name of the vector store.

The expiration policy for a vector store.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/vector_stores/vs_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" -d '{ "name": "Support FAQ" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "id": "vs_abc123", "object": "vector_store", "created_at": 1699061776, "name": "Support FAQ", "bytes": 139920, "file_counts": { "in_progress": 0, "completed": 3, "failed": 0, "cancelled": 0, "total": 3 } }

delete https://api.openai.com/v1/vector\_stores/{vector\_store\_id}

Delete a vector store.

Path parameters

The ID of the vector store to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/vector_stores/vs_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -X DELETE
1 2 3 4 5 { id: "vs_abc123", object: "vector_store.deleted", deleted: true }

A vector store is a collection of processed files can be used by the tool.file_search

The identifier, which can be referenced in API endpoints.

The object type, which is always .vector_store

The Unix timestamp (in seconds) for when the vector store was created.

The name of the vector store.

The total number of bytes used by the files in the vector store.

The status of the vector store, which can be either , , or . A status of indicates that the vector store is ready for use.expired``in_progress``completed``completed

The expiration policy for a vector store.

The Unix timestamp (in seconds) for when the vector store will expire.

The Unix timestamp (in seconds) for when the vector store was last active.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "vs_123", "object": "vector_store", "created_at": 1698107661, "usage_bytes": 123456, "last_active_at": 1698107661, "name": "my_vector_store", "status": "completed", "file_counts": { "in_progress": 0, "completed": 100, "cancelled": 0, "failed": 0, "total": 100 }, "metadata": {}, "last_used_at": 1698107661 }

Vector store files represent files inside a vector store.

Related guide: File Search

post https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/files

Create a vector store file by attaching a File to a vector store.

Path parameters

The ID of the vector store for which to create a File.

Request body

A File ID that the vector store should use. Useful for tools like that can access files.file_search

The chunking strategy used to chunk the file(s). If not set, will use the strategy.auto

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/vector_stores/vs_abc123/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "file_id": "file-abc123" }'
1 2 3 4 5 6 7 8 9 { "id": "file-abc123", "object": "vector_store.file", "created_at": 1699061776, "usage_bytes": 1234, "vector_store_id": "vs_abcd", "status": "completed", "last_error": null }

get https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/files

Returns a list of vector store files.

Path parameters

The ID of the vector store that the files belong to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Filter by file status. One of , , , .in_progress``completed``failed``cancelled

Returns

1 2 3 4 curl https://api.openai.com/v1/vector_stores/vs_abc123/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "object": "list", "data": [ { "id": "file-abc123", "object": "vector_store.file", "created_at": 1699061776, "vector_store_id": "vs_abc123" }, { "id": "file-abc456", "object": "vector_store.file", "created_at": 1699061776, "vector_store_id": "vs_abc123" } ], "first_id": "file-abc123", "last_id": "file-abc456", "has_more": false }

get https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/files/{file\_id}

Retrieves a vector store file.

Path parameters

The ID of the vector store that the file belongs to.

The ID of the file being retrieved.

Returns

1 2 3 4 curl https://api.openai.com/v1/vector_stores/vs_abc123/files/file-abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 { "id": "file-abc123", "object": "vector_store.file", "created_at": 1699061776, "vector_store_id": "vs_abcd", "status": "completed", "last_error": null }

delete https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/files/{file\_id}

Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the delete file endpoint.

Path parameters

The ID of the vector store that the file belongs to.

The ID of the file to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/vector_stores/vs_abc123/files/file-abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -X DELETE
1 2 3 4 5 { id: "file-abc123", object: "vector_store.file.deleted", deleted: true }

A list of files attached to a vector store.

The identifier, which can be referenced in API endpoints.

The object type, which is always .vector_store.file

The total vector store usage in bytes. Note that this may be different from the original file size.

The Unix timestamp (in seconds) for when the vector store file was created.

The status of the vector store file, which can be either , , , or . The status indicates that the vector store file is ready for use.in_progress``completed``cancelled``failed``completed

The last error associated with this vector store file. Will be if there are no errors.null

The strategy used to chunk the file.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 { "id": "file-abc123", "object": "vector_store.file", "usage_bytes": 1234, "created_at": 1698107661, "vector_store_id": "vs_abc123", "status": "completed", "last_error": null, "chunking_strategy": { "type": "static", "static": { "max_chunk_size_tokens": 800, "chunk_overlap_tokens": 400 } } }

Vector store file batches represent operations to add multiple files to a vector store.

Related guide: File Search

post https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/file\_batches

Create a vector store file batch.

Path parameters

The ID of the vector store for which to create a File Batch.

Request body

A list of File IDs that the vector store should use. Useful for tools like that can access files.file_search

The chunking strategy used to chunk the file(s). If not set, will use the strategy.auto

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/vector_stores/vs_abc123/file_batches \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json \ -H "OpenAI-Beta: assistants=v2" \ -d '{ "file_ids": ["file-abc123", "file-abc456"] }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "id": "vsfb_abc123", "object": "vector_store.file_batch", "created_at": 1699061776, "vector_store_id": "vs_abc123", "status": "in_progress", "file_counts": { "in_progress": 1, "completed": 1, "failed": 0, "cancelled": 0, "total": 0, } }

get https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/file\_batches/{batch\_id}

Retrieves a vector store file batch.

Path parameters

The ID of the vector store that the file batch belongs to.

The ID of the file batch being retrieved.

Returns

1 2 3 4 curl https://api.openai.com/v1/vector_stores/vs_abc123/files_batches/vsfb_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "id": "vsfb_abc123", "object": "vector_store.file_batch", "created_at": 1699061776, "vector_store_id": "vs_abc123", "status": "in_progress", "file_counts": { "in_progress": 1, "completed": 1, "failed": 0, "cancelled": 0, "total": 0, } }

post https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/file\_batches/{batch\_id}/cancel

Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible.

Path parameters

The ID of the vector store that the file batch belongs to.

The ID of the file batch to cancel.

Returns

The modified vector store file batch object.

1 2 3 4 5 curl https://api.openai.com/v1/vector_stores/vs_abc123/files_batches/vsfb_abc123/cancel \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2" \ -X POST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "id": "vsfb_abc123", "object": "vector_store.file_batch", "created_at": 1699061776, "vector_store_id": "vs_abc123", "status": "cancelling", "file_counts": { "in_progress": 12, "completed": 3, "failed": 0, "cancelled": 0, "total": 15, } }

get https://api.openai.com/v1/vector\_stores/{vector\_store\_id}/file\_batches/{batch\_id}/files

Returns a list of vector store files in a batch.

Path parameters

The ID of the vector store that the files belong to.

The ID of the file batch that the files belong to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Filter by file status. One of , , , .in_progress``completed``failed``cancelled

Returns

1 2 3 4 curl https://api.openai.com/v1/vector_stores/vs_abc123/files_batches/vsfb_abc123/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v2"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "object": "list", "data": [ { "id": "file-abc123", "object": "vector_store.file", "created_at": 1699061776, "vector_store_id": "vs_abc123" }, { "id": "file-abc456", "object": "vector_store.file", "created_at": 1699061776, "vector_store_id": "vs_abc123" } ], "first_id": "file-abc123", "last_id": "file-abc456", "has_more": false }

A batch of files attached to a vector store.

The identifier, which can be referenced in API endpoints.

The object type, which is always .vector_store.file_batch

The Unix timestamp (in seconds) for when the vector store files batch was created.

The status of the vector store files batch, which can be either , , or .in_progress``completed``cancelled``failed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "id": "vsfb_123", "object": "vector_store.files_batch", "created_at": 1698107661, "vector_store_id": "vs_abc123", "status": "completed", "file_counts": { "in_progress": 0, "completed": 100, "failed": 0, "cancelled": 0, "total": 100 } }

Represents a message delta i.e. any changed fields on a message during streaming.

The identifier of the message, which can be referenced in API endpoints.

The object type, which is always .thread.message.delta

The delta containing the fields that have changed on the Message.

1 2 3 4 5 6 7 8 9 10 11 12 13 { "id": "msg_123", "object": "thread.message.delta", "delta": { "content": [ { "index": 0, "type": "text", "text": { "value": "Hello", "annotations": [] } } ] } }

Represents a run step delta i.e. any changed fields on a run step during streaming.

The identifier of the run step, which can be referenced in API endpoints.

The object type, which is always .thread.run.step.delta

The delta containing the fields that have changed on the run step.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 { "id": "step_123", "object": "thread.run.step.delta", "delta": { "step_details": { "type": "tool_calls", "tool_calls": [ { "index": 0, "id": "call_123", "type": "code_interpreter", "code_interpreter": { "input": "", "outputs": [] } } ] } } }

Represents an event emitted when streaming a Run.

Each event in a server-sent events stream has an and property:event``data

event: thread.created data: {"id": "thread_123", "object": "thread", ...}

We emit events whenever a new object is created, transitions to a new state, or is being streamed in parts (deltas). For example, we emit when a new run is created, when a run completes, and so on. When an Assistant chooses to create a message during a run, we emit a , a event, many events, and finally a event.thread.run.created``thread.run.completed``thread.message.created event``thread.message.in_progress``thread.message.delta``thread.message.completed

We may add additional events over time, so we recommend handling unknown events gracefully in your code. See the Assistants API quickstart to learn how to integrate the Assistants API with streaming.

Occurs when a new thread is created.

Occurs when a new run is created.

Occurs when a run moves to a status.queued

Occurs when a run moves to an status.in_progress

Occurs when a run moves to a status.requires_action

Occurs when a run is completed.

Occurs when a run ends with status .incomplete

Occurs when a run moves to a status.cancelling

Occurs when a run is cancelled.

Occurs when a run expires.

Occurs when a run step moves to an state.in_progress

Occurs when parts of a run step are being streamed.

Occurs when a message moves to an state.in_progress

Occurs when parts of a Message are being streamed.

Occurs when a message is completed.

Occurs when a message ends before it is completed.

Occurs when an error occurs. This can happen due to an internal server error or a timeout.

Occurs when a stream ends.

Given a prompt, the model will return one or more predicted completions along with the probabilities of alternative tokens at each position. Most developer should use our Chat Completions API to leverage our best and newest models.

post https://api.openai.com/v1/completions

Creates a completion for the provided prompt and parameters.

Request body

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.

Note that


<|endoftext|>

is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.

Generates completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.best_of

When used with , controls the number of candidate completions and specifies how many to return – must be greater than .n``best_of``n``best_of``n

Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for and .max_tokens``stop

Echo back the prompt in addition to the completion

Modify the likelihood of specified tokens appearing in the completion.

Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this tokenizer tool to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.

As an example, you can pass to prevent the

<|endoftext|> token from being generated.

{"50256": -100}

Include the log probabilities on the most likely output tokens, as well the chosen tokens. For example, if is 5, the API will return a list of the 5 most likely tokens. The API will always return the of the sampled token, so there may be up to elements in the response.logprobs``logprobs``logprob``logprobs+1

The maximum value for is 5.logprobs

The maximum number of tokens that can be generated in the completion.

The token count of your prompt plus cannot exceed the model's context length. Example Python code for counting tokens.max_tokens

How many completions to generate for each prompt.

Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for and .max_tokens``stop

If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same and parameters should return the same result.seed

Determinism is not guaranteed, and you should refer to the response parameter to monitor changes in the backend.system_fingerprint

Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.

Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a message. Example Python code.data: [DONE]

Options for streaming response. Only set this when you set .stream: true

The suffix that comes after a completion of inserted text.

This parameter is only supported for .gpt-3.5-turbo-instruct

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

We generally recommend altering this or but not both.top_p

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or but not both.temperature

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Returns

Returns a completion object, or a sequence of completion objects if the request is streamed.

1 2 3 4 5 6 7 8 9 curl https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-3.5-turbo-instruct", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", "object": "text_completion", "created": 1589478378, "model": "gpt-3.5-turbo-instruct", "system_fingerprint": "fp_44709d6fcb", "choices": [ { "text": "\n\nThis is indeed a test", "index": 0, "logprobs": null, "finish_reason": "length" } ], "usage": { "prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12 } }

Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).

A unique identifier for the completion.

The list of completion choices the model generated for the input prompt.

The Unix timestamp (in seconds) of when the completion was created.

The model used for completion.

This fingerprint represents the backend configuration that the model runs with.

Can be used in conjunction with the request parameter to understand when backend changes have been made that might impact determinism.seed

The object type, which is always "text_completion"

Usage statistics for the completion request.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 { "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", "object": "text_completion", "created": 1589478378, "model": "gpt-4-turbo", "choices": [ { "text": "\n\nThis is indeed a test", "index": 0, "logprobs": null, "finish_reason": "length" } ], "usage": { "prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12 } }

post https://api.openai.com/v1/assistants

Create an assistant with a model and instructions.

Request body

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them. type: string

The name of the assistant. The maximum length is 256 characters.

The description of the assistant. The maximum length is 512 characters.

The system instructions that the assistant uses. The maximum length is 256,000 characters.

A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types , , or .code_interpreter``retrieval``function

A list of file IDs attached to this assistant. There can be a maximum of 20 files attached to the assistant. Files are ordered by their creation date in ascending order.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 8 9 10 curl "https://api.openai.com/v1/assistants" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", "name": "Math Tutor", "tools": [{"type": "code_interpreter"}], "model": "gpt-4-turbo" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 { "id": "asst_abc123", "object": "assistant", "created_at": 1698984975, "name": "Math Tutor", "description": null, "model": "gpt-4-turbo", "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", "tools": [ { "type": "code_interpreter" } ], "file_ids": [], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

post https://api.openai.com/v1/assistants/{assistant\_id}/files

Create an assistant file by attaching a File to an assistant.

Path parameters

The ID of the assistant for which to create a File.

Request body

A File ID (with ) that the assistant should use. Useful for tools like and that can access files.purpose="assistants"``retrieval``code_interpreter

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/assistants/asst_abc123/files \ -H 'Authorization: Bearer $OPENAI_API_KEY"' \ -H 'Content-Type: application/json' \ -H 'OpenAI-Beta: assistants=v1' \ -d '{ "file_id": "file-abc123" }'
1 2 3 4 5 6 { "id": "file-abc123", "object": "assistant.file", "created_at": 1699055364, "assistant_id": "asst_abc123" }

get https://api.openai.com/v1/assistants

Returns a list of assistants.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl "https://api.openai.com/v1/assistants?order=desc&limit=20" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 { "object": "list", "data": [ { "id": "asst_abc123", "object": "assistant", "created_at": 1698982736, "name": "Coding Tutor", "description": null, "model": "gpt-4-turbo", "instructions": "You are a helpful assistant designed to make me better at coding!", "tools": [], "file_ids": [], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }, { "id": "asst_abc456", "object": "assistant", "created_at": 1698982718, "name": "My Assistant", "description": null, "model": "gpt-4-turbo", "instructions": "You are a helpful assistant designed to make me better at coding!", "tools": [], "file_ids": [], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }, { "id": "asst_abc789", "object": "assistant", "created_at": 1698982643, "name": null, "description": null, "model": "gpt-4-turbo", "instructions": null, "tools": [], "file_ids": [], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" } ], "first_id": "asst_abc123", "last_id": "asst_abc789", "has_more": false }

get https://api.openai.com/v1/assistants/{assistant\_id}/files

Returns a list of assistant files.

Path parameters

The ID of the assistant the file belongs to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/assistants/asst_abc123/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "object": "list", "data": [ { "id": "file-abc123", "object": "assistant.file", "created_at": 1699060412, "assistant_id": "asst_abc123" }, { "id": "file-abc456", "object": "assistant.file", "created_at": 1699060412, "assistant_id": "asst_abc123" } ], "first_id": "file-abc123", "last_id": "file-abc456", "has_more": false }

get https://api.openai.com/v1/assistants/{assistant\_id}

Retrieves an assistant.

Path parameters

The ID of the assistant to retrieve.

Returns

The assistant object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/assistants/asst_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "id": "asst_abc123", "object": "assistant", "created_at": 1699009709, "name": "HR Helper", "description": null, "model": "gpt-4-turbo", "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies.", "tools": [ { "type": "retrieval" } ], "file_ids": [ "file-abc123" ], "metadata": {} }

get https://api.openai.com/v1/assistants/{assistant\_id}/files/{file\_id}

Retrieves an AssistantFile.

Path parameters

The ID of the assistant who the file belongs to.

The ID of the file we're getting.

Returns

1 2 3 4 curl https://api.openai.com/v1/assistants/asst_abc123/files/file-abc123 \ -H 'Authorization: Bearer $OPENAI_API_KEY"' \ -H 'Content-Type: application/json' \ -H 'OpenAI-Beta: assistants=v1'
1 2 3 4 5 6 { "id": "file-abc123", "object": "assistant.file", "created_at": 1699055364, "assistant_id": "asst_abc123" }

post https://api.openai.com/v1/assistants/{assistant\_id}

Modifies an assistant.

Path parameters

The ID of the assistant to modify.

Request body

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them. type: string

The name of the assistant. The maximum length is 256 characters.

The description of the assistant. The maximum length is 512 characters.

The system instructions that the assistant uses. The maximum length is 256,000 characters.

A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types , , or .code_interpreter``retrieval``function

A list of File IDs attached to this assistant. There can be a maximum of 20 files attached to the assistant. Files are ordered by their creation date in ascending order. If a file was previously attached to the list but does not show up in the list, it will be deleted from the assistant.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 8 9 10 curl https://api.openai.com/v1/assistants/asst_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", "tools": [{"type": "retrieval"}], "model": "gpt-4-turbo", "file_ids": ["file-abc123", "file-abc456"] }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 { "id": "asst_abc123", "object": "assistant", "created_at": 1699009709, "name": "HR Helper", "description": null, "model": "gpt-4-turbo", "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", "tools": [ { "type": "retrieval" } ], "file_ids": [ "file-abc123", "file-abc456" ], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

delete https://api.openai.com/v1/assistants/{assistant\_id}

Delete an assistant.

Path parameters

The ID of the assistant to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/assistants/asst_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -X DELETE
1 2 3 4 5 { "id": "asst_abc123", "object": "assistant.deleted", "deleted": true }

delete https://api.openai.com/v1/assistants/{assistant\_id}/files/{file\_id}

Delete an assistant file.

Path parameters

The ID of the assistant that the file belongs to.

The ID of the file to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/assistants/asst_abc123/files/file-abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1" \ -X DELETE
1 2 3 4 5 { id: "file-abc123", object: "assistant.file.deleted", deleted: true }

Represents an that can call the model and use tools.assistant

The identifier, which can be referenced in API endpoints.

The object type, which is always .assistant

The Unix timestamp (in seconds) for when the assistant was created.

The name of the assistant. The maximum length is 256 characters.

The description of the assistant. The maximum length is 512 characters.

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them. type: string

The system instructions that the assistant uses. The maximum length is 256,000 characters.

A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types , , or .code_interpreter``retrieval``function

A list of file IDs attached to this assistant. There can be a maximum of 20 files attached to the assistant. Files are ordered by their creation date in ascending order.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 { "id": "asst_abc123", "object": "assistant", "created_at": 1698984975, "name": "Math Tutor", "description": null, "model": "gpt-4-turbo", "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", "tools": [ { "type": "code_interpreter" } ], "file_ids": [], "metadata": {}, "top_p": 1.0, "temperature": 1.0, "response_format": "auto" }

A list of Files attached to an .assistant

The identifier, which can be referenced in API endpoints.

The object type, which is always .assistant.file

The Unix timestamp (in seconds) for when the assistant file was created.

The assistant ID that the file is attached to.

1 2 3 4 5 6 { "id": "file-abc123", "object": "assistant.file", "created_at": 1699055364, "assistant_id": "asst_abc123" }

Create threads that assistants can interact with.

Related guide: Assistants

post https://api.openai.com/v1/threads

Create a thread.

Request body

A list of messages to start the thread with.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/threads \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -d ''
1 2 3 4 5 6 { "id": "thread_abc123", "object": "thread", "created_at": 1699012949, "metadata": {} }

get https://api.openai.com/v1/threads/{thread\_id}

Retrieves a thread.

Path parameters

The ID of the thread to retrieve.

Returns

The thread object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 { "id": "thread_abc123", "object": "thread", "created_at": 1699014083, "metadata": {} }

post https://api.openai.com/v1/threads/{thread\_id}

Modifies a thread.

Path parameters

The ID of the thread to modify. Only the can be modified.metadata

Request body

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

The modified thread object matching the specified ID.

1 2 3 4 5 6 7 8 9 10 curl https://api.openai.com/v1/threads/thread_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "metadata": { "modified": "true", "user": "abc123" } }'
1 2 3 4 5 6 7 8 9 { "id": "thread_abc123", "object": "thread", "created_at": 1699014083, "metadata": { "modified": "true", "user": "abc123" } }

delete https://api.openai.com/v1/threads/{thread\_id}

Delete a thread.

Path parameters

The ID of the thread to delete.

Returns

1 2 3 4 5 curl https://api.openai.com/v1/threads/thread_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -X DELETE
1 2 3 4 5 { "id": "thread_abc123", "object": "thread.deleted", "deleted": true }

Represents a thread that contains messages.

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread

The Unix timestamp (in seconds) for when the thread was created.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 { "id": "thread_abc123", "object": "thread", "created_at": 1698107661, "metadata": {} }

Create messages within threads

Related guide: Assistants

post https://api.openai.com/v1/threads/{thread\_id}/messages

Create a message.

Path parameters

The ID of the thread to create a message for.

Request body

The role of the entity that is creating the message. Allowed values include:

  • user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
  • assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.

The content of the message.

A list of File IDs that the message should use. There can be a maximum of 10 files attached to a message. Useful for tools like and that can access and use files.retrieval``code_interpreter

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 6 7 8 curl https://api.openai.com/v1/threads/thread_abc123/messages \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "role": "user", "content": "How does AI work? Explain it in simple terms." }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "msg_abc123", "object": "thread.message", "created_at": 1699017614, "thread_id": "thread_abc123", "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "file_ids": [], "assistant_id": null, "run_id": null, "metadata": {} }

get https://api.openai.com/v1/threads/{thread\_id}/messages

Returns a list of messages for a given thread.

Path parameters

The ID of the thread the messages belong to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Filter messages by the run ID that generated them.

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/messages \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 { "object": "list", "data": [ { "id": "msg_abc123", "object": "thread.message", "created_at": 1699016383, "thread_id": "thread_abc123", "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "file_ids": [], "assistant_id": null, "run_id": null, "metadata": {} }, { "id": "msg_abc456", "object": "thread.message", "created_at": 1699016383, "thread_id": "thread_abc123", "role": "user", "content": [ { "type": "text", "text": { "value": "Hello, what is AI?", "annotations": [] } } ], "file_ids": [ "file-abc123" ], "assistant_id": null, "run_id": null, "metadata": {} } ], "first_id": "msg_abc123", "last_id": "msg_abc456", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}/files

Returns a list of message files.

Path parameters

The ID of the thread that the message and files belong to.

The ID of the message that the files belongs to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "object": "list", "data": [ { "id": "file-abc123", "object": "thread.message.file", "created_at": 1699061776, "message_id": "msg_abc123" }, { "id": "file-abc123", "object": "thread.message.file", "created_at": 1699061776, "message_id": "msg_abc123" } ], "first_id": "file-abc123", "last_id": "file-abc123", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}

Retrieve a message.

Path parameters

The ID of the thread to which this message belongs.

The ID of the message to retrieve.

Returns

The message object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "msg_abc123", "object": "thread.message", "created_at": 1699017614, "thread_id": "thread_abc123", "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "file_ids": [], "assistant_id": null, "run_id": null, "metadata": {} }

get https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}/files/{file\_id}

Retrieves a message file.

Path parameters

The ID of the thread to which the message and File belong.

The ID of the message the file belongs to.

The ID of the file being retrieved.

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123/files/file-abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 { "id": "file-abc123", "object": "thread.message.file", "created_at": 1699061776, "message_id": "msg_abc123" }

post https://api.openai.com/v1/threads/{thread\_id}/messages/{message\_id}

Modifies a message.

Path parameters

The ID of the thread to which this message belongs.

The ID of the message to modify.

Request body

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

1 2 3 4 5 6 7 8 9 10 curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "metadata": { "modified": "true", "user": "abc123" } }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 { "id": "msg_abc123", "object": "thread.message", "created_at": 1699017614, "thread_id": "thread_abc123", "role": "user", "content": [ { "type": "text", "text": { "value": "How does AI work? Explain it in simple terms.", "annotations": [] } } ], "file_ids": [], "assistant_id": null, "run_id": null, "metadata": { "modified": "true", "user": "abc123" } }

Represents a message within a thread.

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread.message

The Unix timestamp (in seconds) for when the message was created.

The thread ID that this message belongs to.

The status of the message, which can be either , , or .in_progress``incomplete``completed

On an incomplete message, details about why the message is incomplete.

The Unix timestamp (in seconds) for when the message was completed.

The Unix timestamp (in seconds) for when the message was marked as incomplete.

The entity that produced the message. One of or .user``assistant

The content of the message in array of text and/or images.

If applicable, the ID of the assistant that authored this message.

The ID of the run associated with the creation of this message. Value is when messages are created manually using the create message or create thread endpoints.null

A list of file IDs that the assistant should use. Useful for tools like retrieval and code_interpreter that can access files. A maximum of 10 files can be attached to a message.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "id": "msg_abc123", "object": "thread.message", "created_at": 1698983503, "thread_id": "thread_abc123", "role": "assistant", "content": [ { "type": "text", "text": { "value": "Hi! How can I help you today?", "annotations": [] } } ], "file_ids": [], "assistant_id": "asst_abc123", "run_id": "run_abc123", "metadata": {} }

A list of files attached to a .message

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread.message.file

The Unix timestamp (in seconds) for when the message file was created.

1 2 3 4 5 6 7 { "id": "file-abc123", "object": "thread.message.file", "created_at": 1698107661, "message_id": "message_QLoItBbqwyAJEzlTy4y9kOMM", "file_id": "file-abc123" }

Represents an execution run on a thread.

Related guide: Assistants

post https://api.openai.com/v1/threads/{thread\_id}/runs

Create a run.

Path parameters

The ID of the thread to run.

Request body

The ID of the assistant to use to execute this run.

The ID of the Model to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used.

Overrides the instructions of the assistant. This is useful for modifying the behavior on a per-run basis.

Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions.

Adds additional messages to the thread before creating the run.

Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

If , returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a message.true``data: [DONE]

The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status . See for more info.complete``incomplete_details

The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status . See for more info.complete``incomplete_details

Controls which (if any) tool is called by the model. means the model will not call any tools and instead generates a message. is the default value and means the model can pick between generating a message or calling a tool. Specifying a particular tool like or forces the model to call that tool.none``auto``{"type": "TOOL_TYPE"}``{"type": "function", "function": {"name": "my_function"}}

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 curl https://api.openai.com/v1/threads/thread_abc123/runs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "assistant_id": "asst_abc123" }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 { "id": "run_abc123", "object": "thread.run", "created_at": 1699063290, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "queued", "started_at": 1699063290, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699063291, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "file_ids": [ "file-abc123", "file-abc456" ], "metadata": {}, "usage": null, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" }

post https://api.openai.com/v1/threads/runs

Create a thread and run it in one request.

Request body

The ID of the assistant to use to execute this run.

The ID of the Model to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used.

Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis.

Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

If , returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a message.true``data: [DONE]

The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status . See for more info.complete``incomplete_details

The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status . See for more info.complete``incomplete_details

Controls which (if any) tool is called by the model. means the model will not call any tools and instead generates a message. is the default value and means the model can pick between generating a message or calling a tool. Specifying a particular tool like or forces the model to call that tool.none``auto``{"type": "TOOL_TYPE"}``{"type": "function", "function": {"name": "my_function"}}

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

Returns

1 2 3 4 5 6 7 8 9 10 11 12 curl https://api.openai.com/v1/threads/runs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "assistant_id": "asst_abc123", "thread": { "messages": [ {"role": "user", "content": "Explain deep learning to a 5 year old."} ] } }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 { "id": "run_abc123", "object": "thread.run", "created_at": 1699076792, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "queued", "started_at": null, "expires_at": 1699077392, "cancelled_at": null, "failed_at": null, "completed_at": null, "last_error": null, "model": "gpt-4-turbo", "instructions": "You are a helpful assistant.", "tools": [], "file_ids": [], "metadata": {}, "usage": null, "temperature": 1 }

get https://api.openai.com/v1/threads/{thread\_id}/runs

Returns a list of runs belonging to a thread.

Path parameters

The ID of the thread the run belongs to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 { "object": "list", "data": [ { "id": "run_abc123", "object": "thread.run", "created_at": 1699075072, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699075072, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699075073, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "file_ids": [ "file-abc123", "file-abc456" ], "metadata": {}, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" }, { "id": "run_abc456", "object": "thread.run", "created_at": 1699063290, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699063290, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699063291, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "file_ids": [ "file-abc123", "file-abc456" ], "metadata": {}, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" } ], "first_id": "run_abc123", "last_id": "run_abc456", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/steps

Returns a list of run steps belonging to a run.

Path parameters

The ID of the thread the run and run steps belong to.

The ID of the run the run steps belong to.

Query parameters

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

Sort order by the timestamp of the objects. for ascending order and for descending order.created_at``asc``desc

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.after

A cursor for use in pagination. is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.before

Returns

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/steps \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 { "object": "list", "data": [ { "id": "step_abc123", "object": "thread.run.step", "created_at": 1699063291, "run_id": "run_abc123", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "type": "message_creation", "status": "completed", "cancelled_at": null, "completed_at": 1699063291, "expired_at": null, "failed_at": null, "last_error": null, "step_details": { "type": "message_creation", "message_creation": { "message_id": "msg_abc123" } }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 } } ], "first_id": "step_abc123", "last_id": "step_abc456", "has_more": false }

get https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}

Retrieves a run.

Path parameters

The ID of the thread that was run.

The ID of the run to retrieve.

Returns

The run object matching the specified ID.

1 2 3 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 { "id": "run_abc123", "object": "thread.run", "created_at": 1699075072, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699075072, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699075073, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "file_ids": [ "file-abc123", "file-abc456" ], "metadata": {}, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" }

get https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/steps/{step\_id}

Retrieves a run step.

Path parameters

The ID of the thread to which the run and run step belongs.

The ID of the run to which the run step belongs.

The ID of the run step to retrieve.

Returns

The run step object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/steps/step_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 { "id": "step_abc123", "object": "thread.run.step", "created_at": 1699063291, "run_id": "run_abc123", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "type": "message_creation", "status": "completed", "cancelled_at": null, "completed_at": 1699063291, "expired_at": null, "failed_at": null, "last_error": null, "step_details": { "type": "message_creation", "message_creation": { "message_id": "msg_abc123" } }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 } }

post https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}

Modifies a run.

Path parameters

The ID of the thread that was run.

The ID of the run to modify.

Request body

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

Returns

The modified run object matching the specified ID.

1 2 3 4 5 6 7 8 9 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123 \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "metadata": { "user_id": "user_abc123" } }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 { "id": "run_abc123", "object": "thread.run", "created_at": 1699075072, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699075072, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699075073, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "code_interpreter" } ], "file_ids": [ "file-abc123", "file-abc456" ], "metadata": { "user_id": "user_abc123" }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" }

post https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/submit\_tool\_outputs

When a run has the and is , this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.status: "requires_action"``required_action.type``submit_tool_outputs

Path parameters

Request body

Returns

The modified run object matching the specified ID.

1 2 3 4 5 6 7 8 9 10 11 12 curl https://api.openai.com/v1/threads/thread_123/runs/run_123/submit_tool_outputs \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -H "OpenAI-Beta: assistants=v1" \ -d '{ "tool_outputs": [ { "tool_call_id": "call_001", "output": "70 degrees and sunny." } ] }'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 { "id": "run_123", "object": "thread.run", "created_at": 1699075592, "assistant_id": "asst_123", "thread_id": "thread_123", "status": "queued", "started_at": 1699075592, "expires_at": 1699076192, "cancelled_at": null, "failed_at": null, "completed_at": null, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "incomplete_details": null, "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], "file_ids": [], "metadata": {}, "usage": null, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" }

post https://api.openai.com/v1/threads/{thread\_id}/runs/{run\_id}/cancel

Cancels a run that is .in_progress

Path parameters

The ID of the thread to which this run belongs.

The ID of the run to cancel.

Returns

The modified run object matching the specified ID.

1 2 3 4 curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/cancel \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: assistants=v1" \ -X POST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 { "id": "run_abc123", "object": "thread.run", "created_at": 1699076126, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "cancelling", "started_at": 1699076126, "expires_at": 1699076726, "cancelled_at": null, "failed_at": null, "completed_at": null, "last_error": null, "model": "gpt-4-turbo", "instructions": "You summarize books.", "tools": [ { "type": "retrieval" } ], "file_ids": [], "metadata": {}, "usage": null, "temperature": 1.0, "top_p": 1.0, }

Represents an execution run on a thread.

The identifier, which can be referenced in API endpoints.

The object type, which is always .thread.run

The Unix timestamp (in seconds) for when the run was created.

The ID of the thread that was executed on as a part of this run.

The ID of the assistant used for execution of this run.

The status of the run, which can be either , , , , , , , or .queued``in_progress``requires_action``cancelling``cancelled``failed``completed``expired

Details on the action required to continue the run. Will be if no action is required.null

The last error associated with this run. Will be if there are no errors.null

The Unix timestamp (in seconds) for when the run will expire.

The Unix timestamp (in seconds) for when the run was started.

The Unix timestamp (in seconds) for when the run was cancelled.

The Unix timestamp (in seconds) for when the run failed.

The Unix timestamp (in seconds) for when the run was completed.

Details on why the run is incomplete. Will be if the run is not incomplete.null

The model that the assistant used for this run.

The instructions that the assistant used for this run.

The list of tools that the assistant used for this run.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

The sampling temperature used for this run. If not set, defaults to 1.

The nucleus sampling value used for this run. If not set, defaults to 1.

The maximum number of prompt tokens specified to have been used over the course of the run.

The maximum number of completion tokens specified to have been used over the course of the run.

Controls which (if any) tool is called by the model. means the model will not call any tools and instead generates a message. is the default value and means the model can pick between generating a message or calling a tool. Specifying a particular tool like or forces the model to call that tool.none``auto``{"type": "TOOL_TYPE"}``{"type": "function", "function": {"name": "my_function"}}

Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since .gpt-3.5-turbo-1106

Setting to enables JSON mode, which guarantees the message the model generates is valid JSON.{ "type": "json_object" }

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if , which indicates the generation exceeded or the conversation exceeded the max context length.finish_reason="length"``max_tokens

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 { "id": "run_abc123", "object": "thread.run", "created_at": 1698107661, "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "status": "completed", "started_at": 1699073476, "expires_at": null, "cancelled_at": null, "failed_at": null, "completed_at": 1699073498, "last_error": null, "model": "gpt-4-turbo", "instructions": null, "tools": [{"type": "retrieval"}, {"type": "code_interpreter"}], "file_ids": [], "metadata": {}, "incomplete_details": null, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 }, "temperature": 1.0, "top_p": 1.0, "max_prompt_tokens": 1000, "max_completion_tokens": 1000, "truncation_strategy": { "type": "auto", "last_messages": null }, "response_format": "auto", "tool_choice": "auto" }

Represents a step in execution of a run.

The identifier of the run step, which can be referenced in API endpoints.

The object type, which is always .thread.run.step

The Unix timestamp (in seconds) for when the run step was created.

The ID of the assistant associated with the run step.

The ID of the thread that was run.

The ID of the run that this run step is a part of.

The type of run step, which can be either or .message_creation``tool_calls

The status of the run step, which can be either , , , , or .in_progress``cancelled``failed``completed``expired

The details of the run step.

The last error associated with this run step. Will be if there are no errors.null

The Unix timestamp (in seconds) for when the run step expired. A step is considered expired if the parent run is expired.

The Unix timestamp (in seconds) for when the run step was cancelled.

The Unix timestamp (in seconds) for when the run step failed.

The Unix timestamp (in seconds) for when the run step completed.

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 { "id": "step_abc123", "object": "thread.run.step", "created_at": 1699063291, "run_id": "run_abc123", "assistant_id": "asst_abc123", "thread_id": "thread_abc123", "type": "message_creation", "status": "completed", "cancelled_at": null, "completed_at": 1699063291, "expired_at": null, "failed_at": null, "last_error": null, "step_details": { "type": "message_creation", "message_creation": { "message_id": "msg_abc123" } }, "usage": { "prompt_tokens": 123, "completion_tokens": 456, "total_tokens": 579 } }

Represents a message delta i.e. any changed fields on a message during streaming.

The identifier of the message, which can be referenced in API endpoints.

The object type, which is always .thread.message.delta

The delta containing the fields that have changed on the Message.

1 2 3 4 5 6 7 8 9 10 11 12 13 { "id": "msg_123", "object": "thread.message.delta", "delta": { "content": [ { "index": 0, "type": "text", "text": { "value": "Hello", "annotations": [] } } ] } }

Represents a run step delta i.e. any changed fields on a run step during streaming.

The identifier of the run step, which can be referenced in API endpoints.

The object type, which is always .thread.run.step.delta

The delta containing the fields that have changed on the run step.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 { "id": "step_123", "object": "thread.run.step.delta", "delta": { "step_details": { "type": "tool_calls", "tool_calls": [ { "index": 0, "id": "call_123", "type": "code_interpreter", "code_interpreter": { "input": "", "outputs": [] } } ] } } }

Represents an event emitted when streaming a Run.

Each event in a server-sent events stream has an and property:event``data

event: thread.created data: {"id": "thread_123", "object": "thread", ...}

We emit events whenever a new object is created, transitions to a new state, or is being streamed in parts (deltas). For example, we emit when a new run is created, when a run completes, and so on. When an Assistant chooses to create a message during a run, we emit a , a event, many events, and finally a event.thread.run.created``thread.run.completed``thread.message.created event``thread.message.in_progress``thread.message.delta``thread.message.completed

We may add additional events over time, so we recommend handling unknown events gracefully in your code. See the Assistants API quickstart to learn how to integrate the Assistants API with streaming.

Occurs when a new thread is created.

Occurs when a new run is created.

Occurs when a run moves to a status.queued

Occurs when a run moves to an status.in_progress

Occurs when a run moves to a status.requires_action

Occurs when a run is completed.

Occurs when a run moves to a status.cancelling

Occurs when a run is cancelled.

Occurs when a run expires.

Occurs when a run step moves to an state.in_progress

Occurs when parts of a run step are being streamed.

Occurs when a message moves to an state.in_progress

Occurs when parts of a Message are being streamed.

Occurs when a message is completed.

Occurs when a message ends before it is completed.

Occurs when an error occurs. This can happen due to an internal server error or a timeout.

Occurs when a stream ends.