Create thread and run

POSThttps:/api.openai.com/v1/threads/runs

Create a thread and run it in one request.

Request body

  • assistant_id
    string
    Required

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

  • thread
    object
    Options to create a new thread. If no thread is provided when running a request, an empty thread will be created.
    • messages
      array

      A list of messages to start the thread with.

      • items
        object
        • role
          string
          Required

          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.
          • user
            string
          • assistant
            string
        • content
          string or array
          • Text content
            string
            Required
            The text contents of the message.
          • Array of content parts
            array
            Required

            An array of content parts with a defined type, each can be of type text or images can be passed with image_url or image_file. Image types are only supported on Vision-compatible models.

            • Image file
              object

              References an image File in the content of a message.

              • type
                string
                Required

                Always image_file.

                • image_file
                  string
              • image_file
                object
                Required
                • file_id
                  string
                  Required

                  The File ID of the image in the message content. Set purpose="vision" when uploading the File if you need to later display the file content.

                • detail
                  string
                  Defaults: auto

                  Specifies the detail level of the image if specified by the user. low uses fewer tokens, you can opt in to high resolution using high.

                  • auto
                    string
                  • low
                    string
                  • high
                    string
            • Image URL
              object
              References an image URL in the content of a message.
              • type
                string
                Required
                The type of the content part.
                • image_url
                  string
              • image_url
                object
                Required
                • url
                  string
                  Required
                  The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp.
                • detail
                  string
                  Defaults: auto

                  Specifies the detail level of the image. low uses fewer tokens, you can opt in to high resolution using high. Default value is auto

                  • auto
                    string
                  • low
                    string
                  • high
                    string
            • Text
              object
              The text content that is part of a message.
              • type
                string
                Required

                Always text.

                • text
                  string
              • text
                string
                Required
                Text content to be sent to the model
        • attachments
          array or null
          A list of files attached to the message, and the tools they should be added to.
          • items
            object
            • file_id
              string
              The ID of the file to attach to the message.
            • tools
              array
              The tools to add this file to.
              • Code interpreter tool
                object
                • type
                  string
                  Required

                  The type of tool being defined: code_interpreter

                  • code_interpreter
                    string
              • FileSearch tool
                object
                • type
                  string
                  Required

                  The type of tool being defined: file_search

                  • file_search
                    string
        • metadata
          object or null
          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, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
    • tool_resources
      object or null

      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 code_interpreter tool requires a list of file IDs, while the file_search tool requires a list of vector store IDs.

      • code_interpreter
        object
        • file_ids
          array
          Defaults:

          A list of file IDs made available to the code_interpreter tool. There can be a maximum of 20 files associated with the tool.

          • items
            string
      • file_search
        object
        • file_search
          object
        • file_search
          object
    • metadata
      object or null
      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, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
  • model
    string

    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.

    • model
      string
    • model
      string
      • gpt-4.1
        string
      • gpt-4.1-mini
        string
      • gpt-4.1-nano
        string
      • gpt-4.1-2025-04-14
        string
      • gpt-4.1-mini-2025-04-14
        string
      • gpt-4.1-nano-2025-04-14
        string
      • gpt-4o
        string
      • gpt-4o-2024-11-20
        string
      • gpt-4o-2024-08-06
        string
      • gpt-4o-2024-05-13
        string
      • gpt-4o-mini
        string
      • gpt-4o-mini-2024-07-18
        string
      • gpt-4.5-preview
        string
      • gpt-4.5-preview-2025-02-27
        string
      • gpt-4-turbo
        string
      • gpt-4-turbo-2024-04-09
        string
      • gpt-4-0125-preview
        string
      • gpt-4-turbo-preview
        string
      • gpt-4-1106-preview
        string
      • gpt-4-vision-preview
        string
      • gpt-4
        string
      • gpt-4-0314
        string
      • gpt-4-0613
        string
      • gpt-4-32k
        string
      • gpt-4-32k-0314
        string
      • gpt-4-32k-0613
        string
      • gpt-3.5-turbo
        string
      • gpt-3.5-turbo-16k
        string
      • gpt-3.5-turbo-0613
        string
      • gpt-3.5-turbo-1106
        string
      • gpt-3.5-turbo-0125
        string
      • gpt-3.5-turbo-16k-0613
        string
  • instructions
    string or null
    Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis.
  • tools
    array or null
    Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.
    • Code interpreter tool
      object
      • type
        string
        Required

        The type of tool being defined: code_interpreter

        • code_interpreter
          string
    • FileSearch tool
      object
      • type
        string
        Required

        The type of tool being defined: file_search

        • file_search
          string
      • file_search
        object
        Overrides for the file search tool.
        • max_num_results
          integer

          The maximum number of results the file search tool should output. The default is 20 for gpt-4* models and 5 for gpt-3.5-turbo. This number should be between 1 and 50 inclusive.

          Note that the file search tool may output fewer than max_num_results results. See the file search tool documentation for more information.

        • ranking_options
          object

          The ranking options for the file search. If not specified, the file search tool will use the auto ranker and a score_threshold of 0.

          See the file search tool documentation for more information.

          • ranker
            string

            The ranker to use for the file search. If not specified will use the auto ranker.

            • auto
              string
            • default_2024_08_21
              string
          • score_threshold
            number
            Required
            The score threshold for the file search. All values must be a floating point number between 0 and 1.
    • Function tool
      object
      • type
        string
        Required

        The type of tool being defined: function

        • function
          string
      • function
        object
        Required
        • description
          string
          A description of what the function does, used by the model to choose when and how to call the function.
        • name
          string
          Required
          The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
        • parameters
          object

          The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.

          Omitting parameters defines a function with an empty parameter list.

        • strict
          boolean or null
          Defaults: false

          Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the parameters field. Only a subset of JSON Schema is supported when strict is true. Learn more about Structured Outputs in the .

  • tool_resources
    object or null

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

    • code_interpreter
      object
      • file_ids
        array
        Defaults:

        A list of file IDs made available to the code_interpreter tool. There can be a maximum of 20 files associated with the tool.

        • items
          string
    • file_search
      object
      • vector_store_ids
        array

        The ID of the vector store attached to this assistant. There can be a maximum of 1 vector store attached to the assistant.

        • items
          string
  • metadata
    object or null
    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, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
  • temperature
    number or null
    Defaults: 1
    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.
  • top_p
    number or null
    Defaults: 1
    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.
  • stream
    boolean or null

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

  • max_prompt_tokens
    integer or null

    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 incomplete. See incomplete_details for more info.

  • max_completion_tokens
    integer or null

    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 incomplete. See incomplete_details for more info.

  • truncation_strategy
    object
  • tool_choice
    object
  • parallel_tool_calls
    boolean
    Defaults: true

    Whether to enable parallel function calling during tool use.

  • response_format
    string or object

    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 Structured Outputs which ensures the model will match your supplied JSON schema. Learn more in the Structured Outputs guide.

    Setting to enables JSON mode, which ensures 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.

    • response_format
      string

      auto is the default value

      • auto
        string
    • Text
      object
      Default response format. Used to generate text responses.
      • type
        string
        Required

        The type of response format being defined. Always text.

        • text
          string
    • JSON object
      object

      JSON object response format. An older method of generating JSON responses. Using json_schema is recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.

      • type
        string
        Required

        The type of response format being defined. Always json_object.

        • json_object
          string
    • JSON schema
      object

      JSON Schema response format. Used to generate structured JSON responses. Learn more about Structured Outputs.

      • type
        string
        Required

        The type of response format being defined. Always json_schema.

        • json_schema
          string
      • json_schema
        object
        Required
        Structured Outputs configuration options, including a JSON Schema.
        • description
          string
          A description of what the response format is for, used by the model to determine how to respond in the format.
        • name
          string
          Required
          The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
        • schema
          object

          The schema for the response format, described as a JSON Schema object. Learn how to build JSON schemas here.

        • strict
          boolean or null
          Defaults: false

          Whether to enable strict schema adherence when generating the output. If set to true, the model will always follow the exact schema defined in the schema field. Only a subset of JSON Schema is supported when strict is true. To learn more, read the Structured Outputs guide.

Response

A run object.

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