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
Section titled “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
Section titled “Installation”pip install cohere[oci]This installs the Cohere SDK along with the OCI SDK dependency required for authentication and request signing.
Quick Start
Section titled “Quick Start”Chat with Command A (V2 API)
Section titled “Chat with Command A (V2 API)”import cohere
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = client.chat(
model="command-a-03-2025",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "Explain RAG in three sentences.",
},
],
)
print(response.message.content[0].text)Chat with Command R (V1 API)
Section titled “Chat with Command R (V1 API)”import cohere
client = cohere.OciClient(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = client.chat(
model="command-r-plus-08-2024",
message="Explain RAG in three sentences.",
)
print(response.text)Embeddings
Section titled “Embeddings”import cohere
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = client.embed(
model="embed-english-v3.0",
texts=["Oracle Cloud Infrastructure", "Generative AI service"],
input_type="search_document",
)
for i, embedding in enumerate(response.embeddings.float_):
print(f"Text {i}: {len(embedding)} dimensions")Streaming (V2)
Section titled “Streaming (V2)”import cohere
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
for event in client.chat_stream(
model="command-a-03-2025",
messages=[
{"role": "user", "content": "Explain RAG in three sentences."}
],
):
if event.type == "content-delta":
print(event.delta.message.content.text, end="")Streaming (V1)
Section titled “Streaming (V1)”import cohere
client = cohere.OciClient(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
for event in client.chat_stream(
model="command-r-plus-08-2024",
message="Explain RAG in three sentences.",
):
if hasattr(event, "text") and event.text:
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
Section titled “Authentication”The SDK supports five authentication methods, covering every deployment scenario from local development to serverless production.
1. Config File (Default)
Section titled “1. Config File (Default)”Uses ~/.oci/config with the DEFAULT profile. No additional parameters needed beyond region and compartment.
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)2. Custom Profile
Section titled “2. Custom Profile”Use a specific profile from your OCI config file.
client = cohere.OciClientV2(
oci_profile="MY_PROFILE",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)3. Session-based Authentication
Section titled “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.
client = cohere.OciClientV2(
oci_profile="MY_SESSION_PROFILE", # Profile with security_token_file
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)4. Direct Credentials
Section titled “4. Direct Credentials”Pass OCI credentials directly without a config file. Useful for CI/CD pipelines or containerized deployments.
client = cohere.OciClientV2(
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
Section titled “5. Instance Principal”For applications running on OCI Compute instances. No credentials needed -- the instance's identity is used automatically.
client = cohere.OciClientV2(
auth_type="instance_principal",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)6. Resource Principal
Section titled “6. Resource Principal”For OCI Functions (serverless). Zero credentials in the deployment -- the function inherits the compartment's security posture.
client = cohere.OciClientV2(
auth_type="resource_principal",
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)V1 vs V2 API
Section titled “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 models | Command R family | Command A family |
| Chat format | Single message string |
messages array |
| Streaming events | text-generation, stream-end |
message-start, content-delta, message-end |
| Embed response | response.embeddings (list of floats) |
response.embeddings.float_ (dict by type) |
| Tool use | tools + tool_results |
tools + tool_calls + tool_choice |
| Thinking | Not supported | Supported via thinking parameter |
Tool Use (V2)
Section titled “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.
import cohere
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = client.chat(
model="command-a-03-2025",
messages=[
{"role": "user", "content": "What's the weather in Toronto?"}
],
max_tokens=200,
tools=[
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name",
}
},
"required": ["location"],
},
},
}
],
)
if response.message.tool_calls:
for tc in response.message.tool_calls:
print(f"{tc.function.name}({tc.function.arguments})")
# Output: get_weather({"location":"Toronto"})Vision (V2)
Section titled “Vision (V2)”Command A Vision can reason over images alongside text. Pass images as base64 data URIs or URLs in the message content.
import cohere
import base64
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
# Read and encode an image
with open("document.png", "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()
response = client.chat(
model="command-a-vision",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe what you see in this image.",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{img_b64}"
},
},
],
}
],
)
print(response.message.content[0].text)Embed v4
Section titled “Embed v4”Embed v4 is Cohere's latest embedding model with 1536 dimensions, available alongside the Embed v3 family.
import cohere
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
response = client.embed(
model="embed-v4.0",
texts=["Oracle Cloud Infrastructure", "Generative AI service"],
input_type="search_document",
)
for i, embedding in enumerate(response.embeddings.float_):
print(f"Text {i}: {len(embedding)} dimensions")
# Output: 1536 dimensions per textSupported Features
Section titled “Supported Features”| Feature | OCI Support |
|---|---|
chat |
Supported |
chat_stream |
Supported |
embed |
Supported |
rerank |
Dedicated endpoints only |
generate |
Not supported (OCI base models require fine-tuning) |
classify |
Not supported |
summarize |
Not supported |
tokenize |
Offline only |
detokenize |
Offline only |
End-to-End Example
Section titled “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.
import cohere
import base64
# Initialize V2 client for Command A models
client = cohere.OciClientV2(
oci_region="us-chicago-1",
oci_compartment_id="ocid1.compartment.oc1...",
)
# --- Step 1: Build a knowledge base with embeddings ---
documents = [
"Oracle Cloud Infrastructure provides enterprise-grade AI services.",
"Cohere Command A is a 111B parameter model with 256K context window.",
"OCI Generative AI is FedRAMP High and DISA IL5 authorized.",
]
doc_embeddings = client.embed(
model="embed-english-v3.0",
texts=documents,
input_type="search_document",
).embeddings.float_
query_embedding = client.embed(
model="embed-english-v3.0",
texts=["What security certifications does OCI have?"],
input_type="search_query",
).embeddings.float_[0]
# Find the most relevant document (cosine similarity)
best_idx = max(
range(len(documents)),
key=lambda i: sum(
a * b for a, b in zip(query_embedding, doc_embeddings[i])
),
)
print(f"Best match: {documents[best_idx]}")
# --- Step 2: Grounded chat with retrieved context ---
response = client.chat(
model="command-a-03-2025",
messages=[
{
"role": "system",
"content": "Answer based on the provided context only.",
},
{
"role": "user",
"content": f"Context: {documents[best_idx]}\n\nWhat certifications does OCI have?",
},
],
temperature=0.3,
)
print(f"Answer: {response.message.content[0].text}")
# --- Step 3: Tool use — call an external API ---
response = client.chat(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": "What's the current stock price of ORCL?",
}
],
tools=[
{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "Get the current stock price for a ticker symbol",
"parameters": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "Stock ticker symbol",
}
},
"required": ["ticker"],
},
},
}
],
)
# Model returns a tool call
tool_call = response.message.tool_calls[0]
print(
f"Tool call: {tool_call.function.name}({tool_call.function.arguments})"
)
# Send the tool result back
final = client.chat(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": "What's the current stock price of ORCL?",
},
{
"role": "assistant",
"tool_calls": [
{
"id": tool_call.id,
"type": "function",
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments,
},
}
],
"tool_plan": response.message.tool_plan,
},
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": [
{
"type": "text",
"text": '{"ticker": "ORCL", "price": 187.42, "currency": "USD"}',
}
],
},
],
tools=[
{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "Get the current stock price for a ticker symbol",
"parameters": {
"type": "object",
"properties": {"ticker": {"type": "string"}},
"required": ["ticker"],
},
},
}
],
)
print(f"Final answer: {final.message.content[0].text}")
# --- Step 4: Vision — analyze an image ---
with open("chart.png", "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()
response = client.chat(
model="command-a-vision",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe the trend shown in this chart.",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{img_b64}"
},
},
],
}
],
)
print(f"Vision: {response.message.content[0].text}")
# --- Step 5: Stream a response in real time ---
print("Streaming: ", end="")
for event in client.chat_stream(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": "Summarize why enterprises choose OCI for AI.",
}
],
):
if event.type == "content-delta":
print(event.delta.message.content.text, end="")
print()Additional Resources
Section titled “Additional Resources”You can also work with Cohere models on OCI through the OCI Console, the OCI CLI, or the OCI API directly.