Using Cohere models via the OpenAI SDK
The Compatibility API allows developers to use Cohere’s models through OpenAI’s SDK.
It makes it easy to switch existing OpenAI-based applications to use Cohere’s models while still maintaining the use of OpenAI SDK — no big refactors needed.
The supported libraries are:
- TypeScript / JavaScript
- Python
- .NET
- Java (beta)
- Go (beta)
This is a quickstart guide to help you get started with the Compatibility API.
Installation
First, install the OpenAI SDK and import the package.
Then, create a client and configure it with the compatibility API base URL and your Cohere API key.
Python
TypeScript
Basic chat completions
Here’s a basic example of using the Chat Completions API.
Python
TypeScript
cURL
Example response (via the Python SDK):
Chat with streaming
To stream the response, set the stream parameter to True.
Python
TypeScript
cURL
Example response (via the Python SDK):
State management
For state management, use the messages parameter to build the conversation history.
You can include a system message via the developer role and the multiple chat turns between the user and assistant.
Python
TypeScript
cURL
Example response (via the Python SDK):
Structured outputs
The Structured Outputs feature allows you to specify the schema of the model response. It guarantees that the response will strictly follow the schema.
To use it, set the response_format parameter to the JSON Schema of the desired output.
Python
TypeScript
cURL
Example response (via the Python SDK):
Tool use (function calling)
You can utilize the tool use feature by passing a list of tools to the tools parameter in the API call.
Specifying the strict parameter to True in the tool calling step will guarantee that every generated tool call follows the specified tool schema.
Python
TypeScript
cURL
Example response (via the Python SDK):
Embeddings
You can generate text embeddings Embeddings API by passing a list of strings as the input parameter. You can also specify in encoding_format the format of embeddings to be generated. Can be either float or base64.
Python
TypeScript
cURL
Example response (via the Python SDK):
Supported parameters
The following is the list supported parameters in the Compatibility API, including those that are not explicitly demonstrated in the examples above:
Chat completions
modelmessagesstreamreasoning_effort(Only “none” and “high” are currently supported.)response_formattoolstemperaturemax_tokensstopseedtop_pfrequency_penaltypresence_penalty
Note
Currently, onlynoneandhighare supported forreasoning_effort.
These correspond to passing{ thinking: { type: "disabled" \| "enabled" } }in Chat V2.
Passingmediumorlowis not supported at this time.
Embeddings
inputmodelencoding_format
Unsupported parameters
The following parameters are not supported in the Compatibility API:
Chat completions
storemetadatalogit_biastop_logprobsnmodalitiespredictionaudioservice_tierparallel_tool_calls
Embeddings
dimensionsuser
Cohere-specific parameters
Parameters that are uniquely available on the Cohere API but not on the OpenAI SDK are not supported.
Chat endpoint:
connectorsdocumentscitation_options- …more here
Embed endpoint:
input_typeimagestruncate- …more here