Text generation - quickstart

About text generation

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
import cohere
co = cohere.ClientV2(
"COHERE_API_KEY"
) # 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.

With reasoning models such as Command A+, the response content list can include a thinking block before the final text block. Iterate over the content items and check each item’s type instead of assuming content[0] is text. For more information, see the Reasoning page.

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
response = co.chat(
model="command-a-plus-05-2026",
messages=[
{
"role": "user",
"content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
}
],
)
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print("text:", content_item.text)
"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
messages = [
{
"role": "system",
"content": "You respond in concise sentences.",
},
{"role": "user", "content": "Hello"},
]
# User sends a message
response = co.chat(
model="command-a-plus-05-2026",
messages=messages,
)
# The model responds
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print(
"text:", content_item.text
) # Hi, how can I help you today?
# Append the model's response to the messages
messages.append(response.message)
# append another user message to the messages
messages.append(
{
"role": "user",
"content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
}
)
# get the model's second response
response = co.chat(
model="command-a-plus-05-2026",
messages=messages,
)
for content_item in response.message.content:
if content_item.type == "thinking":
print("thinking:", content_item.thinking)
if content_item.type == "text":
print("text:", content_item.text)
"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
res = co.chat_stream(
model="command-a-plus-05-2026",
messages=[
{
"role": "user",
"content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
}
],
)
for chunk in res:
if chunk.type == "content-delta":
if chunk.delta.message.content.thinking:
print(chunk.delta.message.content.thinking, end="")
if chunk.delta.message.content.text:
print(chunk.delta.message.content.text, end="")
"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