/v2/chatChat API (v2)Generates a text response to a user message and streams it down, token by token. To learn how to use the Chat API with streaming follow our Text Generation guides.
Follow the Migration Guide for instructions on moving from API v1 to API v2.
Parameters
X-Client-NamestringheaderThe name of the project that is making the request.
Request body
application/json
citation_optionsobjectOptions for controlling citation generation.
Show child attributes
modestringDefaults to `"enabled"`. Citations are enabled by default for models that support it, but can be turned off by setting `"type": "disabled"`.
documentsarray of valueA list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
Show child attributes
Show array items
oneOf · 2 options
Relevant information that could be used by the model to generate a more accurate reply. The content of each document are generally short (should be under 300 words). Metadata should be used to provide additional information, both the key name and the value will be passed to the model.
dataobjectrequiredA relevant document that the model can cite to generate a more accurate reply. Each document is a string-any dictionary.
idstringUnique identifier for this document which will be referenced in citations. If not provided an ID will be automatically generated.
frequency_penaltynumber · floatDefaults to `0.0`, min value of `0.0`, max value of `1.0`. Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
kintegerEnsures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled. Defaults to `0`, min value of `0`, max value of `500`.
logprobsbooleanDefaults to `false`. When set to `true`, the log probabilities of the generated tokens will be included in the response.
max_tokensintegerThe maximum number of output tokens the model will generate in the response. If not set, `max_tokens` defaults to the model's maximum output token limit. You can find the maximum output token limits for each model in the [model documentation](https://docs.cohere.com/docs/models). **Note**: Setting a low value may result in incomplete generations. In such cases, the `finish_reason` field in the response will be set to `"MAX_TOKENS"`. **Note**: If `max_tokens` is set higher than the model's maximum output token limit, the generation will be capped at that model-specific maximum limit.
messagesarray of valuerequiredA list of chat messages in chronological order, representing a conversation between the user and the model. Messages can be from `User`, `Assistant`, `Tool` and `System` roles. Learn more about messages and roles in [the Chat API guide](https://docs.cohere.com/v2/docs/chat-api).
Show child attributes
Show array items
Represents a single message in the chat history from a given role.
oneOf · 4 options
User Message
A message from the user.
contentvaluerequiredThe content of the message. This can be a string or a list of content blocks. If a string is provided, it will be treated as a text content block.
Show child attributes
oneOf · 2 options
Show array items
A Content block which contains information about the content type and the content itself.
oneOf · 2 options
Text content of the message.
textstringrequiredtypestringrequiredImage content of the message.
image_urlobjectrequiredShow child attributes
detailstringControls the level of detail in image processing. `"auto"` is the default and lets the system choose, `"low"` is faster but less detailed, and `"high"` preserves maximum detail. You can save tokens and speed up responses by using detail: `"low"`.
urlstringrequiredURL of an image. Can be either a base64 data URI or a web URL.
typestringrequiredType of message content
rolestringrequiredAssistant Message
A message from the assistant role can contain text and tool call information.
citationsarray of objectShow child attributes
Show array items
Citation information containing sources and the text cited.
content_indexintegerIndex of the content block in which this citation appears.
endintegerEnd index of the cited snippet in the original source text.
sourcesarray of valueShow child attributes
Show array items
A source object containing information about the source of the data cited.
oneOf · 2 options
Tool Output
idstringThe unique identifier of the document
tool_outputobjectA document source object containing the unique identifier of the document and the document itself.
documentobjectidstringThe unique identifier of the document
startintegerStart index of the cited snippet in the original source text.
textstringText snippet that is being cited.
typestringThe type of citation which indicates what part of the response the citation is for.
contentvalueShow child attributes
oneOf · 2 options
Show array items
oneOf · 2 options
Thinking content of the message. This will be present when `thinking` is enabled, and will contain the models internal reasoning.
thinkingstringrequiredtypestringrequiredrolestringrequiredtool_callsarray of objectShow child attributes
Show array items
An array of tool calls to be made.
functionobjectShow child attributes
argumentsstringnamestringidstringrequiredtypestringrequiredtool_planstringA chain-of-thought style reflection and plan that the model generates when working with Tools.
System Message
A message from the system.
contentvaluerequiredShow child attributes
oneOf · 2 options
Show array items
oneOf · 1 option
rolestringrequiredTool Message
A message with Tool outputs.
contentvaluerequiredOutputs from a tool. The content should formatted as a JSON object string, or a list of tool content blocks
Show child attributes
oneOf · 2 options
Show array items
A content block which contains information about the content of a tool result
oneOf · 2 options
Document content.
Relevant information that could be used by the model to generate a more accurate reply. The content of each document are generally short (should be under 300 words). Metadata should be used to provide additional information, both the key name and the value will be passed to the model.
typestringrequiredrolestringrequiredtool_call_idstringrequiredThe id of the associated tool call that has provided the given content
modelstringrequiredThe name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models).
pnumber · floatEnsures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`. Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
presence_penaltynumber · floatDefaults to `0.0`, min value of `0.0`, max value of `1.0`. Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
priorityintegerControls how early the request is handled. Lower numbers indicate higher priority (default: 0, the highest). When the system is under load, higher-priority requests are processed first and are the least likely to be dropped.
response_formatvalueConfiguration for forcing the model output to adhere to the specified format. Supported on [Command R](https://docs.cohere.com/v2/docs/command-r), [Command R+](https://docs.cohere.com/v2/docs/command-r-plus) and newer models. The model can be forced into outputting JSON objects by setting `{ "type": "json_object" }`. A [JSON Schema](https://json-schema.org/) can optionally be provided, to ensure a specific structure. **Note**: When using `{ "type": "json_object" }` your `message` should always explicitly instruct the model to generate a JSON (eg: _"Generate a JSON ..."_) . Otherwise the model may end up getting stuck generating an infinite stream of characters and eventually run out of context length. **Note**: When `json_schema` is not specified, the generated object can have up to 5 layers of nesting. **Limitation**: The parameter is not supported when used in combinations with the `documents` or `tools` parameters.
Show child attributes
oneOf · 2 options
typestringrequiredDefaults to `"text"`. When set to `"json_object"`, the model's output will be a valid JSON Object.
json_schemaobjectA [JSON schema](https://json-schema.org/overview/what-is-jsonschema) object that the output will adhere to. There are some restrictions we have on the schema, refer to [our guide](https://docs.cohere.com/docs/structured-outputs-json#schema-constraints) for more information. Example (required name and age object): ```json { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"} }, "required": ["name", "age"] } ``` **Note**: This field must not be specified when the `type` is set to `"text"`.
typestringrequiredDefaults to `"text"`. When set to `"json_object"`, the model's output will be a valid JSON Object.
safety_modevalueUsed to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`. When `OFF` is specified, the safety instruction will be omitted. Safety modes are not yet configurable in combination with `tools` and `documents` parameters. **Note**: This parameter is only compatible newer Cohere models, starting with [Command R 08-2024](https://docs.cohere.com/docs/command-r#august-2024-release) and [Command R+ 08-2024](https://docs.cohere.com/docs/command-r-plus#august-2024-release). **Note**: `command-r7b-12-2024` and newer models only support `"CONTEXTUAL"` and `"STRICT"` modes.
seedintegerIf specified, the backend will make a best effort to sample tokens deterministically, such that repeated requests with the same seed and parameters should return the same result. However, determinism cannot be totally guaranteed.
stop_sequencesarray of stringA list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
Show child attributes
streambooleanDefaults to `false`. When `true`, the response will be a SSE stream of events. Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
strict_toolsbooleanWhen set to `true`, tool calls in the Assistant message will be forced to follow the tool definition strictly. Learn more in the [Structured Outputs (Tools) guide](https://docs.cohere.com/docs/structured-outputs-json#structured-outputs-tools). **Note**: The first few requests with a new set of tools will take longer to process.
temperaturenumber · floatDefaults to `0.3`. A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations. Randomness can be further maximized by increasing the value of the `p` parameter.
thinkingobjectConfiguration for [reasoning features](https://docs.cohere.com/docs/reasoning).
Show child attributes
token_budgetintegerThe maximum number of tokens the model can use for thinking, which must be set to a positive integer. The model will stop thinking if it reaches the thinking token budget and will proceed with the response.
typestringrequiredReasoning is enabled by default for models that support it, but can be turned off by setting `"type": "disabled"`.
tool_choicevalueUsed to control whether or not the model will be forced to use a tool when answering. When `REQUIRED` is specified, the model will be forced to use at least one of the user-defined tools, and the `tools` parameter must be passed in the request. When `NONE` is specified, the model will be forced **not** to use one of the specified tools, and give a direct response. If tool_choice isn't specified, then the model is free to choose whether to use the specified tools or not. **Note**: This parameter is only compatible with models [Command-r7b](https://docs.cohere.com/v2/docs/command-r7b) and newer.
toolsarray of objectA list of tools (functions) available to the model. The model response may contain 'tool_calls' to the specified tools. Learn more in the [Tool Use guide](https://docs.cohere.com/docs/tools).
Show child attributes
Show array items
functionobjectThe function to be executed.
Show child attributes
descriptionstringThe description of the function.
namestringrequiredThe name of the function.
parametersobjectrequiredThe parameters of the function as a JSON schema.
typestringrequired{
"citation_options": {
"mode": "ACCURATE"
},
"documents": [
{
"data": {},
"id": "string"
}
],
"frequency_penalty": 0,
"k": 0,
"logprobs": true,
"max_tokens": 0,
"messages": [
{
"content": "string",
"role": "system"
}
],
"model": "string",
"p": 0.75,
"presence_penalty": 0,
"priority": 0,
"response_format": {
"type": "text"
},
"safety_mode": "CONTEXTUAL",
"seed": 0,
"stop_sequences": [
"string"
],
"stream": true,
"strict_tools": true,
"temperature": 0,
"thinking": {
"token_budget": 0,
"type": "disabled"
},
"tool_choice": "NONE",
"tools": [
{
"function": {
"description": "string",
"name": "string",
"parameters": {}
},
"type": "function"
}
]
}Responses
oneOf · 2 options
finish_reasonstringrequiredThe reason a chat request has finished. - **complete**: The model finished sending a complete message. - **max_tokens**: The number of generated tokens exceeded the model's context length or the value specified via the `max_tokens` parameter. - **stop_sequence**: One of the provided `stop_sequence` entries was reached in the model's generation. - **tool_call**: The model generated a Tool Call and is expecting a Tool Message in return - **error**: The generation failed due to an internal error - **timeout**: The generation was stopped because it exceeded the allowed time limit.
idstringrequiredUnique identifier for the generated reply. Useful for submitting feedback.
logprobsarray of objectShow child attributes
Show array items
logprobsarray of number · floatThe log probability of each token used to construct the text chunk.
Show child attributes
textstringThe text chunk for which the log probabilities was calculated.
token_idsarray of integerrequiredThe token ids of each token used to construct the text chunk.
Show child attributes
messageobjectrequiredA message from the assistant role can contain text and tool call information.
Show child attributes
citationsarray of objectShow child attributes
contentarray of valueShow child attributes
Show array items
oneOf · 2 options
rolestringrequiredtool_callsarray of objectShow child attributes
tool_planstringA chain-of-thought style reflection and plan that the model generates when working with Tools.
usageobjectShow child attributes
billed_unitsobjectShow child attributes
classificationsnumberThe number of billed classifications units.
input_tokensnumberThe number of billed input tokens.
output_tokensnumberThe number of billed output tokens.
search_unitsnumberThe number of billed search units.
cached_tokensnumberThe number of prompt tokens that hit the inference cache.
tokensobjectShow child attributes
input_tokensnumberThe number of tokens used as input to the model.
output_tokensnumberThe number of tokens produced by the model.
StreamedChatResponse is returned in streaming mode (specified with `stream=True` in the request).
oneOf · 10 options
A streamed event which signifies that a stream has started.
allOf · 2 options
The streamed event types
typestringrequireddeltaobjectShow child attributes
messageobjectShow child attributes
rolestringThe role of the message.
idstringUnique identifier for the generated reply.
A streamed delta event which signifies that a new content block has started.
allOf · 2 options
deltaobjectShow child attributes
messageobjectShow child attributes
contentobjectShow child attributes
textstringthinkingstringtypestringindexintegerA streamed delta event which contains a delta of chat text content.
allOf · 2 options
deltaobjectShow child attributes
messageobjectShow child attributes
contentobjectShow child attributes
textstringthinkingstringindexintegerA streamed delta event which signifies that the content block has ended.
allOf · 2 options
indexintegerA streamed event which contains a delta of tool plan text.
allOf · 2 options
deltaobjectShow child attributes
messageobjectShow child attributes
tool_planstringA streamed event delta which signifies a tool call has started streaming.
allOf · 2 options
deltaobjectShow child attributes
messageobjectShow child attributes
An array of tool calls to be made.
indexintegerA streamed event delta which signifies a delta in tool call arguments.
allOf · 2 options
deltaobjectShow child attributes
messageobjectShow child attributes
tool_callsobjectShow child attributes
functionobjectShow child attributes
argumentsstringindexintegerA streamed event delta which signifies a tool call has finished streaming.
allOf · 2 options
indexintegerA streamed event which signifies that the chat message has ended.
allOf · 2 options
deltaobjectShow child attributes
errorstringAn error message if an error occurred during the generation.
finish_reasonstringThe reason a chat request has finished. - **complete**: The model finished sending a complete message. - **max_tokens**: The number of generated tokens exceeded the model's context length or the value specified via the `max_tokens` parameter. - **stop_sequence**: One of the provided `stop_sequence` entries was reached in the model's generation. - **tool_call**: The model generated a Tool Call and is expecting a Tool Message in return - **error**: The generation failed due to an internal error - **timeout**: The generation was stopped because it exceeded the allowed time limit.
idstringallOf · 2 options
event_typestringrequiredpromptstring{
"finish_reason": "COMPLETE",
"id": "string",
"logprobs": [
{
"logprobs": [
0
],
"text": "string",
"token_ids": [
0
]
}
],
"message": {
"citations": [
{
"content_index": 0,
"end": 0,
"sources": [
{
"document": {
"additionalProp1": null
},
"id": "string",
"type": "document"
}
],
"start": 0,
"text": "string",
"type": "PLAN"
}
],
"content": [
{
"text": "string",
"type": "text"
}
],
"role": "assistant",
"tool_calls": [
{
"function": {
"arguments": "string",
"name": "string"
},
"id": "string",
"type": "function"
}
],
"tool_plan": "string"
},
"usage": {
"billed_units": {
"classifications": 0,
"input_tokens": 0,
"output_tokens": 0,
"search_units": 0
},
"cached_tokens": 0,
"tokens": {
"input_tokens": 0,
"output_tokens": 0
}
}
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}idstringmessagestring{
"id": "string",
"message": "string"
}