The OCI GenAI Auth Python library provides OCI request-signing helpers for the OpenAI-compatible REST APIs hosted by OCI Generative AI. Partner/Passthrough endpoints do not store conversation history on OCI servers, while AgentHub (non-passthrough) stores data on OCI-managed servers.
- Before you start
- Using OCI IAM Auth
- Using API Key Auth
- Using AgentHub APIs (non-passthrough)
- Using Partner APIs (passthrough)
- Running the Examples
Important!
Note that this package, as well as API keys package described below, only supports OpenAI, xAi Grok and Meta LLama models on OCI Generative AI.
Before you start using this package, determine if this is the right option for you.
If you are looking for a seamless way to port your code from an OpenAI compatible endpoint to OCI Generative AI endpoint, and you are currently using OpenAI-style API keys, you might want to use OCI Generative AI API Keys instead.
With OCI Generative AI API Keys, use the native openai SDK like before. Just update the base_url, create API keys in your OCI console, insure the policy granting the key access to generative AI services is present and you are good to go.
- Create an API key in Console: Generative AI -> API Keys
- Create a security policy: Identity & Security -> Policies
To authorize a specific API Key
allow any-user to use generative-ai-family in compartment <compartment-name> where ALL { request.principal.type='generativeaiapikey', request.principal.id='ocid1.generativeaiapikey.oc1.us-chicago-1....' }
To authorize any API Key
allow any-user to use generative-ai-family in compartment <compartment-name> where ALL { request.principal.type='generativeaiapikey' }
Use OCI IAM auth when you want to sign requests with your OCI profile (session/user/resource/instance principal) instead of API keys.
import httpx
from openai import OpenAI
from oci_genai_auth import OciSessionAuth
client = OpenAI(
base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1",
api_key="not-used",
http_client=httpx.Client(auth=OciSessionAuth(profile_name="DEFAULT")),
)Use OCI Generative AI API Keys if you want a direct API-key workflow with the OpenAI SDK.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1",
api_key=os.getenv("OPENAI_API_KEY"),
)AgentHub runs in non-pass-through mode and provides a unified interface for interacting with models and agentic capabilities. It is compatible with OpenAI's Responses API and the Open Responses Spec, enabling developers/users to: build agents with OpenAI SDK. Only the project OCID is required.
import httpx
from openai import OpenAI
from oci_genai_auth import OciSessionAuth
client = OpenAI(
base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1",
api_key="not-used",
project="ocid1.generativeaiproject.oc1.us-chicago-1.aaaaaaaaexample",
http_client=httpx.Client(auth=OciSessionAuth(profile_name="DEFAULT")),
)Partner endpoints run in pass-through mode and require the compartment OCID header.
import httpx
from openai import OpenAI
from oci_genai_auth import OciSessionAuth
client = OpenAI(
base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/v1",
api_key="not-used",
default_headers={
"opc-compartment-id": "ocid1.compartment.oc1..aaaaaaaaexample",
},
http_client=httpx.Client(auth=OciSessionAuth(profile_name="DEFAULT")),
)- Update
examples/agenthub/openai/common.pyand/orexamples/partner/openai/common.pywith yourCOMPARTMENT_ID,PROJECT_OCID, and set the correctREGION. - Set the
OPENAI_API_KEYenvironment variable when an example uses API key authentication. - Install optional dev dependencies:
pip install -e '.[dev]'.
Run an example either by calling its main() method or from the command line.