Create chat completion

POSThttps:/api.openai.com/v1/chat/completions

Starting a new project? We recommend trying Responses to take advantage of the latest OpenAI platform features. Compare Chat Completions with Responses.


Creates a model response for the given chat conversation. Learn more in the text generation, vision, and audio guides.

Parameter support can differ depending on the model used to generate the response, particularly for newer reasoning models. Parameters that are only supported for reasoning models are noted below. For the current state of unsupported parameters in reasoning models, refer to the reasoning guide.

Request body

  • 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. We generally recommend altering this or top_p but not both.

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

  • user
    string

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

  • service_tier
    string or null
    Defaults: auto

    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', and the Project is Scale tier enabled, the system will utilize scale tier credits until they are exhausted.
    • If set to 'auto', and the Project is not Scale tier enabled, the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee.
    • If set to 'default', the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee.
    • If set to 'flex', the request will be processed with the Flex Processing service tier. Learn more.
    • When not set, the default behavior is 'auto'.

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

    • auto
      string
    • default
      string
    • flex
      string
  • messages
    array
    Required

    A list of messages comprising the conversation so far. Depending on the model you use, different message types (modalities) are supported, like text, images, and audio.

    • Developer message
      object

      Developer-provided instructions that the model should follow, regardless of messages sent by the user. With o1 models and newer, developer messages replace the previous system messages.

      • content
        string or array
        The contents of the developer message.
        • Text content
          string
          Required
          The contents of the developer message.
        • Array of content parts
          array
          Required

          An array of content parts with a defined type. For developer messages, only type text is supported.

          • items
            object

            Learn about text inputs.

            • type
              string
              Required
              The type of the content part.
              • text
                string
            • text
              string
              Required
              The text content.
      • role
        string
        Required

        The role of the messages author, in this case developer.

        • developer
          string
      • name
        string
        An optional name for the participant. Provides the model information to differentiate between participants of the same role.
    • System message
      object

      Developer-provided instructions that the model should follow, regardless of messages sent by the user. With o1 models and newer, use developer messages for this purpose instead.

      • content
        string or array
        The contents of the system message.
        • Text content
          string
          Required
          The contents of the system message.
        • Array of content parts
          array
          Required

          An array of content parts with a defined type. For system messages, only type text is supported.

          • Text content part
            object

            Learn about text inputs.

            • type
              string
              Required
              The type of the content part.
              • text
                string
            • text
              string
              Required
              The text content.
      • role
        string
        Required

        The role of the messages author, in this case system.

        • system
          string
      • name
        string
        An optional name for the participant. Provides the model information to differentiate between participants of the same role.
    • User message
      object
      Messages sent by an end user, containing prompts or additional context information.
      • content
        string or array
        The contents of the user message.
        • 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. Supported options differ based on the model being used to generate the response. Can contain text, image, or audio inputs.

          • Text content part
            object

            Learn about text inputs.

            • type
              string
              Required
              The type of the content part.
              • text
                string
            • text
              string
              Required
              The text content.
          • Image content part
            object

            Learn about image inputs.

            • type
              string
              Required
              The type of the content part.
              • image_url
                string
            • image_url
              object
              Required
              • url
                string
                Required
                Either a URL of the image or the base64 encoded image data.
              • detail
                string
                Defaults: auto

                Specifies the detail level of the image. Learn more in the Vision guide.

                • auto
                  string
                • low
                  string
                • high
                  string
          • Audio content part
            object

            Learn about audio inputs.

            • type
              string
              Required

              The type of the content part. Always input_audio.

              • input_audio
                string
            • input_audio
              object
              Required
              • data
                string
                Required
                Base64 encoded audio data.
              • format
                string
                Required
                The format of the encoded audio data. Currently supports "wav" and "mp3".
                • wav
                  string
                • mp3
                  string
          • File content part
            object

            Learn about file inputs for text generation.

            • type
              string
              Required

              The type of the content part. Always file.

              • file
                string
            • file
              object
              Required
              • filename
                string
                The name of the file, used when passing the file to the model as a string.
              • file_data
                string
                The base64 encoded file data, used when passing the file to the model as a string.
              • file_id
                string
                The ID of an uploaded file to use as input.
      • role
        string
        Required

        The role of the messages author, in this case user.

        • user
          string
      • name
        string
        An optional name for the participant. Provides the model information to differentiate between participants of the same role.
    • Assistant message
      object
      Messages sent by the model in response to user messages.
      • content
        string or array

        The contents of the assistant message. Required unless tool_calls or function_call is specified.

        • Text content
          string
          The contents of the assistant message.
        • Array of content parts
          array

          An array of content parts with a defined type. Can be one or more of type text, or exactly one of type refusal.

          • Text content part
            object

            Learn about text inputs.

            • type
              string
              Required
              The type of the content part.
              • text
                string
            • text
              string
              Required
              The text content.
          • Refusal content part
            object
            • type
              string
              Required
              The type of the content part.
              • refusal
                string
            • refusal
              string
              Required
              The refusal message generated by the model.
      • refusal
        string or null
        The refusal message by the assistant.
      • role
        string
        Required

        The role of the messages author, in this case assistant.

        • assistant
          string
      • name
        string
        An optional name for the participant. Provides the model information to differentiate between participants of the same role.
      • audio
        object or null

        Data about a previous audio response from the model. Learn more.

        • id
          string
          Required
          Unique identifier for a previous audio response from the model.
      • tool_calls
        array
        The tool calls generated by the model, such as function calls.
        • items
          object
          • id
            string
            Required
            The ID of the tool call.
          • type
            string
            Required

            The type of the tool. Currently, only function is supported.

            • function
              string
          • function
            object
            Required
            The function that the model called.
            • name
              string
              Required
              The name of the function to call.
            • arguments
              string
              Required
              The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
      • function_call
        object or null
        Deprecated

        Deprecated and replaced by tool_calls. The name and arguments of a function that should be called, as generated by the model.

        • arguments
          string
          Required
          The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
        • name
          string
          Required
          The name of the function to call.
    • Tool message
      object
      • role
        string
        Required

        The role of the messages author, in this case tool.

        • tool
          string
      • content
        string or array
        The contents of the tool message.
        • Text content
          string
          Required
          The contents of the tool message.
        • Array of content parts
          array
          Required

          An array of content parts with a defined type. For tool messages, only type text is supported.

          • Text content part
            object

            Learn about text inputs.

            • type
              string
              Required
              The type of the content part.
              • text
                string
            • text
              string
              Required
              The text content.
      • tool_call_id
        string
        Required
        Tool call that this message is responding to.
    • Function message
      object
      Deprecated
      • role
        string
        Required

        The role of the messages author, in this case function.

        • function
          string
      • content
        string or null
        Required
        The contents of the function message.
      • name
        string
        Required
        The name of the function to call.
  • model
    string

    Model ID used to generate the response, like gpt-4o or o3. OpenAI offers a wide range of models with different capabilities, performance characteristics, and price points. Refer to the model guide to browse and compare available models.

    • model
      string
      Required
    • model
      string
      Required
      • 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
      • o4-mini
        string
      • o4-mini-2025-04-16
        string
      • o3
        string
      • o3-2025-04-16
        string
      • o3-mini
        string
      • o3-mini-2025-01-31
        string
      • o1
        string
      • o1-2024-12-17
        string
      • o1-preview
        string
      • o1-preview-2024-09-12
        string
      • o1-mini
        string
      • o1-mini-2024-09-12
        string
      • gpt-4o
        string
      • gpt-4o-2024-11-20
        string
      • gpt-4o-2024-08-06
        string
      • gpt-4o-2024-05-13
        string
      • gpt-4o-audio-preview
        string
      • gpt-4o-audio-preview-2024-10-01
        string
      • gpt-4o-audio-preview-2024-12-17
        string
      • gpt-4o-mini-audio-preview
        string
      • gpt-4o-mini-audio-preview-2024-12-17
        string
      • gpt-4o-search-preview
        string
      • gpt-4o-mini-search-preview
        string
      • gpt-4o-search-preview-2025-03-11
        string
      • gpt-4o-mini-search-preview-2025-03-11
        string
      • chatgpt-4o-latest
        string
      • gpt-4o-mini
        string
      • gpt-4o-mini-2024-07-18
        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-0301
        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
  • modalities
    array or null

    Output types that you would like the model to generate. Most models are capable of generating text, which is the default:

    ["text"]

    The gpt-4o-audio-preview model can also be used to generate audio. To request that this model generate both text and audio responses, you can use:

    ["text", "audio"]

    • items
      string
      • text
        string
      • audio
        string
  • reasoning_effort
    string or null
    Defaults: medium

    o-series models only

    Constrains effort on reasoning for reasoning models. Currently supported values are low, medium, and high. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.

    • low
      string
    • medium
      string
    • high
      string
  • max_completion_tokens
    integer or null

    An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.

  • frequency_penalty
    number or null
    Defaults: 0
    Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
  • presence_penalty
    number or null
    Defaults: 0
    Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
  • web_search_options
    object

    This tool searches the web for relevant results to use in a response. Learn more about the web search tool.

    • user_location
      object or null
      Approximate location parameters for the search.
      • type
        string
        Required

        The type of location approximation. Always approximate.

        • approximate
          string
      • approximate
        object
        Required
        Approximate location parameters for the search.
        • country
          string

          The two-letter ISO country code of the user, e.g. US.

        • region
          string

          Free text input for the region of the user, e.g. California.

        • city
          string

          Free text input for the city of the user, e.g. San Francisco.

        • timezone
          string

          The IANA timezone of the user, e.g. America/Los_Angeles.

    • search_context_size
      string
      Defaults: medium

      High level guidance for the amount of context window space to use for the search. One of low, medium, or high. medium is the default.

      • low
        string
      • medium
        string
      • high
        string
  • top_logprobs
    integer or null

    An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used.

  • response_format
    object

    An object specifying the format that the model must output.

    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 the older JSON mode, which ensures the message the model generates is valid JSON. Using json_schema is preferred for models that support it.

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

    • 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
  • audio
    object or null

    Parameters for audio output. Required when audio output is requested with modalities: ["audio"]. Learn more.

    • voice
      string

      The voice the model uses to respond. Supported voices are alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, and shimmer.

      • voice
        string
        Required
      • voice
        string
        Required
        • alloy
          string
        • ash
          string
        • ballad
          string
        • coral
          string
        • echo
          string
        • fable
          string
        • onyx
          string
        • nova
          string
        • sage
          string
        • shimmer
          string
        • verse
          string
    • format
      string
      Required

      Specifies the output audio format. Must be one of wav, mp3, flac, opus, or pcm16.

      • wav
        string
      • aac
        string
      • mp3
        string
      • flac
        string
      • opus
        string
      • pcm16
        string
  • store
    boolean or null
    Defaults: false

    Whether or not to store the output of this chat completion request for use in our model distillation or evals products.

  • stream
    boolean or null
    Defaults: false

    If set to true, the model response data will be streamed to the client as it is generated using server-sent events. See the Streaming section below for more information, along with the streaming responses guide for more information on how to handle the streaming events.

  • stop
    string or array

    Not supported with latest reasoning models o3 and o4-mini.

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

    • stop
      string or null
      Defaults: <|endoftext|>
    • stop
      array
      • items
        string
  • logit_bias
    object or null
    Defaults: null
    Modify the likelihood of specified tokens appearing in the completion. Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. 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.
  • logprobs
    boolean or null
    Defaults: false

    Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message.

  • max_tokens
    integer or null
    Deprecated

    The maximum number of tokens that can be generated in the chat completion. This value can be used to control costs for text generated via API.

    This value is now deprecated in favor of max_completion_tokens, and is not compatible with o-series models.

  • n
    integer or null
    Defaults: 1

    How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

  • prediction
    object

    Configuration for a Predicted Output, which can greatly improve response times when large parts of the model response are known ahead of time. This is most common when you are regenerating a file with only minor changes to most of the content.

    • Static Content
      object
      Static predicted output content, such as the content of a text file that is being regenerated.
      • type
        string
        Required

        The type of the predicted content you want to provide. This type is currently always content.

        • content
          string
      • content
        string or array
        The content that should be matched when generating a model response. If generated tokens would match this content, the entire model response can be returned much more quickly.
        • Text content
          string
          Required
          The content used for a Predicted Output. This is often the text of a file you are regenerating with minor changes.
        • Array of content parts
          array
          Required

          An array of content parts with a defined type. Supported options differ based on the model being used to generate the response. Can contain text inputs.

          • items
            object

            Learn about text inputs.

            • type
              string
              Required
              The type of the content part.
              • text
                string
            • text
              string
              Required
              The text content.
  • seed
    integer or null

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

  • stream_options
    object or null
    Defaults: null

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

    • include_usage
      boolean

      If set, an additional chunk will be streamed before the data: [DONE] message. The usage field on this chunk shows the token usage statistics for the entire request, and the choices field will always be an empty array.

      All other chunks will also include a usage field, but with a null value. NOTE: If the stream is interrupted, you may not receive the final usage chunk which contains the total token usage for the request.

  • tools
    array
    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.
    • items
      object
      • type
        string
        Required

        The type of the tool. Currently, only function is supported.

        • 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_choice
    string or object

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

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

    • tool_choice
      string

      none means the model will not call any tool and instead generates a message. auto means the model can pick between generating a message or calling one or more tools. required means the model must call one or more tools.

      • none
        string
      • auto
        string
      • required
        string
    • tool_choice
      object
      Specifies a tool the model should use. Use to force the model to call a specific function.
      • type
        string
        Required

        The type of the tool. Currently, only function is supported.

        • function
          string
      • function
        object
        Required
        • name
          string
          Required
          The name of the function to call.
  • parallel_tool_calls
    boolean
    Defaults: true

    Whether to enable parallel function calling during tool use.

  • function_call
    string or object

    Deprecated in favor of tool_choice.

    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 forces the model to call that function.

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

    • function_call
      string

      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.

      • none
        string
      • auto
        string
    • function_call
      object

      Specifying a particular function via forces the model to call that function.

      • name
        string
        Required
        The name of the function to call.
  • functions
    array
    Deprecated

    Deprecated in favor of tools.

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

    • items
      object
      Deprecated
      • 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.

