Cohere on Oracle Cloud Infrastructure (OCI)

The Cohere Python SDK natively supports Oracle Cloud Infrastructure (OCI) Generative AI service. With pip install cohere[oci], you get OciClient and OciClientV2 classes that behave identically to the Cohere-hosted Client and ClientV2 — same methods, same response types, same streaming format. Switching from Cohere’s hosted API to OCI Generative AI means changing one constructor.

Under the hood, the SDK handles URL rewriting, request and response format translation, OCI cryptographic request signing, and streaming event transformation. Your application code never sees the OCI-specific details.

Available Models

The SDK supports all Cohere models available on OCI Generative AI, including the Command A family (via OciClientV2), the Command R family (via OciClient), Embed models, and Rerank models. For the current list of available models and their IDs, see the OCI Generative AI pretrained models documentation.

Installation

$pip install cohere[oci]

This installs the Cohere SDK along with the OCI SDK dependency required for authentication and request signing.

Quick Start

Chat with Command A (V2 API)

1import cohere
2
3client = cohere.OciClientV2(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8response = client.chat(
9 model="command-a-03-2025",
10 messages=[
11 {"role": "system", "content": "You are a helpful assistant."},
12 {
13 "role": "user",
14 "content": "Explain RAG in three sentences.",
15 },
16 ],
17)
18
19print(response.message.content[0].text)

Chat with Command R (V1 API)

1import cohere
2
3client = cohere.OciClient(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8response = client.chat(
9 model="command-r-plus-08-2024",
10 message="Explain RAG in three sentences.",
11)
12
13print(response.text)

Embeddings

1import cohere
2
3client = cohere.OciClientV2(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8response = client.embed(
9 model="embed-english-v3.0",
10 texts=["Oracle Cloud Infrastructure", "Generative AI service"],
11 input_type="search_document",
12)
13
14for i, embedding in enumerate(response.embeddings.float_):
15 print(f"Text {i}: {len(embedding)} dimensions")

Streaming (V2)

1import cohere
2
3client = cohere.OciClientV2(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8for event in client.chat_stream(
9 model="command-a-03-2025",
10 messages=[
11 {"role": "user", "content": "Explain RAG in three sentences."}
12 ],
13):
14 if event.type == "content-delta":
15 print(event.delta.message.content.text, end="")

Streaming (V1)

1import cohere
2
3client = cohere.OciClient(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8for event in client.chat_stream(
9 model="command-r-plus-08-2024",
10 message="Explain RAG in three sentences.",
11):
12 if hasattr(event, "text") and event.text:
13 print(event.text, end="")

The SDK transforms OCI’s streaming format to match Cohere’s standard streaming events. V2 uses message-start, content-delta, content-end, message-end; V1 uses stream-start, text-generation, stream-end.

Authentication

The SDK supports five authentication methods, covering every deployment scenario from local development to serverless production.

1. Config File (Default)

Uses ~/.oci/config with the DEFAULT profile. No additional parameters needed beyond region and compartment.

1client = cohere.OciClientV2(
2 oci_region="us-chicago-1",
3 oci_compartment_id="ocid1.compartment.oc1...",
4)

2. Custom Profile

Use a specific profile from your OCI config file.

1client = cohere.OciClientV2(
2 oci_profile="MY_PROFILE",
3 oci_region="us-chicago-1",
4 oci_compartment_id="ocid1.compartment.oc1...",
5)

3. Session-based Authentication

Works with OCI CLI session tokens. The SDK automatically re-reads the token file on each request, so oci session refresh is picked up without restarting the client.

1client = cohere.OciClientV2(
2 oci_profile="MY_SESSION_PROFILE", # Profile with security_token_file
3 oci_region="us-chicago-1",
4 oci_compartment_id="ocid1.compartment.oc1...",
5)

4. Direct Credentials

Pass OCI credentials directly without a config file. Useful for CI/CD pipelines or containerized deployments.

1client = cohere.OciClientV2(
2 oci_user_id="ocid1.user.oc1...",
3 oci_fingerprint="xx:xx:xx:...",
4 oci_tenancy_id="ocid1.tenancy.oc1...",
5 oci_private_key_path="~/.oci/key.pem",
6 oci_region="us-chicago-1",
7 oci_compartment_id="ocid1.compartment.oc1...",
8)

5. Instance Principal

For applications running on OCI Compute instances. No credentials needed — the instance’s identity is used automatically.

1client = cohere.OciClientV2(
2 auth_type="instance_principal",
3 oci_region="us-chicago-1",
4 oci_compartment_id="ocid1.compartment.oc1...",
5)

6. Resource Principal

For OCI Functions (serverless). Zero credentials in the deployment — the function inherits the compartment’s security posture.

1client = cohere.OciClientV2(
2 auth_type="resource_principal",
3 oci_region="us-chicago-1",
4 oci_compartment_id="ocid1.compartment.oc1...",
5)

V1 vs V2 API

The SDK provides two client classes that map to the two OCI Generative AI API formats:

OciClient (V1)OciClientV2 (V2)
Chat modelsCommand R familyCommand A family
Chat formatSingle message stringmessages array
Streaming eventstext-generation, stream-endmessage-start, content-delta, message-end
Embed responseresponse.embeddings (list of floats)response.embeddings.float_ (dict by type)
Tool usetools + tool_resultstools + tool_calls + tool_choice
ThinkingNot supportedSupported via thinking parameter

Tool Use (V2)

Command A supports native tool use on OCI Generative AI. Define tools and the model will return tool_calls with structured arguments.

1import cohere
2
3client = cohere.OciClientV2(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8response = client.chat(
9 model="command-a-03-2025",
10 messages=[
11 {"role": "user", "content": "What's the weather in Toronto?"}
12 ],
13 max_tokens=200,
14 tools=[
15 {
16 "type": "function",
17 "function": {
18 "name": "get_weather",
19 "description": "Get current weather for a location",
20 "parameters": {
21 "type": "object",
22 "properties": {
23 "location": {
24 "type": "string",
25 "description": "City name",
26 }
27 },
28 "required": ["location"],
29 },
30 },
31 }
32 ],
33)
34
35if response.message.tool_calls:
36 for tc in response.message.tool_calls:
37 print(f"{tc.function.name}({tc.function.arguments})")
38# Output: get_weather({"location":"Toronto"})

Vision (V2)

Command A Vision can reason over images alongside text. Pass images as base64 data URIs or URLs in the message content.

1import cohere
2import base64
3
4client = cohere.OciClientV2(
5 oci_region="us-chicago-1",
6 oci_compartment_id="ocid1.compartment.oc1...",
7)
8
9# Read and encode an image
10with open("document.png", "rb") as f:
11 img_b64 = base64.b64encode(f.read()).decode()
12
13response = client.chat(
14 model="command-a-vision",
15 messages=[
16 {
17 "role": "user",
18 "content": [
19 {
20 "type": "text",
21 "text": "Describe what you see in this image.",
22 },
23 {
24 "type": "image_url",
25 "image_url": {
26 "url": f"data:image/png;base64,{img_b64}"
27 },
28 },
29 ],
30 }
31 ],
32)
33
34print(response.message.content[0].text)

Embed v4

Embed v4 is Cohere’s latest embedding model with 1536 dimensions, available alongside the Embed v3 family.

1import cohere
2
3client = cohere.OciClientV2(
4 oci_region="us-chicago-1",
5 oci_compartment_id="ocid1.compartment.oc1...",
6)
7
8response = client.embed(
9 model="embed-v4.0",
10 texts=["Oracle Cloud Infrastructure", "Generative AI service"],
11 input_type="search_document",
12)
13
14for i, embedding in enumerate(response.embeddings.float_):
15 print(f"Text {i}: {len(embedding)} dimensions")
16# Output: 1536 dimensions per text

Supported Features

FeatureOCI Support
chatSupported
chat_streamSupported
embedSupported
rerankDedicated endpoints only
generateNot supported (OCI base models require fine-tuning)
classifyNot supported
summarizeNot supported
tokenizeOffline only
detokenizeOffline only

End-to-End Example

The following example demonstrates a complete application flow on OCI Generative AI: embedding documents for a knowledge base, retrieving relevant context, using tool calling for live data, processing images with vision, and streaming a final response.

1import cohere
2import base64
3
4# Initialize V2 client for Command A models
5client = cohere.OciClientV2(
6 oci_region="us-chicago-1",
7 oci_compartment_id="ocid1.compartment.oc1...",
8)
9
10# --- Step 1: Build a knowledge base with embeddings ---
11
12documents = [
13 "Oracle Cloud Infrastructure provides enterprise-grade AI services.",
14 "Cohere Command A is a 111B parameter model with 256K context window.",
15 "OCI Generative AI is FedRAMP High and DISA IL5 authorized.",
16]
17
18doc_embeddings = client.embed(
19 model="embed-english-v3.0",
20 texts=documents,
21 input_type="search_document",
22).embeddings.float_
23
24query_embedding = client.embed(
25 model="embed-english-v3.0",
26 texts=["What security certifications does OCI have?"],
27 input_type="search_query",
28).embeddings.float_[0]
29
30# Find the most relevant document (cosine similarity)
31best_idx = max(
32 range(len(documents)),
33 key=lambda i: sum(
34 a * b for a, b in zip(query_embedding, doc_embeddings[i])
35 ),
36)
37print(f"Best match: {documents[best_idx]}")
38
39# --- Step 2: Grounded chat with retrieved context ---
40
41response = client.chat(
42 model="command-a-03-2025",
43 messages=[
44 {
45 "role": "system",
46 "content": "Answer based on the provided context only.",
47 },
48 {
49 "role": "user",
50 "content": f"Context: {documents[best_idx]}\n\nWhat certifications does OCI have?",
51 },
52 ],
53 temperature=0.3,
54)
55print(f"Answer: {response.message.content[0].text}")
56
57# --- Step 3: Tool use — call an external API ---
58
59response = client.chat(
60 model="command-a-03-2025",
61 messages=[
62 {
63 "role": "user",
64 "content": "What's the current stock price of ORCL?",
65 }
66 ],
67 tools=[
68 {
69 "type": "function",
70 "function": {
71 "name": "get_stock_price",
72 "description": "Get the current stock price for a ticker symbol",
73 "parameters": {
74 "type": "object",
75 "properties": {
76 "ticker": {
77 "type": "string",
78 "description": "Stock ticker symbol",
79 }
80 },
81 "required": ["ticker"],
82 },
83 },
84 }
85 ],
86)
87
88# Model returns a tool call
89tool_call = response.message.tool_calls[0]
90print(
91 f"Tool call: {tool_call.function.name}({tool_call.function.arguments})"
92)
93
94# Send the tool result back
95final = client.chat(
96 model="command-a-03-2025",
97 messages=[
98 {
99 "role": "user",
100 "content": "What's the current stock price of ORCL?",
101 },
102 {
103 "role": "assistant",
104 "tool_calls": [
105 {
106 "id": tool_call.id,
107 "type": "function",
108 "function": {
109 "name": tool_call.function.name,
110 "arguments": tool_call.function.arguments,
111 },
112 }
113 ],
114 "tool_plan": response.message.tool_plan,
115 },
116 {
117 "role": "tool",
118 "tool_call_id": tool_call.id,
119 "content": [
120 {
121 "type": "text",
122 "text": '{"ticker": "ORCL", "price": 187.42, "currency": "USD"}',
123 }
124 ],
125 },
126 ],
127 tools=[
128 {
129 "type": "function",
130 "function": {
131 "name": "get_stock_price",
132 "description": "Get the current stock price for a ticker symbol",
133 "parameters": {
134 "type": "object",
135 "properties": {"ticker": {"type": "string"}},
136 "required": ["ticker"],
137 },
138 },
139 }
140 ],
141)
142print(f"Final answer: {final.message.content[0].text}")
143
144# --- Step 4: Vision — analyze an image ---
145
146with open("chart.png", "rb") as f:
147 img_b64 = base64.b64encode(f.read()).decode()
148
149response = client.chat(
150 model="command-a-vision",
151 messages=[
152 {
153 "role": "user",
154 "content": [
155 {
156 "type": "text",
157 "text": "Describe the trend shown in this chart.",
158 },
159 {
160 "type": "image_url",
161 "image_url": {
162 "url": f"data:image/png;base64,{img_b64}"
163 },
164 },
165 ],
166 }
167 ],
168)
169print(f"Vision: {response.message.content[0].text}")
170
171# --- Step 5: Stream a response in real time ---
172
173print("Streaming: ", end="")
174for event in client.chat_stream(
175 model="command-a-03-2025",
176 messages=[
177 {
178 "role": "user",
179 "content": "Summarize why enterprises choose OCI for AI.",
180 }
181 ],
182):
183 if event.type == "content-delta":
184 print(event.delta.message.content.text, end="")
185print()

Additional Resources

You can also work with Cohere models on OCI through the OCI Console, the OCI CLI, or the OCI API directly.