Create message

POSThttps:/api.openai.com/v1/threads/{thread_id}/messages

Create a message.

Path parameters

  • thread_id
    string
    Required

    The ID of the thread to create a message for.

Request body

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

Response

A message object.

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