Description
This endpoint interacts with OpenAI chat models, taking a series of messages as input and returning model-generated responses.It is suitable for chatbots, customer support, assistant tools, natural language interfaces, and many other scenarios.
Request body parameters
| Name | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model ID to use, for example gpt-4o-mini. |
messages | array | Yes | Array of messages representing the conversation history in order. |
temperature | number | No | Sampling temperature in the range 0–2; higher values make output more creative. Default is 1. |
top_p | number | No | Nucleus sampling parameter; usually adjust either temperature or top_p, not both. Default is 1. |
max_tokens | integer | No | Maximum number of tokens to generate in the response. |
n | integer | No | Number of responses to generate for each input. Default is 1. |
stream | boolean | No | Whether to return results as a streaming SSE response. |
stop | string / array | No | Up to four sequences where the API will stop generating further tokens. |
presence_penalty | number | No | Penalizes new tokens based on whether they appear in the text so far, reducing repetition of topics. |
frequency_penalty | number | No | Penalizes new tokens based on their existing frequency in the text, reducing token repetition. |
logit_bias | object | No | Modifies the likelihood of specific tokens appearing in the completion. |
user | string | No | Unique identifier for the end user, to help OpenAI monitor and detect abuse. |
tools | array | No | Array of tool definitions used to enable function / tool calling. |
tool_choice | string / object | No | Tool calling strategy, such as "auto", "none", or forcing a specific tool. |
messages array
Each message is an object. A typical example:
| Field | Type | Required | Description |
|---|---|---|---|
role | string | Yes | Role in the conversation: system, user, assistant, or tool. |
content | string / array | Yes | Message content, usually a string. For multimodal or rich content, this can be an array of segments. |
name | string | No | Optional name for the entity, often used for tools or specific personas. |
role usage:
system: Defines global behavior and instructions, such as style and persona.user: Content provided by the end user.assistant: Previous model responses, used as context.tool: Data returned from executing tools / functions.
Tool calling (tools and tool_choice)
Tool calling lets the model request that certain functions be executed, after which your application executes them and returns the result as a tool message.
tools example:
tool_choice example:
Response schema
A successful response includes the following top-level fields:| Field | Type | Description |
|---|---|---|
id | string | Unique ID for this completion. |
object | string | Object type, typically chat.completion or chat.completion.chunk (for streaming). |
created | integer | Unix timestamp of when the completion was created. |
model | string | ID of the model used. |
choices | array | List of generated completion choices. |
usage | object | Token usage statistics. |
system_fingerprint | string | System fingerprint for debugging (optional). |
choices looks like:
index: Position of this choice in thechoicesarray.message: The assistant message returned by the model.finish_reason: Why generation stopped, such asstop,length, ortool_calls.
usage field:
Errors
Like other OpenAI APIs, this endpoint returns non-2xx HTTP codes with a standard error object when something goes wrong:invalid_request_error: The request parameters are invalid or missing.authentication_error: Authentication failed (missing or invalid API key).rate_limit_error: You have hit a rate limit.insufficient_quota: Your billing plan or credit is insufficient.