Skip to main content

Cohere Python SDK

version badge license badge fern shield

The Cohere Python SDK allows access to Cohere models across many different platforms: the cohere platform, AWS (Bedrock, Sagemaker), Azure, GCP and Oracle OCI. For a full list of support and snippets, please take a look at the SDK support docs page.

Documentation

Cohere documentation and API reference is available here.

Installation

pip install cohere

Usage

import cohere

co = cohere.ClientV2()

response = co.chat(
    model="command-r-plus-08-2024",
    messages=[{"role": "user", "content": "hello world!"}],
)

print(response)

Streaming

The SDK supports streaming endpoints. To take advantage of this feature for chat, use chat_stream.

import cohere

co = cohere.ClientV2()

response = co.chat_stream(
    model="command-r-plus-08-2024",
    messages=[{"role": "user", "content": "hello world!"}],
)

for event in response:
    if event.type == "content-delta":
        print(event.delta.message.content.text, end="")

Oracle Cloud Infrastructure (OCI)

The SDK supports Oracle Cloud Infrastructure (OCI) Generative AI service. First, install the OCI SDK:

pip install 'cohere[oci]'

Then use the OciClient or OciClientV2:

import cohere

# Using OCI config file authentication (default: ~/.oci/config)
co = cohere.OciClient(
    oci_region="us-chicago-1",
    oci_compartment_id="ocid1.compartment.oc1...",
)

response = co.embed(
    model="embed-english-v3.0",
    texts=["Hello world"],
    input_type="search_document",
)

print(response.embeddings)

OCI Authentication Methods

1. Config File (Default)

co = cohere.OciClient(
    oci_region="us-chicago-1",
    oci_compartment_id="ocid1.compartment.oc1...",
    # Uses ~/.oci/config with DEFAULT profile
)

2. Custom Profile

co = cohere.OciClient(
    oci_profile="MY_PROFILE",
    oci_region="us-chicago-1",
    oci_compartment_id="ocid1.compartment.oc1...",
)

3. Session-based Authentication (Security Token)

# Works with OCI CLI session tokens
co = cohere.OciClient(
    oci_profile="MY_SESSION_PROFILE",  # Profile with security_token_file
    oci_region="us-chicago-1",
    oci_compartment_id="ocid1.compartment.oc1...",
)

4. Direct Credentials

co = cohere.OciClient(
    oci_user_id="ocid1.user.oc1...",
    oci_fingerprint="xx:xx:xx:...",
    oci_tenancy_id="ocid1.tenancy.oc1...",
    oci_private_key_path="~/.oci/key.pem",
    oci_region="us-chicago-1",
    oci_compartment_id="ocid1.compartment.oc1...",
)

5. Instance Principal (for OCI Compute instances)

co = cohere.OciClient(
    auth_type="instance_principal",
    oci_region="us-chicago-1",
    oci_compartment_id="ocid1.compartment.oc1...",
)

Supported OCI APIs

The OCI client supports the following Cohere APIs:

  • Embed: Full support for all embedding models
  • Chat: Full support with both V1 (OciClient) and V2 (OciClientV2) APIs
    • Streaming available via chat_stream()
    • Supports Command-R and Command-A model families

OCI Model Availability and Limitations

Available on OCI On-Demand Inference:

  • ✅ Embed models: available on OCI Generative AI
  • ✅ Chat models: available via OciClient (V1) and OciClientV2 (V2)

Not Available on OCI On-Demand Inference:

  • ❌ Generate API: OCI TEXT_GENERATION models are base models that require fine-tuning before deployment
  • ❌ Rerank API: OCI TEXT_RERANK models are base models that require fine-tuning before deployment
  • ❌ Multiple Embedding Types: OCI on-demand models only support single embedding type per request (cannot request both float and int8 simultaneously)

Note: To use Generate or Rerank models on OCI, you need to:

  1. Fine-tune the base model using OCI's fine-tuning service
  2. Deploy the fine-tuned model to a dedicated endpoint
  3. Update your code to use the deployed model endpoint

For the latest model availability, see the OCI Generative AI documentation.

Contributing

While we value open-source contributions to this SDK, the code is generated programmatically. Additions made directly would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

Metadata

Release files for cohere 7.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cohere 7.3.2
File Size Uploaded Provenance Actions
cohere-7.3.2.tar.gz 222.6 kB Without provenance Details for cohere-7.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cohere 7.3.2
File Interpreter ABI Platform Actions
cohere-7.3.2-py3-none-any.whl Python 3 none any Details for cohere-7.3.2-py3-none-any.whl

Total release size: 598.9 kB

Release files / cohere-7.3.2.tar.gz

Download URL cohere-7.3.2.tar.gz
Size 222.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b75e17dff0d5d65fe9af80e86292f2f7081018d00b1bdddf3d13aa046836fe64
BLAKE2b-256 checksum
How to use checksums
284b82434cb3c221b6be27c582cd636486b1be5c6f6fbc79a66eb6e366ead8ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.22 Linux/7.0.0-1020-azure

Release files / cohere-7.3.2-py3-none-any.whl

Download URL cohere-7.3.2-py3-none-any.whl
Size 376.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
606efe69b318f6b0b841a20e947fd9615946b3e56c1152e7a9036b3146a0f32d
BLAKE2b-256 checksum
How to use checksums
908b3b60e8991765e02af8fb3e7c75cc6ec3c1b324891876d12036b4c2c3e935
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.22 Linux/7.0.0-1020-azure

Security for cohere 7.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

7.3.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page