Skip to content

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:

Package.swift
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")
]
)
]

You can import the default provider instance cohere from CohereProvider:

import CohereProvider
// Use the global cohere instance
let 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 Authorization header. It defaults to the COHERE_API_KEY environment variable.

  • headers [String: String]

    Custom headers to include in the requests.

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")

You can use Cohere language models to generate text with the generateText function:

import SwiftAISDK
import CohereProvider
let result = try await generateText(
model: cohere("command-r-plus"),
prompt: "Write a vegetarian lasagna recipe for 4 people."
)
let text = result.text

Cohere language models can also be used in the streamText and generateObject functions.

ModelImage InputObject GenerationTool UsageTool 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

Cohere has introduced reasoning with the command-a-reasoning-08-2025 model. You can learn more at https://docs.cohere.com/docs/reasoning.

import SwiftAISDK
import 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)

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 SwiftAISDK
import 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.embedding

Cohere embedding models support additional provider options:

import SwiftAISDK
import 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.embedding

The 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.
ModelEmbedding Dimensions
embed-english-v3.01024
embed-multilingual-v3.01024
embed-english-light-v3.0384
embed-multilingual-light-v3.0384
embed-english-v2.04096
embed-english-light-v2.01024
embed-multilingual-v2.0768