Text generation - quickstart

Cohere’s Command family of LLMs are available via the Chat endpoint. This endpoint enables you to build generative AI applications and facilitates a conversational interface for building chatbots.

This quickstart guide shows you how to perform text generation with the Chat endpoint.

1

Setup

First, install the Cohere Python SDK with the following command.

$pip install -U cohere

Next, import the library and create a client.

PYTHON
1import cohere
2
3co = cohere.ClientV2(
4 "COHERE_API_KEY"
5) # Get your free API key here: https://dashboard.cohere.com/api-keys
2

Basic Text Generation

To perform a basic text generation, call the Chat endpoint by passing the messages parameter containing the user message.

The model parameter definition for private deployments is the same as the Cohere platform, as shown below. Find more details on private deployments usage here.

PYTHON
1response = co.chat(
2 model="command-a-03-2025",
3 messages=[
4 {
5 "role": "user",
6 "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
7 }
8 ],
9)
10
11print(response.message.content[0].text)
1"Excited to be part of the Co1t team, I'm [Your Name], a [Your Role], passionate about [Your Area of Expertise] and looking forward to contributing to the company's success."
3

State Management

To maintain the state of a conversation, such as for building chatbots, append a sequence of user and assistant messages to the messages list. You can also include a system message at the start of the list to set the context of the conversation.

PYTHON
1messages = [
2 {
3 "role": "system",
4 "content": "You respond in concise sentences.",
5 },
6 {"role": "user", "content": "Hello"},
7]
8
9# User sends a message
10response = co.chat(
11 model="command-a-03-2025",
12 messages=messages,
13)
14
15# The model responds
16print(
17 response.message.content[0].text
18) # Hi, how can I help you today?
19
20# Append the model's response to the messages
21messages.append(response.message)
22
23# append another user message to the messages
24messages.append(
25 {
26 "role": "user",
27 "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
28 }
29)
30
31# get the model's second response
32response = co.chat(
33 model="command-a-03-2025",
34 messages=messages,
35)
36
37print(response.message.content[0].text)
1"Excited to join the team at Co1t, looking forward to contributing my skills and collaborating with everyone!"
4

Streaming

To stream text generation, call the Chat endpoint using chat_stream instead of chat. This returns a generator that yields chunk objects, which you can access the generated text from.

PYTHON
1res = co.chat_stream(
2 model="command-a-03-2025",
3 messages=[
4 {
5 "role": "user",
6 "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
7 }
8 ],
9)
10
11for chunk in res:
12 if chunk.type == "content-delta":
13 print(chunk.delta.message.content.text, end="")
1"Excited to be part of the Co1t team, I'm [Your Name], a [Your Role/Position], looking forward to contributing my skills and collaborating with this talented group to drive innovation and success."

Further Resources