Response

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

Example request
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": "VAR_chat_model_id",
6
"messages": [
7
{
8
"role": "developer",
9
"content": "You are a helpful assistant."
10
},
11
{
12
"role": "user",
13
"content": "Hello!"
14
}
15
]
16
}'
Example response
1
{
2
"id": "chatcmpl-B9MBs8CjcvOU2jLn4n570S5qMJKcT",
3
"object": "chat.completion",
4
"created": 1741569952,
5
"model": "gpt-4.1-2025-04-14",
6
"choices": [
7
{
8
"index": 0,
9
"message": {
10
"role": "assistant",
11
"content": "Hello! How can I assist you today?",
12
"refusal": null,
13
"annotations": []
14
},
15
"logprobs": null,
16
"finish_reason": "stop"
17
}
18
],
19
"usage": {
20
"prompt_tokens": 19,
21
"completion_tokens": 10,
22
"total_tokens": 29,
23
"prompt_tokens_details": {
24
"cached_tokens": 0,
25
"audio_tokens": 0
26
},
27
"completion_tokens_details": {
28
"reasoning_tokens": 0,
29
"audio_tokens": 0,
30
"accepted_prediction_tokens": 0,
31
"rejected_prediction_tokens": 0
32
}
33
},
34
"service_tier": "default"
35
}
Built with