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.
Authentication
AuthorizationBearer
Bearer authentication of the form Bearer <token>, where token is your auth token.
Headers
X-Client-NamestringOptional
The name of the project that is making the request.
Request
This endpoint expects an object.
streamfalseRequired
Defaults 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.
A 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.
citation_optionsobjectOptional
Options for controlling citation generation.
response_formatobjectOptional
Configuration 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.
safety_modeenumOptional
Used 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.
Allowed values:
max_tokensintegerOptional
The 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.
stop_sequenceslist of stringsOptional
A 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.
temperaturedoubleOptional
Defaults 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.
seedintegerOptional0-18446744073709552000
If 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.
frequency_penaltydoubleOptional
Defaults 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.
presence_penaltydoubleOptional
Defaults 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.
kintegerOptional0-500Defaults to 0
Ensures 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.
pdoubleOptional
Ensures 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.
logprobsbooleanOptional
Defaults to false. When set to true, the log probabilities of the generated tokens will be included in the response.
tool_choiceenumOptional
Used 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.
Controls 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.
strict_toolsbooleanOptionalBeta
When 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.
Response
idstring
Unique identifier for the generated reply. Useful for submitting feedback.
finish_reasonenum
The 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.
messageobject
A message from the assistant role can contain text and tool call information.
Configuration for forcing the model output to adhere to the specified format. Supported on Command R, Command R+ and newer models.
The model can be forced into outputting JSON objects by setting { "type": "json_object" }.
A JSON Schema 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.
Used to select the safety instruction 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: command-r7b-12-2024 and newer models only support "CONTEXTUAL" and "STRICT" modes.
The 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.
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.
Used 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 and newer.
When 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.
Note: The first few requests with a new set of tools will take longer to process.
The 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.