Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Environment:Cohere ai Cohere python Python SDK Runtime

From Leeroopedia
Knowledge Sources
Domains Infrastructure, SDK
Last Updated 2026-02-15 14:00 GMT

Overview

Python 3.9+ runtime environment with core dependencies (httpx, pydantic, fastavro, tokenizers) required to run the Cohere Python SDK.

Description

This environment defines the base Python runtime and package dependencies needed to use the Cohere Python SDK (v5.x). The SDK is built on httpx for HTTP communication, pydantic (supporting both v1 and v2) for data modeling, fastavro for Avro serialization, and tokenizers for local tokenization. The SDK is OS-independent and runs on Linux, macOS, and Windows.

Usage

Use this environment for any interaction with the Cohere API through the Python SDK. This is the mandatory prerequisite for all workflows: Chat Completion, Streaming Chat, Text Embedding, Semantic Search, Model Finetuning, Tool Use, and AWS Bedrock Deployment.

System Requirements

Category Requirement Notes
OS Linux, macOS, or Windows OS-independent per pyproject.toml classifiers
Python >= 3.9 Supports 3.9 through 3.15; poetry constraint `^3.9`
Network Internet access Required for API calls and tokenizer config downloads

Dependencies

Python Packages

  • `httpx` >= 0.21.2 -- HTTP client for sync and async API requests
  • `pydantic` >= 1.9.2 -- Data validation and serialization (v1 and v2 supported)
  • `pydantic-core` >= 2.18.2 -- Core Pydantic engine
  • `fastavro` >= 1.9.4 -- Avro serialization for batch data
  • `tokenizers` >= 0.15, < 1.0 -- HuggingFace tokenizers for local tokenize/detokenize
  • `requests` >= 2.0.0 -- Used for tokenizer config download and HEAD requests
  • `typing_extensions` >= 4.0.0 -- Backport of typing features for Python 3.9-3.10
  • `types-requests` >= 2.0.0 -- Type stubs for requests

Optional Packages (AWS Integration)

  • `boto3` -- Required only for AWS Bedrock/SageMaker deployments
  • `botocore` -- Required only for SigV4 request signing
  • `sagemaker` -- Required only for SageMaker endpoint access

Credentials

The following environment variables are used:

  • `CO_API_KEY`: Primary Cohere API key (documented and preferred).
  • `COHERE_API_KEY`: Fallback Cohere API key (accepted but not documented).
  • `CO_API_URL`: Custom API base URL override (defaults to `https://api.cohere.com`).

Quick Install

# Install the Cohere SDK with all core dependencies
pip install cohere

# For AWS Bedrock/SageMaker support (optional)
pip install cohere boto3 botocore sagemaker

Code Evidence

API key resolution from `client.py:514-519`:

def _get_api_key_from_environment() -> typing.Optional[str]:
    """
    Retrieves the Cohere API key from specific environment variables.
    CO_API_KEY is preferred (and documented) COHERE_API_KEY is accepted (but not documented).
    """
    return os.getenv("CO_API_KEY", os.getenv("COHERE_API_KEY"))

Base URL resolution from `client.py:134-138`:

def __init__(
    self,
    api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None,
    *,
    base_url: typing.Optional[str] = os.getenv("CO_API_URL"),
    environment: ClientEnvironment = ClientEnvironment.PRODUCTION,
    ...
):

Production endpoint from `environment.py:3-4`:

class ClientEnvironment(enum.Enum):
    PRODUCTION = "https://api.cohere.com"

Pydantic version detection from `core/pydantic_utilities.py:35`:

IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.")

Common Errors

Error Message Cause Solution
`ImportError: AWS dependencies are not installed. Please install boto3, botocore, and sagemaker.` Attempting AWS Bedrock/SageMaker without AWS packages `pip install boto3 botocore sagemaker`
`ValueError: No tokenizer URL found for model {model}` Model does not have a tokenizer config URL Use API-based tokenization by setting `offline=False`
Missing `CO_API_KEY` results in `None` token No API key set in environment or constructor Set `CO_API_KEY` env var or pass `api_key` parameter

Compatibility Notes

  • Pydantic v1 vs v2: SDK supports both. Version detected at import time via `IS_PYDANTIC_V2`. Pydantic v2 users get `pydantic.v1` compatibility imports.
  • Python 3.9-3.10: `NotRequired` imported from `typing_extensions` instead of `typing`.
  • Tokenizers: The `tokenizers` package must be < 1.0 (breaking changes expected in v1.0).
  • httpx: Minimum 0.21.2 required; used for both sync and async HTTP clients.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment