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.
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
model
messages
stream
response_format
tools
temperature
max_tokens
stop
seed
top_p
frequency_penalty
presence_penalty
Embeddings
input
model
encoding_format
Unsupported parameters
The following parameters are not supported in the Compatibility API:
Chat completions
tool_choice
store
reasoning_effort
metadata
logit_bias
logprobs
top_logprobs
max_completion_tokens
n
modalities
prediction
audio
service_tier
stream_options
parallel_tool_calls
user
Embeddings
dimensions
user