## Create message

`client.Beta.Threads.Messages.New(ctx, threadID, body) (*Message, error)`

**post** `/threads/{thread_id}/messages`

Create a message.

### Parameters

- `threadID string`

- `body BetaThreadMessageNewParams`

  - `Content param.Field[BetaThreadMessageNewParamsContentUnion]`

    The text contents of the message.

    - `string`

    - `type BetaThreadMessageNewParamsContentArrayOfContentParts []MessageContentPartParamUnionResp`

      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](https://platform.openai.com/docs/models).

      - `type ImageFileContentBlock struct{…}`

        References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message.

        - `ImageFile ImageFile`

          - `FileID string`

            The [File](https://platform.openai.com/docs/api-reference/files) 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 ImageFileDetail`

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

            - `const ImageFileDetailAuto ImageFileDetail = "auto"`

            - `const ImageFileDetailLow ImageFileDetail = "low"`

            - `const ImageFileDetailHigh ImageFileDetail = "high"`

        - `Type ImageFile`

          Always `image_file`.

          - `const ImageFileImageFile ImageFile = "image_file"`

      - `type ImageURLContentBlock struct{…}`

        References an image URL in the content of a message.

        - `ImageURL ImageURL`

          - `URL string`

            The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp.

          - `Detail ImageURLDetail`

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

            - `const ImageURLDetailAuto ImageURLDetail = "auto"`

            - `const ImageURLDetailLow ImageURLDetail = "low"`

            - `const ImageURLDetailHigh ImageURLDetail = "high"`

        - `Type ImageURL`

          The type of the content part.

          - `const ImageURLImageURL ImageURL = "image_url"`

      - `type TextContentBlockParam struct{…}`

        The text content that is part of a message.

        - `Text string`

          Text content to be sent to the model

        - `Type Text`

          Always `text`.

          - `const TextText Text = "text"`

  - `Role param.Field[BetaThreadMessageNewParamsRole]`

    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.

    - `const BetaThreadMessageNewParamsRoleUser BetaThreadMessageNewParamsRole = "user"`

    - `const BetaThreadMessageNewParamsRoleAssistant BetaThreadMessageNewParamsRole = "assistant"`

  - `Attachments param.Field[[]BetaThreadMessageNewParamsAttachment]`

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

    - `FileID string`

      The ID of the file to attach to the message.

    - `Tools []BetaThreadMessageNewParamsAttachmentToolUnion`

      The tools to add this file to.

      - `type CodeInterpreterTool struct{…}`

        - `Type CodeInterpreter`

          The type of tool being defined: `code_interpreter`

          - `const CodeInterpreterCodeInterpreter CodeInterpreter = "code_interpreter"`

      - `type BetaThreadMessageNewParamsAttachmentToolFileSearch struct{…}`

        - `Type FileSearch`

          The type of tool being defined: `file_search`

          - `const FileSearchFileSearch FileSearch = "file_search"`

  - `Metadata param.Field[Metadata]`

    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.

### Returns

- `type Message struct{…}`

  Represents a message within a [thread](https://platform.openai.com/docs/api-reference/threads).

  - `ID string`

    The identifier, which can be referenced in API endpoints.

  - `AssistantID string`

    If applicable, the ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) that authored this message.

  - `Attachments []MessageAttachment`

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

    - `FileID string`

      The ID of the file to attach to the message.

    - `Tools []MessageAttachmentToolUnion`

      The tools to add this file to.

      - `type CodeInterpreterTool struct{…}`

        - `Type CodeInterpreter`

          The type of tool being defined: `code_interpreter`

          - `const CodeInterpreterCodeInterpreter CodeInterpreter = "code_interpreter"`

      - `type MessageAttachmentToolAssistantToolsFileSearchTypeOnly struct{…}`

        - `Type FileSearch`

          The type of tool being defined: `file_search`

          - `const FileSearchFileSearch FileSearch = "file_search"`

  - `CompletedAt int64`

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

  - `Content []MessageContentUnion`

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

    - `type ImageFileContentBlock struct{…}`

      References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message.

      - `ImageFile ImageFile`

        - `FileID string`

          The [File](https://platform.openai.com/docs/api-reference/files) 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 ImageFileDetail`

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

          - `const ImageFileDetailAuto ImageFileDetail = "auto"`

          - `const ImageFileDetailLow ImageFileDetail = "low"`

          - `const ImageFileDetailHigh ImageFileDetail = "high"`

      - `Type ImageFile`

        Always `image_file`.

        - `const ImageFileImageFile ImageFile = "image_file"`

    - `type ImageURLContentBlock struct{…}`

      References an image URL in the content of a message.

      - `ImageURL ImageURL`

        - `URL string`

          The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp.

        - `Detail ImageURLDetail`

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

          - `const ImageURLDetailAuto ImageURLDetail = "auto"`

          - `const ImageURLDetailLow ImageURLDetail = "low"`

          - `const ImageURLDetailHigh ImageURLDetail = "high"`

      - `Type ImageURL`

        The type of the content part.

        - `const ImageURLImageURL ImageURL = "image_url"`

    - `type TextContentBlock struct{…}`

      The text content that is part of a message.

      - `Text Text`

        - `Annotations []AnnotationUnion`

          - `type FileCitationAnnotation struct{…}`

            A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files.

            - `EndIndex int64`

            - `FileCitation FileCitationAnnotationFileCitation`

              - `FileID string`

                The ID of the specific File the citation is from.

            - `StartIndex int64`

            - `Text string`

              The text in the message content that needs to be replaced.

            - `Type FileCitation`

              Always `file_citation`.

              - `const FileCitationFileCitation FileCitation = "file_citation"`

          - `type FilePathAnnotation struct{…}`

            A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file.

            - `EndIndex int64`

            - `FilePath FilePathAnnotationFilePath`

              - `FileID string`

                The ID of the file that was generated.

            - `StartIndex int64`

            - `Text string`

              The text in the message content that needs to be replaced.

            - `Type FilePath`

              Always `file_path`.

              - `const FilePathFilePath FilePath = "file_path"`

        - `Value string`

          The data that makes up the text.

      - `Type Text`

        Always `text`.

        - `const TextText Text = "text"`

    - `type RefusalContentBlock struct{…}`

      The refusal content generated by the assistant.

      - `Refusal string`

      - `Type Refusal`

        Always `refusal`.

        - `const RefusalRefusal Refusal = "refusal"`

  - `CreatedAt int64`

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

  - `IncompleteAt int64`

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

  - `IncompleteDetails MessageIncompleteDetails`

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

    - `Reason string`

      The reason the message is incomplete.

      - `const MessageIncompleteDetailsReasonContentFilter MessageIncompleteDetailsReason = "content_filter"`

      - `const MessageIncompleteDetailsReasonMaxTokens MessageIncompleteDetailsReason = "max_tokens"`

      - `const MessageIncompleteDetailsReasonRunCancelled MessageIncompleteDetailsReason = "run_cancelled"`

      - `const MessageIncompleteDetailsReasonRunExpired MessageIncompleteDetailsReason = "run_expired"`

      - `const MessageIncompleteDetailsReasonRunFailed MessageIncompleteDetailsReason = "run_failed"`

  - `Metadata Metadata`

    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.

  - `Object ThreadMessage`

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

    - `const ThreadMessageThreadMessage ThreadMessage = "thread.message"`

  - `Role MessageRole`

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

    - `const MessageRoleUser MessageRole = "user"`

    - `const MessageRoleAssistant MessageRole = "assistant"`

  - `RunID string`

    The ID of the [run](https://platform.openai.com/docs/api-reference/runs) associated with the creation of this message. Value is `null` when messages are created manually using the create message or create thread endpoints.

  - `Status MessageStatus`

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

    - `const MessageStatusInProgress MessageStatus = "in_progress"`

    - `const MessageStatusIncomplete MessageStatus = "incomplete"`

    - `const MessageStatusCompleted MessageStatus = "completed"`

  - `ThreadID string`

    The [thread](https://platform.openai.com/docs/api-reference/threads) ID that this message belongs to.

### Example

```go
package main

import (
  "context"
  "fmt"

  "github.com/openai/openai-go"
  "github.com/openai/openai-go/option"
)

func main() {
  client := openai.NewClient(
    option.WithAPIKey("My API Key"),
  )
  message, err := client.Beta.Threads.Messages.New(
    context.TODO(),
    "thread_id",
    openai.BetaThreadMessageNewParams{
      Content: openai.BetaThreadMessageNewParamsContentUnion{
        OfString: openai.String("string"),
      },
      Role: openai.BetaThreadMessageNewParamsRoleUser,
    },
  )
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", message.ID)
}
```

#### Response

```json
{
  "id": "id",
  "assistant_id": "assistant_id",
  "attachments": [
    {
      "file_id": "file_id",
      "tools": [
        {
          "type": "code_interpreter"
        }
      ]
    }
  ],
  "completed_at": 0,
  "content": [
    {
      "image_file": {
        "file_id": "file_id",
        "detail": "auto"
      },
      "type": "image_file"
    }
  ],
  "created_at": 0,
  "incomplete_at": 0,
  "incomplete_details": {
    "reason": "content_filter"
  },
  "metadata": {
    "foo": "string"
  },
  "object": "thread.message",
  "role": "user",
  "run_id": "run_id",
  "status": "in_progress",
  "thread_id": "thread_id"
}
```
