Cohere
The Cohere provider contains language and embedding model support for the Cohere chat API.
The Cohere provider is available in the CohereProvider module. Add it to your Swift package:
dependencies: [ .package(url: "https://github.com/teunlao/swift-ai-sdk", from: "0.2.1")],targets: [ .target( name: "YourTarget", dependencies: [ .product(name: "SwiftAISDK", package: "swift-ai-sdk"), .product(name: "CohereProvider", package: "swift-ai-sdk") ] )]Provider Instance
Section titled “Provider Instance”You can import the default provider instance cohere from CohereProvider:
import CohereProvider
// Use the global cohere instancelet model = cohere("command-r-plus")If you need a customized setup, you can use createCohereProvider to create a provider instance with your settings:
import CohereProvider
let customCohere = createCohereProvider( settings: CohereProviderSettings( baseURL: "https://custom.api.com/v2", apiKey: "your-api-key" ))You can use the following optional settings to customize the Cohere provider instance:
-
baseURL String
Use a different URL prefix for API calls, e.g. to use proxy servers. The default prefix is
https://api.cohere.com/v2. -
apiKey String
API key that is being sent using the
Authorizationheader. It defaults to theCOHERE_API_KEYenvironment variable. -
headers [String: String]
Custom headers to include in the requests.
Language Models
Section titled “Language Models”You can create models that call the Cohere chat API using a provider instance.
The first argument is the model id, e.g. command-r-plus.
Some Cohere chat models support tool calls.
let model = cohere("command-r-plus")Example
Section titled “Example”You can use Cohere language models to generate text with the generateText function:
import SwiftAISDKimport CohereProvider
let result = try await generateText( model: cohere("command-r-plus"), prompt: "Write a vegetarian lasagna recipe for 4 people.")let text = result.textCohere language models can also be used in the streamText and generateObject functions.
Model Capabilities
Section titled “Model Capabilities”| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
|---|---|---|---|---|
command-a-03-2025 | ||||
command-a-reasoning-08-2025 | ||||
command-r7b-12-2024 | ||||
command-r-plus-04-2024 | ||||
command-r-plus | ||||
command-r-08-2024 | ||||
command-r-03-2024 | ||||
command-r | ||||
command | ||||
command-nightly | ||||
command-light | ||||
command-light-nightly |
Reasoning
Section titled “Reasoning”Cohere has introduced reasoning with the command-a-reasoning-08-2025 model. You can learn more at https://docs.cohere.com/docs/reasoning.
import SwiftAISDKimport CohereProvider
let result = try await generateText( model: cohere("command-a-reasoning-08-2025"), prompt: "Alice has 3 brothers and she also has 2 sisters. How many sisters does Alice's brother have?", // optional: reasoning options providerOptions: CohereChatOptions( thinking: .enabled(tokenBudget: 100) ))
print(result.reasoning ?? "")print(result.text)Embedding Models
Section titled “Embedding Models”You can create models that call the Cohere embed API
using the .textEmbedding() factory method.
let model = cohere.textEmbedding("embed-english-v3.0")You can use Cohere embedding models to generate embeddings with the embed function:
import SwiftAISDKimport CohereProvider
let result = try await embed( model: cohere.textEmbedding("embed-english-v3.0"), value: "sunny day at the beach", providerOptions: CohereEmbeddingOptions( inputType: .searchDocument ))let embedding = result.embeddingCohere embedding models support additional provider options:
import SwiftAISDKimport CohereProvider
let result = try await embed( model: cohere.textEmbedding("embed-english-v3.0"), value: "sunny day at the beach", providerOptions: CohereEmbeddingOptions( inputType: .searchDocument, truncate: .end ))let embedding = result.embeddingThe following provider options are available:
-
inputType CohereInputType
Specifies the type of input passed to the model. Default is
.searchQuery..searchDocument: Used for embeddings stored in a vector database for search use-cases..searchQuery: Used for embeddings of search queries run against a vector DB to find relevant documents..classification: Used for embeddings passed through a text classifier..clustering: Used for embeddings run through a clustering algorithm.
-
truncate CohereTruncateType
Specifies how the API will handle inputs longer than the maximum token length. Default is
.end..none: If selected, when the input exceeds the maximum input token length will return an error..start: Will discard the start of the input until the remaining input is exactly the maximum input token length for the model..end: Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
Model Capabilities
Section titled “Model Capabilities”| Model | Embedding Dimensions |
|---|---|
embed-english-v3.0 | 1024 |
embed-multilingual-v3.0 | 1024 |
embed-english-light-v3.0 | 384 |
embed-multilingual-light-v3.0 | 384 |
embed-english-v2.0 | 4096 |
embed-english-light-v2.0 | 1024 |
embed-multilingual-v2.0 | 768 |