API Reference - OpenAI API
API Reference - OpenAI API
Search K
Introduction
GETTING STARTED
You can interact with the API through HTTP requests from any language, via our official Python bindings,
Introduction our official Node.js library, or a community-maintained library.
Authentication
To install the official Python bindings, run the following command:
Making requests
ENDPOINTS
To install the official Node.js library, run the following command in your Node.js project directory:
Audio
Fine-tuning
Files
Images Authentication
Create image
The OpenAI API uses API keys for authentication. Visit your API Keys page to retrieve the API key you'll use
Create image edit in your requests.
Create image variation
Remember that your API key is a secret! Do not share it with others or expose it in any client-side code
The image object (browsers, apps). Production requests must be routed through your own backend server where your API key
Models can be securely loaded from an environment variable or key management service.
Moderations All API requests should include your API key in an Authorization HTTP header as follows:
BETA
Authorization: Bearer OPENAI_API_KEY
Assistants
Threads
Organization (optional)
Messages
For users who belong to multiple organizations, you can pass a header to specify which organization is used
Runs
for an API request. Usage from these API requests will count as usage for the specified organization.
Completions
1 curl https://api.openai.com/v1/models \
2 -H "Authorization: Bearer $OPENAI_API_KEY" \
3 -H "OpenAI-Organization: org-onnseWcOjHF4Hp81pNZTgGNH"
Making requests
You can paste the command below into your terminal to run your first API request. Make sure to replace
$OPENAI_API_KEY with your secret API key.
1 curl https://api.openai.com/v1/chat/completions \
2 -H "Content-Type: application/json" \
3 -H "Authorization: Bearer $OPENAI_API_KEY" \
4 -d '{
5 "model": "gpt-3.5-turbo",
6 "messages": [{"role": "user", "content": "Say this is a test!"}],
7 "temperature": 0.7
8 }'
This request queries the gpt-3.5-turbo model (which under the hood points to the latest gpt-3.5-turbo
model variant) to complete the text starting with a prompt of "Say this is a test". You should get a response
back that resembles the following:
1 {
2 "id": "chatcmpl-abc123",
3 "object": "chat.completion",
4 "created": 1677858242,
5 "model": "gpt-3.5-turbo-1106",
6 "usage": {
7 "prompt_tokens": 13,
8 "completion_tokens": 7,
9 "total_tokens": 20
10 },
11 "choices": [
12 {
13 "message": {
14 "role": "assistant",
15 "content": "\n\nThis is a test!"
16 },
17 "logprobs": null,
18 "finish_reason": "stop",
19 "index": 0
20 }
21 ]
22 }
Now that you've generated your first chat completion, let's break down the response object. We can see the
finish_reason is stop which means the API returned the full chat completion generated by the model
without running into any limits. In the choices list, we only generated a single message but you can set the
n parameter to generate multiple messages choices.
Streaming
The OpenAI API provides the ability to stream responses back to a client in order to allow partial results for
certain requests. To achieve this, we follow the Server-sent events standard.
Our official Node and Python libraries handle Server-sent events for you. In Python, a streaming request
looks like:
Parsing Server-sent events is non-trivial and should be done with caution. Simple strategies like splitting by
a new line may result in parsing errors. We recommend using existing client libraries when possible.
Audio
Learn how to turn audio into text or text into audio.
Create speech
POST https://api.openai.com/v1/audio/speech Example request python Copy
Generates audio from the input text. 1 from pathlib import Path
2 import openai
3
Request body 4 speech_file_path = Path(__file__).parent / "speech.m
5 response = openai.audio.speech.create(
model string Required 6 model="tts-1",
One of the available TTS models: tts-1 or tts-1-hd 7 voice="alloy",
8 input="The quick brown fox jumped over the lazy do
9 )
input string Required 10 response.stream_to_file(speech_file_path)
The text to generate audio for. The maximum length is 4096 characters.
Returns
Create transcription
POST https://api.openai.com/v1/audio/transcriptions Example request python Copy
Transcribes audio into the input language. 1 from openai import OpenAI
2 client = OpenAI()
3
Request body 4 audio_file = open("speech.mp3", "rb")
5 transcript = client.audio.transcriptions.create(
file file Required 6 model="whisper-1",
The audio file object (not file name) to transcribe, in one of these formats: flac, mp3, mp4, mpeg, 7 file=audio_file
Returns
Create translation
POST https://api.openai.com/v1/audio/translations Example request python Copy
Returns
The translated text.
Chat
Given a list of messages comprising a conversation, the model will return a response.
POST https://api.openai.com/v1/chat/completions
Example request gpt-3.5-turbo python Copy
Creates a model response for the given chat conversation.
1 from openai import OpenAI
2 client = OpenAI()
Request body 3
4 completion = client.chat.completions.create(
5 model="gpt-3.5-turbo",
messages array Required
6 messages=[
A list of messages comprising the conversation so far. Example Python code.
7 {"role": "system", "content": "You are a helpful
Show possible types 8 {"role": "user", "content": "Hello!"}
9 ]
10 )
model string Required
11
ID of the model to use. See the model endpoint compatibility table for details on which models
12 print(completion.choices[0].message)
work with the Chat API.
The total length of input tokens and generated tokens is limited by the model's context length.
Example Python code for counting tokens.
Setting to { "type": "json_object" } enables JSON mode, which guarantees the message
the model generates is valid JSON.
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
finish_reason="length" , which indicates the generation exceeded max_tokens or the
conversation exceeded the max context length.
Show properties
none is the default when no functions are present. auto is the default if functions are present.
Show possible types
Controls which (if any) function is called by the model. none means the model will not call a
function and instead generates a message. auto means the model can pick between
generating a message or calling a function. Specifying a particular function via {"name":
"my_function"} forces the model to call that function.
none is the default when no functions are present. auto is the default if functions are present.
Show possible types
Returns
Returns a chat completion object, or a streamed sequence of chat completion chunk objects if
the request is streamed.
object string
The object type, which is always chat.completion .
usage object
Usage statistics for the completion request.
Show properties
model string
The model to generate the completion.
system_fingerprint string
This fingerprint represents the backend configuration that the model runs with. Can be used in
conjunction with the seed request parameter to understand when backend changes have been
made that might impact determinism.
object string
The object type, which is always chat.completion.chunk .
Embeddings
Get a vector representation of a given input that can be easily consumed by machine learning models and
algorithms.
Create embeddings
POST https://api.openai.com/v1/embeddings Example request python Copy
Creates an embedding vector representing the input text. 1 from openai import OpenAI
2 client = OpenAI()
3
Request body 4 client.embeddings.create(
5 model="text-embedding-ada-002",
input string or array Required 6 input="The food was delicious and the waiter...",
Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single 7 encoding_format="float"
request, pass an array of strings or array of token arrays. The input must not exceed the max input 8 )
tokens for the model (8192 tokens for text-embedding-ada-002 ), cannot be an empty string,
and any array must be 2048 dimensions or less. Example Python code for counting tokens.
Show possible types Response Copy
1 {
model string Required
2 "object": "list",
ID of the model to use. You can use the List models API to see all of your available models, or see
3 "data": [
our Model overview for descriptions of them.
4 {
5 "object": "embedding",
encoding_format string Optional Defaults to float 6 "embedding": [
The format to return the embeddings in. Can be either float or base64 . 7 0.0023064255,
8 -0.009327292,
9 .... (1536 floats total for ada-002)
dimensions integer Optional 10 -0.0028842222,
The number of dimensions the resulting output embeddings should have. Only supported in 11 ],
text-embedding-3 and later models. 12 "index": 0
13 }
user string Optional 14 ],
A unique identifier representing your end-user, which can help OpenAI to monitor and detect 15 "model": "text-embedding-ada-002",
abuse. Learn more. 16 "usage": {
17 "prompt_tokens": 8,
18 "total_tokens": 8
19 }
Returns 20 }
index integer 1 {
Fine-tuning
Manage fine-tuning jobs to tailor a model to your specific training data.
POST https://api.openai.com/v1/fine_tuning/jobs
Example request python Copy
Creates a fine-tuning job which begins the process of creating a new model from a
1 from openai import OpenAI
given dataset.
2 client = OpenAI()
3
Response includes details of the enqueued job including job status and the name of
4 client.fine_tuning.jobs.create(
the fine-tuned models once complete. 5 training_file="file-abc123",
6 model="gpt-3.5-turbo"
Learn more about fine-tuning 7 )
For example, a suffix of "custom-model-name" would produce a model name like ft:gpt-
3.5-turbo:openai:custom-model-name:7p4lURel .
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 .
Returns
A fine-tuning.job object.
1 {
limit integer Optional Defaults to 20 2 "object": "list",
Number of fine-tuning jobs to retrieve. 3 "data": [
4 {
5 "object": "fine_tuning.job.event",
6 "id": "ft-event-TjX0lMfOniCZX64t9PUQT5hn",
Returns 7 "created_at": 1689813489,
8 "level": "warn",
A list of paginated fine-tuning job objects. 9 "message": "Fine tuning process stopping due t
10 "data": null,
11 "type": "message"
12 },
13 { ... },
14 { ... }
15 ], "has_more": true
16 }
Get status updates for a fine-tuning job. 1 from openai import OpenAI
2 client = OpenAI()
3
Path parameters 4 client.fine_tuning.jobs.list_events(
5 fine_tuning_job_id="ftjob-abc123",
fine_tuning_job_id string Required 6 limit=2
The ID of the fine-tuning job to get events for. 7 )
Response Copy
Query parameters 1 {
2 "object": "list",
after string Optional 3 "data": [
Identifier for the last event from the previous pagination request. 4 {
5 "object": "fine_tuning.job.event",
6 "id": "ft-event-ddTJfwuMVpfLXseO0Am0Gqjm",
limit integer Optional Defaults to 20
7 "created_at": 1692407401,
Number of events to retrieve.
8 "level": "info",
9 "message": "Fine tuning job successfully compl
10 "data": null,
Returns 11 "type": "message"
12 },
13 {
A list of fine-tuning event objects.
14 "object": "fine_tuning.job.event",
15 "id": "ft-event-tyiGuB72evQncpH87xe505Sv",
16 "created_at": 1692407400,
17 "level": "info",
18 "message": "New fine-tuned model created: ft:g
19 "data": null,
20 "type": "message"
21 }
22 ],
23 "has_more": true
24 }
Path parameters
Response Copy
fine_tuning_job_id string Required
1 {
The ID of the fine-tuning job.
2 "object": "fine_tuning.job",
3 "id": "ftjob-abc123",
4 "model": "davinci-002",
Returns 5 "created_at": 1692661014,
6 "finished_at": 1692661190,
The fine-tuning object with the given ID. 7 "fine_tuned_model": "ft:davinci-002:my-org:custom_
8 "organization_id": "org-123",
9 "result_files": [
10 "file-abc123"
11 ],
12 "status": "succeeded",
13 "validation_file": null,
14 "training_file": "file-abc123",
15 "hyperparameters": {
16 "n_epochs": 4,
17 },
18 "trained_tokens": 5768
19 }
Cancel fine-tuning
POST https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancel Example request python Copy
1 {
2 "object": "fine_tuning.job",
Returns 3 "id": "ftjob-abc123",
4 "model": "gpt-3.5-turbo-0613",
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.
hyperparameters object
The hyperparameters used for the fine-tuning job. See the fine-tuning guide for more details.
Show properties
model string
The base model that is being fine-tuned.
object string
The object type, which is always "fine_tuning.job".
organization_id string
The organization that owns the fine-tuning job.
result_files array
The compiled results file ID(s) for the fine-tuning job. You can retrieve the results with the Files
API.
status string
The current status of the fine-tuning job, which can be either validating_files , queued ,
running , succeeded , failed , or cancelled .
training_file string
The file ID used for training. You can retrieve the training data with the Files API.
id string 1 {
2 "object": "fine_tuning.job.event",
3 "id": "ftevent-abc123"
created_at integer 4 "created_at": 1677610602,
5 "level": "info",
level string 6 "message": "Created fine-tuning job"
7 }
message string
object string
Files
Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
Upload file
POST https://api.openai.com/v1/files Example request python Copy
Upload a file that can be used across various endpoints. The size of all the files 1 from openai import OpenAI
uploaded by one organization can be up to 100 GB. 2 client = OpenAI()
3
The size of individual files can be a maximum of 512 MB or 2 million tokens for 4 client.files.create(
5 file=open("mydata.jsonl", "rb"),
Assistants. See the Assistants Tools guide to learn more about the types of files
6 purpose="fine-tune"
supported. The Fine-tuning API only supports .jsonl files.
7 )
Request body 1 {
2 "id": "file-abc123",
file file Required 3 "object": "file",
The File object (not file name) to be uploaded. 4 "bytes": 120000,
5 "created_at": 1677610602,
6 "filename": "mydata.jsonl",
purpose string Required
7 "purpose": "fine-tune",
The intended purpose of the uploaded file.
8 }
Use "fine-tune" for Fine-tuning and "assistants" for Assistants and Messages. This allows us to
validate the format of the uploaded file is correct for fine-tuning.
Returns
List files
GET https://api.openai.com/v1/files Example request python Copy
Returns a list of files that belong to the user's organization. 1 from openai import OpenAI
2 client = OpenAI()
3
Query parameters 4 client.files.list()
1 {
2 "data": [
Returns 3 {
4 "id": "file-abc123",
Retrieve file
GET https://api.openai.com/v1/files/{file_id} Example request python Copy
1 {
2 "id": "file-abc123",
Returns 3 "object": "file",
4 "bytes": 120000,
Delete file
DELETE https://api.openai.com/v1/files/{file_id} Example request python Copy
1 {
2 "id": "file-abc123",
Returns 3 "object": "file",
4 "deleted": true
Deletion status. 5 }
Returns the contents of the specified file. 1 from openai import OpenAI
2 client = OpenAI()
3
Path parameters 4 content = client.files.retrieve_content("file-abc123"
Returns
The file content.
id string 1 {
The file identifier, which can be referenced in the API endpoints. 2 "id": "file-abc123",
3 "object": "file",
4 "bytes": 120000,
bytes integer 5 "created_at": 1677610602,
The size of the file, in bytes. 6 "filename": "salesOverview.pdf",
7 "purpose": "assistants",
created_at integer 8 }
The Unix timestamp (in seconds) for when the file was created.
filename string
The name of the file.
object string
The object type, which is always file .
purpose string
The intended purpose of the file. Supported values are fine-tune , fine-tune-results ,
assistants , and assistants_output .
Images
Given a prompt and/or an input image, the model will generate a new image.
Create image
POST https://api.openai.com/v1/images/generations Example request python Copy
1 {
n integer or null Optional Defaults to 1
2 "created": 1589478378,
The number of images to generate. Must be between 1 and 10. For dall-e-3 , only n=1 is
3 "data": [
supported.
4 {
5 "url": "https://..."
quality string Optional Defaults to standard 6 },
The quality of the image that will be generated. hd creates images with finer details and greater 7 {
consistency across the image. This param is only supported for dall-e-3 . 8 "url": "https://..."
9 }
10 ]
response_format string or null Optional Defaults to url
11 }
The format in which the generated images are returned. Must be one of url or b64_json .
Returns
Creates an edited or extended image given an original image and a prompt. 1 from openai import OpenAI
2 client = OpenAI()
3
Request body 4 client.images.edit(
5 image=open("otter.png", "rb"),
image file Required 6 mask=open("mask.png", "rb"),
The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, 7 prompt="A cute baby sea otter wearing a beret",
image must have transparency, which will be used as the mask. 8 n=2,
9 size="1024x1024"
10 )
prompt string Required
A text description of the desired image(s). The maximum length is 1000 characters.
Response Copy
Returns
Returns
b64_json string 1 {
The base64-encoded JSON of the generated image, if response_format is b64_json . 2 "url": "...",
3 "revised_prompt": "..."
4 }
url string
The URL of the generated image, if response_format is url (default).
revised_prompt string
The prompt that was used to generate the image, if there was any revision to the 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 Example request python Copy
Lists the currently available models, and provides basic information about each one 1 from openai import OpenAI
such as the owner and availability. 2 client = OpenAI()
3
4 client.models.list()
Returns
Response Copy
A list of model objects.
1 {
2 "object": "list",
3 "data": [
4 {
5 "id": "model-id-0",
6 "object": "model",
7 "created": 1686935002,
8 "owned_by": "organization-owner"
9 },
10 {
11 "id": "model-id-1",
12 "object": "model",
13 "created": 1686935002,
14 "owned_by": "organization-owner",
15 },
16 {
17 "id": "model-id-2",
18 "object": "model",
19 "created": 1686935002,
20 "owned_by": "openai"
21 },
22 ],
23 "object": "list"
24 }
Retrieve model
GET https://api.openai.com/v1/models/{model} Example request gpt-3.5-turbo-instruct python Copy
Retrieves a model instance, providing basic information about the model such as the 1 from openai import OpenAI
owner and permissioning. 2 client = OpenAI()
3
4 client.models.retrieve("gpt-3.5-turbo-instruct")
Path parameters
Response gpt-3.5-turbo-instruct Copy
model string Required
The ID of the model to use for this request
1 {
2 "id": "gpt-3.5-turbo-instruct",
3 "object": "model",
Returns 4 "created": 1686935002,
5 "owned_by": "openai"
6 }
The model object matching the specified ID.
Delete a fine-tuned model. You must have the Owner role in your organization to 1 from openai import OpenAI
delete a model. 2 client = OpenAI()
3
4 client.models.delete("ft:gpt-3.5-turbo:acemeco:suffix
Path parameters
id string 1 {
2 "id": "davinci",
The model identifier, which can be referenced in the API endpoints. 3 "object": "model",
4 "created": 1686935002,
5 "owned_by": "openai"
created integer
6 }
The Unix timestamp (in seconds) when the model was created.
object string
The object type, which is always "model".
owned_by string
The organization that owns the model.
Moderations
Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
Create moderation
POST https://api.openai.com/v1/moderations Example request python Copy
Classifies if text violates OpenAI's Content Policy 1 from openai import OpenAI
2 client = OpenAI()
3
Request body 4 client.moderations.create(input="I want to kill them.
Assistants Beta
Build assistants that can call models and use tools to perform tasks.
POST https://api.openai.com/v1/assistants
Example request python Copy
Create an assistant with a model and instructions.
1 from openai import OpenAI
2 client = OpenAI()
Request body 3
4 my_assistant = client.beta.assistants.create(
5 instructions="You are a personal math tutor. Whe
model Required
6 name="Math Tutor",
ID of the model to use. You can use the List models API to see all of your available models, or see
7 tools=[{"type": "code_interpreter"}],
our Model overview for descriptions of them.
8 model="gpt-4",
9 )
name string or null Optional 10 print(my_assistant)
The name of the assistant. The maximum length is 256 characters.
Returns
An assistant object.
Create an assistant file by attaching a File to an assistant. 1 from openai import OpenAI
2 client = OpenAI()
3
Path parameters 4 assistant_file = client.beta.assistants.files.create(
5 assistant_id="asst_abc123",
assistant_id string Required 6 file_id="file-abc123"
The ID of the assistant for which to create a File. 7 )
8 print(assistant_file)
Request body
Response Copy
1 {
limit integer Optional Defaults to 20
2 "object": "list",
A limit on the number of objects to be returned. Limit can range between 1 and 100, and the
3 "data": [
default is 20.
4 {
5 "id": "file-abc123",
order string Optional Defaults to desc 6 "object": "assistant.file",
Sort order by the created_at timestamp of the objects. asc for ascending order and desc 7 "created_at": 1699060412,
for descending order. 8 "assistant_id": "asst_abc123"
9 },
10 {
after string Optional
11 "id": "file-abc456",
A cursor for use in pagination. after is an object ID that defines your place in the list. For
12 "object": "assistant.file",
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent
13 "created_at": 1699060412,
call can include after=obj_foo in order to fetch the next page of the list. 14 "assistant_id": "asst_abc123"
15 }
before string Optional 16 ],
A cursor for use in pagination. before is an object ID that defines your place in the list. For 17 "first_id": "file-abc123",
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent 18 "last_id": "file-abc456",
call can include before=obj_foo in order to fetch the previous page of the list. 19 "has_more": false
20 }
Returns
Retrieves an assistant.
Path parameters 1 from openai import OpenAI
2 client = OpenAI()
assistant_id string Required 3
The ID of the assistant to retrieve. 4 my_assistant = client.beta.assistants.retrieve("asst_
5 print(my_assistant)
Returns
Response Copy
The assistant object matching the specified ID.
1 {
2 "id": "asst_abc123",
3 "object": "assistant",
4 "created_at": 1699009709,
5 "name": "HR Helper",
6 "description": null,
7 "model": "gpt-4",
8 "instructions": "You are an HR bot, and you have a
9 "tools": [
10 {
11 "type": "retrieval"
12 }
13 ],
14 "file_ids": [
15 "file-abc123"
16 ],
17 "metadata": {}
18 }
1 {
Returns 2 "id": "file-abc123",
3 "object": "assistant.file",
The assistant file object matching the specified ID. 4 "created_at": 1699055364,
5 "assistant_id": "asst_abc123"
6 }
Response Copy
name string or null Optional
The name of the assistant. The maximum length is 256 characters. 1 {
2 "id": "asst_abc123",
3 "object": "assistant",
description string or null Optional
4 "created_at": 1699009709,
The description of the assistant. The maximum length is 512 characters.
5 "name": "HR Helper",
6 "description": null,
instructions string or null Optional 7 "model": "gpt-4",
The system instructions that the assistant uses. The maximum length is 32768 characters. 8 "instructions": "You are an HR bot, and you have a
9 "tools": [
10 {
tools array Optional Defaults to []
11 "type": "retrieval"
A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools
12 }
can be of types code_interpreter , retrieval , or function .
13 ],
Show possible types 14 "file_ids": [
15 "file-abc123",
16 "file-abc456"
file_ids array Optional Defaults to []
17 ],
A list of File IDs attached to this assistant. There can be a maximum of 20 files attached to the
18 "metadata": {}
assistant. Files are ordered by their creation date in ascending order. If a file was previously
19 }
attached to the list but does not show up in the list, it will be deleted from the assistant.
Response Copy
Returns 1 {
2 "id": "asst_abc123",
3 "object": "assistant.deleted",
Deletion status
4 "deleted": true
5 }
1 {
Returns 2 id: "file-abc123",
3 object: "assistant.file.deleted",
Deletion status 4 deleted: true
5 }
Represents an assistant that can call the model and use tools. The assistant object Copy
id string 1 {
The identifier, which can be referenced in API endpoints. 2 "id": "asst_abc123",
3 "object": "assistant",
4 "created_at": 1698984975,
object string 5 "name": "Math Tutor",
The object type, which is always assistant . 6 "description": null,
7 "model": "gpt-4",
model string
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.
tools array
A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools
can be of types code_interpreter , retrieval , or function .
Show possible types
file_ids array
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.
metadata map
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 assistant file object Beta
id string 1 {
created_at integer
The Unix timestamp (in seconds) for when the assistant file was created.
assistant_id string
The assistant ID that the file is attached to.
Threads Beta
POST https://api.openai.com/v1/threads
Example request python Copy
Create a thread.
1 from openai import OpenAI
2 client = OpenAI()
Request body 3
4 empty_thread = client.beta.threads.create()
5 print(empty_thread)
messages array Optional
A list of messages to start the thread with.
Show properties Response Copy
1 {
metadata map Optional
2 "id": "thread_abc123",
Set of 16 key-value pairs that can be attached to an object. This can be useful for storing
3 "object": "thread",
additional information about the object in a structured format. Keys can be a maximum of 64
4 "created_at": 1699012949,
characters long and values can be a maxium of 512 characters long.
5 "metadata": {}
6 }
Returns
A thread object.
Response Copy
Returns 1 {
2 "id": "thread_abc123",
3 "object": "thread",
The thread object matching the specified ID.
4 "created_at": 1699014083,
5 "metadata": {}
6 }
Response Copy
Returns 1 {
2 "id": "thread_abc123",
3 "object": "thread.deleted",
Deletion status
4 "deleted": true
5 }
id string 1 {
The identifier, which can be referenced in API endpoints. 2 "id": "thread_abc123",
3 "object": "thread",
4 "created_at": 1698107661,
object string 5 "metadata": {}
The object type, which is always thread . 6 }
created_at integer
The Unix timestamp (in seconds) for when the thread was created.
metadata map
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.
Messages Beta
Request body
Response Copy
role string Required
The role of the entity that is creating the message. Currently only user is supported.
1 {
2 "id": "msg_abc123",
content string Required 3 "object": "thread.message",
The content of the message. 4 "created_at": 1699017614,
5 "thread_id": "thread_abc123",
6 "role": "user",
file_ids array Optional Defaults to []
7 "content": [
A list of File IDs that the message should use. There can be a maximum of 10 files attached to a
8 {
message. Useful for tools like retrieval and code_interpreter that can access and use
9 "type": "text",
files.
10 "text": {
11 "value": "How does AI work? Explain it in si
metadata map Optional 12 "annotations": []
Set of 16 key-value pairs that can be attached to an object. This can be useful for storing 13 }
additional information about the object in a structured format. Keys can be a maximum of 64 14 }
characters long and values can be a maxium of 512 characters long. 15 ],
16 "file_ids": [],
17 "assistant_id": null,
18 "run_id": null,
Returns 19 "metadata": {}
20 }
A message object.
List messages Beta
Returns a list of messages for a given thread. 1 from openai import OpenAI
2 client = OpenAI()
3
Path parameters 4 thread_messages = client.beta.threads.messages.list("
5 print(thread_messages.data)
thread_id string Required
The ID of the thread the messages belong to.
Response Copy
Query parameters 1 {
2 "object": "list",
3 "data": [
limit integer Optional Defaults to 20
4 {
A limit on the number of objects to be returned. Limit can range between 1 and 100, and the
5 "id": "msg_abc123",
default is 20.
6 "object": "thread.message",
7 "created_at": 1699016383,
order string Optional Defaults to desc 8 "thread_id": "thread_abc123",
Sort order by the created_at timestamp of the objects. asc for ascending order and desc 9 "role": "user",
for descending order. 10 "content": [
11 {
12 "type": "text",
after string Optional
13 "text": {
A cursor for use in pagination. after is an object ID that defines your place in the list. For
14 "value": "How does AI work? Explain it i
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent
15 "annotations": []
call can include after=obj_foo in order to fetch the next page of the list. 16 }
17 }
before string Optional 18 ],
A cursor for use in pagination. before is an object ID that defines your place in the list. For 19 "file_ids": [],
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent 20 "assistant_id": null,
call can include before=obj_foo in order to fetch the previous page of the list. 21 "run_id": null,
22 "metadata": {}
23 },
24 {
Returns 25 "id": "msg_abc456",
26 "object": "thread.message",
A list of message objects. 27 "created_at": 1699016383,
28 "thread_id": "thread_abc123",
29 "role": "user",
30 "content": [
31 {
32 "type": "text",
33 "text": {
34 "value": "Hello, what is AI?",
35 "annotations": []
36 }
37 }
38 ],
39 "file_ids": [
40 "file-abc123"
41 ],
42 "assistant_id": null,
43 "run_id": null,
44 "metadata": {}
45 }
46 ],
47 "first_id": "msg_abc123",
48 "last_id": "msg_abc456",
49 "has_more": false
50 }
1 {
2 "object": "list",
Query parameters 3 "data": [
4 {
limit integer Optional Defaults to 20 5 "id": "file-abc123",
A limit on the number of objects to be returned. Limit can range between 1 and 100, and the 6 "object": "thread.message.file",
default is 20. 7 "created_at": 1699061776,
8 "message_id": "msg_abc123"
9 },
order string Optional Defaults to desc 10 {
Sort order by the created_at timestamp of the objects. asc for ascending order and desc 11 "id": "file-abc123",
for descending order. 12 "object": "thread.message.file",
13 "created_at": 1699061776,
after string Optional 14 "message_id": "msg_abc123"
A cursor for use in pagination. after is an object ID that defines your place in the list. For 15 }
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent 16 ],
17 "first_id": "file-abc123",
call can include after=obj_foo in order to fetch the next page of the list.
18 "last_id": "file-abc123",
19 "has_more": false
before string Optional 20 }
A cursor for use in pagination. before 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.
Returns
1 {
2 "id": "msg_abc123",
Returns 3 "object": "thread.message",
4 "created_at": 1699017614,
The message object matching the specified ID. 5 "thread_id": "thread_abc123",
6 "role": "user",
7 "content": [
8 {
9 "type": "text",
10 "text": {
11 "value": "How does AI work? Explain it in si
12 "annotations": []
13 }
14 }
15 ],
16 "file_ids": [],
17 "assistant_id": null,
18 "run_id": null,
19 "metadata": {}
20 }
id string 1 {
created_at integer 8 {
The Unix timestamp (in seconds) for when the message was created. 9 "type": "text",
10 "text": {
11 "value": "Hi! How can I help you today?",
thread_id string 12 "annotations": []
The thread ID that this message belongs to. 13 }
14 }
role string 15 ],
The entity that produced the message. One of user or assistant . 16 "file_ids": [],
17 "assistant_id": "asst_abc123",
18 "run_id": "run_abc123",
content array 19 "metadata": {}
The content of the message in array of text and/or images. 20 }
Show possible types
file_ids array
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.
metadata map
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.
id string 1 {
The identifier, which can be referenced in API endpoints. 2 "id": "file-abc123",
3 "object": "thread.message.file",
4 "created_at": 1698107661,
object string 5 "message_id": "message_QLoItBbqwyAJEzlTy4y9kOMM",
The object type, which is always thread.message.file . 6 "file_id": "file-abc123"
7 }
created_at integer
The Unix timestamp (in seconds) for when the message file was created.
message_id string
The ID of the message that the File is attached to.
Runs Beta
Response Copy
Request body
1 {
2 "id": "run_abc123",
assistant_id string Required 3 "object": "thread.run",
The ID of the assistant to use to execute this run.
4 "created_at": 1699063290,
5 "assistant_id": "asst_abc123",
model string or null Optional 6 "thread_id": "thread_abc123",
The ID of the Model to be used to execute this run. If a value is provided here, it will override the 7 "status": "queued",
model associated with the assistant. If not, the model associated with the assistant will be used. 8 "started_at": 1699063290,
9 "expires_at": null,
10 "cancelled_at": null,
instructions string or null Optional 11 "failed_at": null,
Overrides the instructions of the assistant. This is useful for modifying the behavior on a per-run 12 "completed_at": 1699063291,
basis. 13 "last_error": null,
14 "model": "gpt-4",
additional_instructions string or null Optional 15 "instructions": null,
Appends additional instructions at the end of the instructions for the run. This is useful for 16 "tools": [
Returns
A run object.
Create a thread and run it in one request. 1 from openai import OpenAI
2 client = OpenAI()
3
Request body 4 run = client.beta.threads.create_and_run(
5 assistant_id="asst_abc123",
assistant_id string Required 6 thread={
The ID of the assistant to use to execute this run. 7 "messages": [
8 {"role": "user", "content": "Explain deep lear
9 ]
thread object Optional 10 }
Show properties 11 )
Response Copy
Query parameters
limit integer Optional Defaults to 20 1 {
A limit on the number of objects to be returned. Limit can range between 1 and 100, and the 2 "object": "list",
default is 20. 3 "data": [
4 {
5 "id": "run_abc123",
order string Optional Defaults to desc 6 "object": "thread.run",
Sort order by the created_at timestamp of the objects. asc for ascending order and desc 7 "created_at": 1699075072,
for descending order. 8 "assistant_id": "asst_abc123",
9 "thread_id": "thread_abc123",
A cursor for use in pagination. after is an object ID that defines your place in the list. For 11 "started_at": 1699075072,
12 "expires_at": null,
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent
13 "cancelled_at": null,
call can include after=obj_foo in order to fetch the next page of the list.
14 "failed_at": null,
15 "completed_at": 1699075073,
before string Optional 16 "last_error": null,
A cursor for use in pagination. before is an object ID that defines your place in the list. For 17 "model": "gpt-3.5-turbo",
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent 18 "instructions": null,
call can include before=obj_foo in order to fetch the previous page of the list. 19 "tools": [
20 {
21 "type": "code_interpreter"
22 }
Returns 23 ],
24 "file_ids": [
A list of run objects. 25 "file-abc123",
26 "file-abc456"
27 ],
28 "metadata": {},
29 "usage": {
30 "prompt_tokens": 123,
31 "completion_tokens": 456,
32 "total_tokens": 579
33 }
34 },
35 {
36 "id": "run_abc456",
37 "object": "thread.run",
38 "created_at": 1699063290,
39 "assistant_id": "asst_abc123",
40 "thread_id": "thread_abc123",
41 "status": "completed",
42 "started_at": 1699063290,
43 "expires_at": null,
44 "cancelled_at": null,
45 "failed_at": null,
46 "completed_at": 1699063291,
47 "last_error": null,
48 "model": "gpt-3.5-turbo",
49 "instructions": null,
50 "tools": [
51 {
52 "type": "code_interpreter"
53 }
54 ],
55 "file_ids": [
56 "file-abc123",
57 "file-abc456"
58 ],
59 "metadata": {},
60 "usage": {
61 "prompt_tokens": 123,
62 "completion_tokens": 456,
63 "total_tokens": 579
64 }
65 }
66 ],
67 "first_id": "run_abc123",
68 "last_id": "run_abc456",
69 "has_more": false
70 }
Returns a list of run steps belonging to a run. 1 from openai import OpenAI
2 client = OpenAI()
3
Path parameters 4 run_steps = client.beta.threads.runs.steps.list(
5 thread_id="thread_abc123",
thread_id string Required 6 run_id="run_abc123"
The ID of the thread the run and run steps belong to. 7 )
8 print(run_steps)
1 {
2 "object": "list",
Query parameters 3 "data": [
4 {
limit integer Optional Defaults to 20 5 "id": "step_abc123",
A limit on the number of objects to be returned. Limit can range between 1 and 100, and the 6 "object": "thread.run.step",
default is 20. 7 "created_at": 1699063291,
8 "run_id": "run_abc123",
9 "assistant_id": "asst_abc123",
order string Optional Defaults to desc
10 "thread_id": "thread_abc123",
Sort order by the created_at timestamp of the objects. asc for ascending order and desc
11 "type": "message_creation",
for descending order.
12 "status": "completed",
13 "cancelled_at": null,
after string Optional 14 "completed_at": 1699063291,
A cursor for use in pagination. after is an object ID that defines your place in the list. For 15 "expired_at": null,
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent 16 "failed_at": null,
call can include after=obj_foo in order to fetch the next page of the list. 17 "last_error": null,
18 "step_details": {
19 "type": "message_creation",
before string Optional
20 "message_creation": {
A cursor for use in pagination. before is an object ID that defines your place in the list. For 21 "message_id": "msg_abc123"
instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent 22 }
call can include before=obj_foo in order to fetch the previous page of the list. 23 },
24 "usage": {
25 "prompt_tokens": 123,
26 "completion_tokens": 456,
Returns 27 "total_tokens": 579
28 }
A list of run step objects. 29 }
30 ],
31 "first_id": "step_abc123",
32 "last_id": "step_abc456",
33 "has_more": false
34 }
1 {
2 "id": "run_abc123",
Returns 3 "object": "thread.run",
4 "created_at": 1699075072,
The run object matching the specified ID. 5 "assistant_id": "asst_abc123",
6 "thread_id": "thread_abc123",
7 "status": "completed",
8 "started_at": 1699075072,
9 "expires_at": null,
10 "cancelled_at": null,
11 "failed_at": null,
12 "completed_at": 1699075073,
13 "last_error": null,
14 "model": "gpt-3.5-turbo",
15 "instructions": null,
16 "tools": [
17 {
18 "type": "code_interpreter"
19 }
20 ],
21 "file_ids": [
22 "file-abc123",
23 "file-abc456"
24 ],
25 "metadata": {},
26 "usage": {
27 "prompt_tokens": 123,
28 "completion_tokens": 456,
29 "total_tokens": 579
30 }
31 }
1 {
Request body 2 "id": "run_abc123",
3 "object": "thread.run",
metadata map Optional 4 "created_at": 1699075072,
Set of 16 key-value pairs that can be attached to an object. This can be useful for storing 5 "assistant_id": "asst_abc123",
additional information about the object in a structured format. Keys can be a maximum of 64 6 "thread_id": "thread_abc123",
characters long and values can be a maxium of 512 characters long. 7 "status": "completed",
8 "started_at": 1699075072,
9 "expires_at": null,
10 "cancelled_at": null,
Returns 11 "failed_at": null,
12 "completed_at": 1699075073,
The modified run object matching the specified ID. 13 "last_error": null,
14 "model": "gpt-3.5-turbo",
15 "instructions": null,
16 "tools": [
17 {
18 "type": "code_interpreter"
19 }
20 ],
21 "file_ids": [
22 "file-abc123",
23 "file-abc456"
24 ],
25 "metadata": {
26 "user_id": "user_abc123"
27 },
28 "usage": {
29 "prompt_tokens": 123,
30 "completion_tokens": 456,
31 "total_tokens": 579
32 }
33 }
Response Copy
Request body
1 {
tool_outputs array Required 2 "id": "run_abc123",
A list of tools for which the outputs are being submitted. 3 "object": "thread.run",
Show properties 4 "created_at": 1699075592,
5 "assistant_id": "asst_abc123",
6 "thread_id": "thread_abc123",
7 "status": "queued",
Returns 8 "started_at": 1699075592,
9 "expires_at": 1699076192,
The modified run object matching the specified ID. 10 "cancelled_at": null,
11 "failed_at": null,
12 "completed_at": null,
13 "last_error": null,
14 "model": "gpt-4",
15 "instructions": "You tell the weather.",
16 "tools": [
17 {
18 "type": "function",
19 "function": {
20 "name": "get_weather",
21 "description": "Determine weather in my loca
22 "parameters": {
23 "type": "object",
24 "properties": {
25 "location": {
26 "type": "string",
27 "description": "The city and state e.g
28 },
29 "unit": {
30 "type": "string",
31 "enum": [
32 "c",
33 "f"
34 ]
35 }
36 },
37 "required": [
38 "location"
39 ]
40 }
41 }
42 }
43 ],
44 "file_ids": [],
45 "metadata": {},
46 "usage": null
47 }
1 {
2 "id": "run_abc123",
Returns 3 "object": "thread.run",
4 "created_at": 1699076126,
The modified run object matching the specified ID. 5 "assistant_id": "asst_abc123",
6 "thread_id": "thread_abc123",
7 "status": "cancelling",
8 "started_at": 1699076126,
9 "expires_at": 1699076726,
10 "cancelled_at": null,
11 "failed_at": null,
12 "completed_at": null,
13 "last_error": null,
14 "model": "gpt-4",
15 "instructions": "You summarize books.",
16 "tools": [
17 {
18 "type": "retrieval"
19 }
20 ],
21 "file_ids": [],
22 "metadata": {},
23 "usage": null
24 }
id string 1 {
The identifier, which can be referenced in API endpoints. 2 "id": "run_abc123",
3 "object": "thread.run",
4 "created_at": 1698107661,
object string 5 "assistant_id": "asst_abc123",
The object type, which is always thread.run . 6 "thread_id": "thread_abc123",
7 "status": "completed",
created_at integer 8 "started_at": 1699073476,
The Unix timestamp (in seconds) for when the run was created. 9 "expires_at": null,
10 "cancelled_at": null,
11 "failed_at": null,
thread_id string 12 "completed_at": 1699073498,
The ID of the thread that was executed on as a part of this run. 13 "last_error": null,
14 "model": "gpt-4",
assistant_id string 15 "instructions": null,
The ID of the assistant used for execution of this run. 16 "tools": [{"type": "retrieval"}, {"type": "code_in
17 "file_ids": [],
18 "metadata": {},
status string 19 "usage": {
The status of the run, which can be either queued , in_progress , requires_action , 20 "prompt_tokens": 123,
cancelling , cancelled , failed , completed , or expired . 21 "completion_tokens": 456,
22 "total_tokens": 579
23 }
required_action object or null
24 }
Details on the action required to continue the run. Will be null if no action is required.
Show properties
expires_at integer
The Unix timestamp (in seconds) for when the run will expire.
model string
The model that the assistant used for this run.
instructions string
The instructions that the assistant used for this run.
tools array
The list of tools that the assistant used for this run.
Show possible types
file_ids array
The list of File IDs the assistant used for this run.
metadata map
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.
id string 1 {
The identifier of the run step, which can be referenced in API endpoints. 2 "id": "step_abc123",
3 "object": "thread.run.step",
4 "created_at": 1699063291,
object string 5 "run_id": "run_abc123",
The object type, which is always thread.run.step . 6 "assistant_id": "asst_abc123",
7 "thread_id": "thread_abc123",
created_at integer 8 "type": "message_creation",
The Unix timestamp (in seconds) for when the run step was created. 9 "status": "completed",
10 "cancelled_at": null,
11 "completed_at": 1699063291,
assistant_id string 12 "expired_at": null,
The ID of the assistant associated with the run step. 13 "failed_at": null,
14 "last_error": null,
thread_id string 15 "step_details": {
The ID of the thread that was run. 16 "type": "message_creation",
17 "message_creation": {
18 "message_id": "msg_abc123"
run_id string 19 }
The ID of the run that this run step is a part of. 20 },
21 "usage": {
type string 22 "prompt_tokens": 123,
The type of run step, which can be either message_creation or tool_calls . 23 "completion_tokens": 456,
24 "total_tokens": 579
25 }
status string 26 }
The status of the run step, which can be either in_progress , cancelled , failed ,
completed , or expired .
step_details object
The details of the run step.
Show possible types
metadata map
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 object or null
Usage statistics related to the run step. This value will be null while the run step's status is
in_progress .
Show properties
Completions Legacy
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. Most models that support the legacy Completions endpoint will be shut off on
January 4th, 2024.
POST https://api.openai.com/v1/completions
Example request gpt-3.5-turbo-instruct python Copy
Creates a completion for the provided prompt and parameters.
1 from openai import OpenAI
2 client = OpenAI()
Request body 3
4 client.completions.create(
5 model="gpt-3.5-turbo-instruct",
model string Required
6 prompt="Say this is a test",
ID of the model to use. You can use the List models API to see all of your available models, or see
7 max_tokens=7,
our Model overview for descriptions of them.
8 temperature=0
9 )
prompt string or array Required
The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens,
or array of token arrays. Response gpt-3.5-turbo-instruct Copy
Note that <|endoftext|> is the document separator that the model sees during training, so if a 1 {
prompt is not specified the model will generate as if from the beginning of a new document. 2 "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
3 "object": "text_completion",
best_of integer or null Optional Defaults to 1 4 "created": 1589478378,
Generates best_of completions server-side and returns the "best" (the one with the highest 5 "model": "gpt-3.5-turbo-instruct",
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 {"50256": -100} to prevent the <|endoftext|> token from being
generated.
The token count of your prompt plus max_tokens cannot exceed the model's context length.
Example Python code for counting tokens.
Note: Because this parameter generates many completions, it can quickly consume your token
quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop .
Determinism is not guaranteed, and you should refer to the system_fingerprint response
parameter to monitor changes in the backend.
Returns
Represents a completion response from the API. Note: both the streamed and non- The completion object Copy
streamed response objects share the same shape (unlike the chat endpoint).
1 {
2 "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
id string
3 "object": "text_completion",
A unique identifier for the completion.