The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
import datetime
import httpx
import pytest
import json
import litellm
# Adds the parent directory to the system path
from litellm import ModelResponse
from litellm.constants import DEFAULT_OCI_CHAT_MAX_TOKENS
from litellm.llms.oci.chat.transformation import (
OCIChatConfig,
OCIRequestWrapper,
version,
)
TEST_MODEL_NAME = "xai.grok-4"
TEST_MODEL = f"oci/{TEST_MODEL_NAME}"
TEST_MESSAGES = [{"role": "user", "content": "Hello, how are you?"}]
TEST_COMPARTMENT_ID = "ocid1.compartment.oc1..xxxxxx"
BASE_OCI_PARAMS = {
"oci_region": "us-ashburn-1",
"oci_user": "ocid1.user.oc1..xxxxxxEXAMPLExxxxxx",
"oci_fingerprint": "4f:29:77:cc:b1:3e:55:ab:61:2a:de:47:f1:38:4c:90",
"oci_tenancy": "ocid1.tenancy.oc1..xxxxxxEXAMPLExxxxxx",
"oci_compartment_id": TEST_COMPARTMENT_ID,
}
TEST_OCI_PARAMS_KEY = {
**BASE_OCI_PARAMS,
"oci_key": "<private_key.pem as string>",
}
TEST_OCI_PARAMS_KEY_FILE = {
**BASE_OCI_PARAMS,
"oci_key_file": "<private_key.pem as a Path>",
}
@pytest.fixture(params=[TEST_OCI_PARAMS_KEY, TEST_OCI_PARAMS_KEY_FILE])
def supplied_params(request):
"""Fixture for passing in optional_parameters"""
return request.param
_AMBIENT_OCI_ENV: tuple[str, ...] = (
"OCI_REGION",
"OCI_USER",
"OCI_FINGERPRINT",
"OCI_TENANCY",
"OCI_KEY_FILE",
"OCI_KEY",
"OCI_COMPARTMENT_ID",
)
@pytest.fixture
def without_ambient_oci_env(monkeypatch):
"""Drop OCI credentials the environment may supply.
validate_environment falls back to os.environ for every credential and to a
default region only when OCI_REGION is unset, so a developer or runner with
OCI configured would see these tests find credentials they never passed.
"""
for variable in _AMBIENT_OCI_ENV:
monkeypatch.delenv(variable, raising=False)
@pytest.mark.usefixtures("without_ambient_oci_env")
class TestOCIChatConfig:
def test_validate_environment_with_oci_region(self, supplied_params):
config = OCIChatConfig()
headers = {}
result = config.validate_environment(
headers=headers,
model=TEST_MODEL,
messages=TEST_MESSAGES, # type: ignore
optional_params=supplied_params,
litellm_params={},
)
assert result["content-type"] == "application/json"
assert result["user-agent"] == f"litellm/{version}"
def test_missing_oci_auth_parameters(self, supplied_params):
params = supplied_params.copy() # safely copy, no reassignment
params.pop("oci_region")
for key in list(params.keys()):
modified_params = params.copy()
del modified_params[key]
config = OCIChatConfig()
headers = {}
with pytest.raises(Exception, match='Missing required parameters: oci_user, oci_fingerprint') as excinfo:
config.validate_environment(
headers=headers,
model=TEST_MODEL,
messages=TEST_MESSAGES, # type: ignore
optional_params=modified_params,
api_base="https://api.oci.example.com",
litellm_params={},
)
assert ("Missing required parameters:") in str(excinfo.value)
def test_transform_request_simple(self):
"""
Tests if a simple request is transformed correctly.
"""
config = OCIChatConfig()
optional_params = {"oci_compartment_id": TEST_COMPARTMENT_ID}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
expected_output = {
"compartmentId": TEST_COMPARTMENT_ID,
"servingMode": {"servingType": "ON_DEMAND", "modelId": TEST_MODEL_NAME},
"chatRequest": {
"apiFormat": "GENERIC",
"isStream": False,
"maxTokens": DEFAULT_OCI_CHAT_MAX_TOKENS,
"messages": [
{
"role": "USER",
"content": [{"type": "TEXT", "text": "Hello, how are you?"}],
}
],
},
}
assert transformed_request == expected_output
def test_transform_request_with_tools(self):
"""
Tests if a request with tools is transformed correctly.
"""
config = OCIChatConfig()
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
},
"required": ["location"],
},
},
}
]
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"tools": tools,
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
assert "tools" in transformed_request["chatRequest"]
assert (
transformed_request["chatRequest"]["tools"][0]["name"]
== "get_current_weather"
)
assert transformed_request["chatRequest"]["tools"][0]["type"] == "FUNCTION"
assert (
transformed_request["chatRequest"]["tools"][0]["description"]
== "Get the current weather in a given location"
)
assert transformed_request["chatRequest"]["tools"][0]["parameters"] is not None
def test_transform_request_dedicated_mode_with_endpoint_id(self):
"""
Tests if a request with DEDICATED serving mode and explicit oci_endpoint_id is transformed correctly.
"""
config = OCIChatConfig()
test_endpoint_id = "ocid1.generativeaiendpoint.oc1.us-chicago-1.xxxxxx"
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"oci_serving_mode": "DEDICATED",
"oci_endpoint_id": test_endpoint_id,
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
expected_serving_mode = {
"servingType": "DEDICATED",
"endpointId": test_endpoint_id,
}
assert transformed_request["servingMode"] == expected_serving_mode
assert transformed_request["compartmentId"] == TEST_COMPARTMENT_ID
def test_transform_request_dedicated_mode_without_endpoint_id(self):
"""
Tests if a request with DEDICATED serving mode falls back to model name when oci_endpoint_id is not provided.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"oci_serving_mode": "DEDICATED",
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
# Should use model name as endpoint ID when oci_endpoint_id is not provided
expected_serving_mode = {
"servingType": "DEDICATED",
"endpointId": TEST_MODEL_NAME,
}
assert transformed_request["servingMode"] == expected_serving_mode
assert transformed_request["compartmentId"] == TEST_COMPARTMENT_ID
def test_transform_request_on_demand_mode(self):
"""
Tests if a request with ON_DEMAND serving mode uses modelId correctly.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"oci_serving_mode": "ON_DEMAND",
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
expected_serving_mode = {
"servingType": "ON_DEMAND",
"modelId": TEST_MODEL_NAME,
}
assert transformed_request["servingMode"] == expected_serving_mode
assert transformed_request["compartmentId"] == TEST_COMPARTMENT_ID
def test_transform_request_invalid_serving_mode(self):
"""
Tests if an invalid serving mode raises an exception.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"oci_serving_mode": "INVALID_MODE",
}
with pytest.raises(Exception, match="kwarg `oci_serving_mode` must be either 'ON_DEMAND' or") as excinfo:
config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
assert "must be either 'ON_DEMAND' or 'DEDICATED'" in str(excinfo.value)
def test_transform_request_dedicated_cohere_with_endpoint_id(self):
"""
Tests that Cohere vendor detection works correctly with DEDICATED mode and oci_endpoint_id.
This is critical because the model parameter determines the API format even when using oci_endpoint_id.
"""
config = OCIChatConfig()
cohere_model = "cohere.command-latest"
test_endpoint_id = "ocid1.generativeaiendpoint.oc1.us-chicago-1.xxxxxx"
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"oci_serving_mode": "DEDICATED",
"oci_endpoint_id": test_endpoint_id,
}
messages = [
{"role": "user", "content": "What is quantum computing?"},
]
transformed_request = config.transform_request(
model=cohere_model,
messages=messages, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
# Verify DEDICATED mode with correct endpoint ID
assert transformed_request["servingMode"]["servingType"] == "DEDICATED"
assert transformed_request["servingMode"]["endpointId"] == test_endpoint_id
# Verify Cohere API format is used (not GENERIC)
assert transformed_request["chatRequest"]["apiFormat"] == "COHERE"
# Verify Cohere-specific request structure
assert "message" in transformed_request["chatRequest"] # Cohere uses "message"
assert (
"chatHistory" in transformed_request["chatRequest"]
) # Cohere uses "chatHistory"
assert (
"messages" not in transformed_request["chatRequest"]
) # Generic uses "messages"
# Verify the message content
assert (
transformed_request["chatRequest"]["message"]
== "What is quantum computing?"
)
def test_transform_request_response_format_json_object(self):
"""
Tests that response_format type 'json_object' is uppercased to 'JSON_OBJECT' for generic OCI models.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {"type": "json_object"},
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf["type"] == "JSON_OBJECT"
def test_transform_request_response_format_text(self):
"""
Tests that response_format type 'text' is uppercased to 'TEXT' for generic OCI models.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {"type": "text"},
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf["type"] == "TEXT"
def test_transform_request_response_format_json_shorthand(self):
"""
Tests that response_format type 'json' is mapped to 'JSON_OBJECT' for generic OCI models.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {"type": "json"},
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf["type"] == "JSON_OBJECT"
def test_transform_request_response_format_json_schema_generic(self):
"""A GENERIC json_schema must become OCI's JSON_SCHEMA shape with the
OpenAI ``strict`` key renamed to ``isStrict``.
OCI's ResponseJsonSchema rejects ``strict`` (and any other extra key)
with HTTP 400 "Please pass in correct format of request", so the raw
OpenAI body must not be forwarded.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "judgment",
"description": "a score and rationale",
"strict": True,
"schema": {
"type": "object",
"properties": {"score": {"type": "integer"}},
"required": ["score"],
},
},
},
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME, # xai.grok-4 -> GENERIC
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf["type"] == "JSON_SCHEMA"
assert "strict" not in rf["jsonSchema"]
assert rf["jsonSchema"]["isStrict"] is True
assert rf["jsonSchema"]["name"] == "judgment"
assert rf["jsonSchema"]["description"] == "a score and rationale"
assert rf["jsonSchema"]["schema"]["properties"]["score"]["type"] == "integer"
def test_transform_request_response_format_json_schema_generic_no_strict(self):
"""A GENERIC json_schema without ``strict`` must omit ``isStrict``."""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {
"type": "json_schema",
"json_schema": {"name": "j", "schema": {"type": "object"}},
},
}
transformed_request = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf["type"] == "JSON_SCHEMA"
assert "isStrict" not in rf["jsonSchema"]
def test_transform_request_response_format_json_schema_cohere(self):
"""A Cohere json_schema must fold the schema onto JSON_OBJECT.
OCI Cohere has no JSON_SCHEMA type; sending one yields HTTP 400.
"""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "judgment",
"strict": True,
"schema": {
"type": "object",
"properties": {"score": {"type": "integer"}},
},
},
},
}
transformed_request = config.transform_request(
model="cohere.command-latest",
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf["type"] == "JSON_OBJECT"
assert "jsonSchema" not in rf
assert rf["schema"]["properties"]["score"]["type"] == "integer"
def test_transform_request_response_format_cohere_json_object(self):
"""Cohere json_object without a schema stays a bare JSON_OBJECT."""
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {"type": "json_object"},
}
transformed_request = config.transform_request(
model="cohere.command-latest",
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
rf = transformed_request["chatRequest"]["responseFormat"]
assert rf == {"type": "JSON_OBJECT"}
def test_transform_request_json_schema_without_body_raises_generic(self):
"""A GENERIC json_schema with no ``json_schema`` body must raise an early
400, not silently emit {"type": "JSON_SCHEMA"} (which OCI rejects)."""
from litellm.llms.oci.common_utils import OCIError
config = OCIChatConfig()
optional_params = {
"oci_compartment_id": TEST_COMPARTMENT_ID,
"response_format": {"type": "json_schema"},
}
with pytest.raises(OCIError) as exc_info:
config.transform_request(
model=TEST_MODEL_NAME, # GENERIC
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
headers={},
)
assert exc_info.value.status_code == 400
assert "json_schema" in str(exc_info.value)
def test_transform_response_without_token_details(self):
"""
Tests that responses missing completionTokensDetails and promptTokensDetails
are handled correctly (fields are optional).
"""
config = OCIChatConfig()
created_time = (
datetime.datetime.now(datetime.timezone.utc)
.isoformat()
.replace("+00:00", "Z")
)
mock_oci_response = {
"modelId": TEST_MODEL_NAME,
"modelVersion": "1.0",
"chatResponse": {
"apiFormat": "GENERIC",
"choices": [
{
"index": 0,
"message": {
"role": "ASSISTANT",
"content": [{"type": "TEXT", "text": "Hello!"}],
},
"finishReason": "STOP",
}
],
"timeCreated": created_time,
"usage": {
"promptTokens": 5,
"completionTokens": 10,
"totalTokens": 15,
},
},
}
response = httpx.Response(
status_code=200,
json=mock_oci_response,
headers={"Content-Type": "application/json"},
)
result = config.transform_response(
model=TEST_MODEL_NAME,
raw_response=response,
model_response=ModelResponse(),
logging_obj={}, # type: ignore
request_data={},
messages=[],
optional_params={},
litellm_params={},
encoding={},
)
assert isinstance(result, ModelResponse)
assert result.choices[0].message.content == "Hello!"
assert result.usage.prompt_tokens == 5 # type: ignore
assert result.usage.completion_tokens == 10 # type: ignore
assert result.usage.total_tokens == 15 # type: ignore
def test_transform_response_simple_text(self):
"""
Tests if a simple text response is transformed correctly.
"""
config = OCIChatConfig()
created_time = (
datetime.datetime.now(datetime.timezone.utc)
.isoformat()
.replace("+00:00", "Z")
)
mock_oci_response = {
"modelId": TEST_MODEL_NAME,
"modelVersion": "1.0",
"chatResponse": {
"apiFormat": "GENERIC",
"choices": [
{
"index": 0,
"message": {
"role": "ASSISTANT",
"content": [
{"type": "TEXT", "text": "I am doing well, thank you!"}
],
},
"finishReason": "COMPLETE",
}
],
"timeCreated": created_time,
"usage": {
"promptTokens": 10,
"completionTokens": 20,
"totalTokens": 30,
"completionTokensDetails": {
"acceptedPredictionTokens": 20,
"reasoningTokens": 20,
},
"promptTokensDetails": {
"cachedTokens": 10,
},
},
},
}
response = httpx.Response(
status_code=200,
json=mock_oci_response,
headers={"Content-Type": "application/json"},
)
result = config.transform_response(
model=TEST_MODEL_NAME,
raw_response=response,
model_response=ModelResponse(),
logging_obj={}, # type: ignore
request_data={},
messages=[],
optional_params={},
litellm_params={},
encoding={},
)
assert isinstance(result, ModelResponse)
assert len(result.choices) == 1
assert isinstance(result.choices[0], litellm.Choices)
assert result.choices[0].message
assert result.choices[0].message.content == "I am doing well, thank you!"
assert result.choices[0].finish_reason == "stop"
assert result.model == TEST_MODEL_NAME
assert hasattr(result, "usage")
assert isinstance(result.usage, litellm.Usage) # type: ignore
assert result.usage.prompt_tokens == 10 # type: ignore
assert result.usage.completion_tokens == 20 # type: ignore
assert result.usage.total_tokens == 30 # type: ignore
# reasoningTokens from OCI's completionTokensDetails is surfaced on
# Usage.completion_tokens_details.reasoning_tokens.
assert result.usage.completion_tokens_details is not None
assert result.usage.completion_tokens_details.reasoning_tokens == 20
assert result.usage.prompt_tokens_details is None
def test_transform_response_with_tool_calls(self):
"""
Tests if a response with tool calls is transformed correctly.
"""
config = OCIChatConfig()
created_time = (
datetime.datetime.now(datetime.timezone.utc)
.isoformat()
.replace("+00:00", "Z")
)
mock_oci_response = {
"modelId": TEST_MODEL_NAME,
"modelVersion": "1.0",
"chatResponse": {
"apiFormat": "GENERIC",
"choices": [
{
"index": 0,
"message": {
"role": "ASSISTANT",
"content": None,
"toolCalls": [
{
"id": "call_abc123",
"type": "FUNCTION",
"name": "get_weather",
"arguments": '{"location": "Vila Velha, BR"}',
}
],
},
"finishReason": "stop",
}
],
"timeCreated": created_time,
"usage": {
"promptTokens": 10,
"completionTokens": 20,
"totalTokens": 30,
"completionTokensDetails": {
"acceptedPredictionTokens": 20,
"reasoningTokens": 20,
},
"promptTokensDetails": {
"cachedTokens": 10,
},
},
},
}
response = httpx.Response(status_code=200, json=mock_oci_response)
model_response = ModelResponse(
choices=[litellm.Choices(index=0, message=litellm.Message())]
)
result = config.transform_response(
model=TEST_MODEL_NAME,
raw_response=response,
model_response=model_response,
logging_obj={}, # type: ignore
request_data={},
messages=[],
optional_params={},
litellm_params={},
encoding={},
)
# General assertions
assert isinstance(result, ModelResponse)
assert len(result.choices) == 1
choice = result.choices[0]
assert isinstance(choice, litellm.Choices)
assert choice.finish_reason == "stop"
# Message and tool_calls assertions
message = choice.message
assert isinstance(message, litellm.Message)
assert hasattr(message, "tool_calls")
assert isinstance(message.tool_calls, list)
assert len(message.tool_calls) == 1
# Specific tool_call assertions
tool_call = message.tool_calls[0]
assert isinstance(tool_call, litellm.utils.ChatCompletionMessageToolCall)
assert tool_call.id == "call_abc123"
assert tool_call.type == "function"
assert tool_call.function["name"] == "get_weather"
assert tool_call.function["arguments"] == '{"location": "Vila Velha, BR"}'
# Usage assertions
assert hasattr(result, "usage")
usage = result.usage # type: ignore
assert isinstance(usage, litellm.Usage) # type: ignore
assert usage.prompt_tokens == 10 # type: ignore
assert usage.completion_tokens == 20 # type: ignore
assert usage.total_tokens == 30 # type: ignore
class TestOCISignerSupport:
"""Tests for OCI SDK Signer integration."""
def test_validate_environment_with_oci_signer(self):
"""Test validation when using oci_signer instead of manual credentials."""
config = OCIChatConfig()
headers = {}
# Mock signer object
class MockSigner:
def do_request_sign(self, request, enforce_content_headers=True):
request.headers["authorization"] = 'Signature version="1"'
optional_params = {"oci_signer": MockSigner(), "oci_region": "us-ashburn-1"}
result = config.validate_environment(
headers=headers,
model=TEST_MODEL,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
)
assert result["content-type"] == "application/json"
assert result["user-agent"] == f"litellm/{version}"
def test_validate_environment_with_oci_signer_no_compartment_id_in_validate(self):
"""Test that oci_compartment_id is not required in validate_environment when using signer."""
config = OCIChatConfig()
headers = {}
class MockSigner:
def do_request_sign(self, request, enforce_content_headers=True):
request.headers["authorization"] = 'Signature version="1"'
optional_params = {"oci_signer": MockSigner(), "oci_region": "us-phoenix-1"}
# Should not raise an exception even without oci_compartment_id
result = config.validate_environment(
headers=headers,
model=TEST_MODEL,
messages=TEST_MESSAGES, # type: ignore
optional_params=optional_params,
litellm_params={},
)
assert result["content-type"] == "application/json"
def test_sign_request_with_oci_signer(self):
"""Test request signing with oci_signer."""
config = OCIChatConfig()
class MockSigner:
def do_request_sign(self, request, enforce_content_headers=True):
request.headers["authorization"] = 'Signature version="1"'
request.headers["date"] = "Mon, 01 Jan 2024 00:00:00 GMT"
optional_params = {"oci_signer": MockSigner(), "method": "POST"}
headers, body = config.sign_request(
headers={},
optional_params=optional_params,
request_data={"test": "data"},
api_base="https://inference.generativeai.us-ashburn-1.oci.oraclecloud.com/20231130/actions/chat",
)
assert "authorization" in headers
assert "Signature" in headers["authorization"]
assert body is not None # oci_signer path returns body
assert json.loads(body.decode("utf-8")) == {"test": "data"}
def test_sign_request_with_oci_signer_updates_headers(self):
"""Test that signer properly updates request headers."""
config = OCIChatConfig()
class MockSigner:
def do_request_sign(self, request, enforce_content_headers=True):
# Verify the request has the expected attributes
assert hasattr(request, "method")
assert hasattr(request, "url")
assert hasattr(request, "headers")
assert hasattr(request, "body")
assert hasattr(request, "path_url")
# Add signature headers
request.headers["authorization"] = 'Signature keyId="test"'
request.headers["date"] = "Mon, 01 Jan 2024 00:00:00 GMT"
request.headers["x-content-sha256"] = "test-hash"
optional_params = {
"oci_signer": MockSigner(),
}
headers, body = config.sign_request(
headers={"custom-header": "custom-value"},
optional_params=optional_params,
request_data={"message": "Hello"},
api_base="https://inference.generativeai.us-ashburn-1.oci.oraclecloud.com/20231130/actions/chat",
)
# Check that all signer-added headers are present
assert headers["authorization"] == 'Signature keyId="test"'
assert headers["date"] == "Mon, 01 Jan 2024 00:00:00 GMT"
assert headers["x-content-sha256"] == "test-hash"
# Original headers should be preserved
assert headers["custom-header"] == "custom-value"
def test_sign_request_with_failing_oci_signer(self):
"""Test error handling when oci_signer fails."""
config = OCIChatConfig()
class FailingSigner:
def do_request_sign(self, request, enforce_content_headers=True):
raise RuntimeError("Signing failed due to invalid credentials")
optional_params = {
"oci_signer": FailingSigner(),
}
from litellm.llms.oci.common_utils import OCIError
with pytest.raises(OCIError) as excinfo:
config.sign_request(
headers={},
optional_params=optional_params,
request_data={"test": "data"},
api_base="https://inference.generativeai.us-ashburn-1.oci.oraclecloud.com/20231130/actions/chat",
)
assert "Failed to sign request with provided oci_signer" in str(excinfo.value)
assert excinfo.value.status_code == 500
def test_sign_request_with_invalid_http_method(self):
"""Test that invalid HTTP methods are rejected."""
config = OCIChatConfig()
class MockSigner:
def do_request_sign(self, request, enforce_content_headers=True):
pass
optional_params = {"oci_signer": MockSigner(), "method": "INVALID"}
with pytest.raises(ValueError, match='Unsupported HTTP method: INVALID') as excinfo:
config.sign_request(
headers={},
optional_params=optional_params,
request_data={"test": "data"},
api_base="https://inference.generativeai.us-ashburn-1.oci.oraclecloud.com/20231130/actions/chat",
)
assert "Unsupported HTTP method: INVALID" in str(excinfo.value)
def test_oci_request_wrapper_path_url(self):
"""Test OCIRequestWrapper path_url property."""
wrapper = OCIRequestWrapper(
method="POST",
url="https://example.com/api/v1/chat?param1=value1¶m2=value2",
headers={},
body=b"test",
)
assert wrapper.path_url == "/api/v1/chat?param1=value1¶m2=value2"
def test_oci_request_wrapper_path_url_no_query(self):
"""Test OCIRequestWrapper path_url property without query string."""
wrapper = OCIRequestWrapper(
method="POST",
url="https://example.com/api/v1/chat",
headers={},
body=b"test",
)
assert wrapper.path_url == "/api/v1/chat"
class TestOCIProviderEmbeddingConfig:
"""
Verifies that get_provider_embedding_config returns OCIEmbedConfig for OCI
and that the dead duplicate elif branch has been removed (Greptile P1).
"""
def test_returns_oci_embed_config(self):
from litellm.llms.oci.embed.transformation import OCIEmbedConfig
from litellm.utils import ProviderConfigManager
from litellm.types.utils import LlmProviders
config = ProviderConfigManager.get_provider_embedding_config(
model="cohere.embed-english-v3.0",
provider=LlmProviders.OCI,
)
assert isinstance(config, OCIEmbedConfig)
class TestOCICohereParamMapping:
"""
Unit tests for Bug 3 (stop → stopSequences) and Bug 4 (hardcoded defaults removed).
"""
def _make_config(self):
return OCIChatConfig()
def test_cohere_stop_maps_to_stop_sequences(self):
"""Bug 3: Cohere API uses 'stopSequences', not 'stop'."""
config = self._make_config()
result = config.map_openai_params(
non_default_params={"stop": ["END", "STOP"]},
optional_params={},
model="cohere.command-latest",
drop_params=False,
)
assert "stopSequences" in result, "stop should map to stopSequences for Cohere"
assert result["stopSequences"] == ["END", "STOP"]
assert "stop" not in result
def test_generic_stop_maps_to_stop(self):
"""GENERIC vendors (Meta, Google, xAI) keep 'stop' as-is."""
config = self._make_config()
result = config.map_openai_params(
non_default_params={"stop": ["END"]},
optional_params={},
model="meta.llama-3.3-70b-instruct",
drop_params=False,
)
assert result.get("stop") == ["END"]
assert "stopSequences" not in result
def test_cohere_no_hardcoded_defaults(self):
"""Bug 4: Cohere calls must not inject maxTokens/temperature/topK/topP/frequencyPenalty
when the user hasn't provided them."""
config = self._make_config()
result = config.map_openai_params(
non_default_params={},
optional_params={},
model="cohere.command-latest",
drop_params=False,
)
for injected in (
"maxTokens",
"temperature",
"topK",
"topP",
"frequencyPenalty",
):
assert (
injected not in result
), f"'{injected}' should not be injected when user did not provide it"
def test_cohere_explicit_params_still_passed(self):
"""User-provided Cohere params must still be forwarded correctly."""
config = self._make_config()
result = config.map_openai_params(
non_default_params={"max_tokens": 200, "temperature": 0.5},
optional_params={},
model="cohere.command-latest",
drop_params=False,
)
assert result.get("maxTokens") == 200
assert result.get("temperature") == 0.5
class TestOCIDefaultMaxTokens:
"""Regression for OCI's tiny server-side token cap (~20 tokens), which
silently truncated responses mid-string whenever the caller omitted
max_tokens (MLflow judges never send it, so their JSON came back cut off).
transform_request injects DEFAULT_OCI_CHAT_MAX_TOKENS when no limit is
supplied, and leaves an explicit limit untouched."""
def _chat_request(self, model: str, optional_params: dict) -> dict:
config = OCIChatConfig()
body = config.transform_request(
model=model,
messages=[{"role": "user", "content": "hi"}],
optional_params={**BASE_OCI_PARAMS, **optional_params},
litellm_params={},
headers={},
)
return body["chatRequest"]
@pytest.mark.parametrize(
"model", ["cohere.command-latest", "meta.llama-3.3-70b-instruct"]
)
def test_default_injected_when_max_tokens_omitted(self, model):
chat_request = self._chat_request(model, {})
assert chat_request["maxTokens"] == DEFAULT_OCI_CHAT_MAX_TOKENS
@pytest.mark.parametrize(
"model", ["cohere.command-latest", "meta.llama-3.3-70b-instruct"]
)
def test_explicit_max_tokens_not_overridden(self, model):
chat_request = self._chat_request(model, {"max_tokens": 256})
assert chat_request["maxTokens"] == 256
def test_reasoning_model_defaults_max_completion_tokens(self):
chat_request = self._chat_request("openai.gpt-5", {})
assert chat_request["maxCompletionTokens"] == DEFAULT_OCI_CHAT_MAX_TOKENS
assert "maxTokens" not in chat_request
class TestOCIReasoningEffort:
"""
Reasoning-effort handling for GENERIC reasoning models:
- OpenAI clients send lowercase ("low"/"medium"/"high"); OCI requires uppercase.
- OpenAI's "disable" maps to OCI's "NONE".
- Cohere on OCI has no reasoning models — the param is unsupported there.
"""
def _build_chat_request(self, model: str, optional_params: dict) -> dict:
"""Drive optional params through map → _get_optional_params and read
the resulting chatRequest body via transform_request."""
from litellm.llms.oci.chat.transformation import OCIChatConfig
config = OCIChatConfig()
mapped = config.map_openai_params(
non_default_params=optional_params,
optional_params={},
model=model,
drop_params=False,
)
body = config.transform_request(
model=model,
messages=[{"role": "user", "content": "hi"}],
optional_params={**BASE_OCI_PARAMS, **mapped},
litellm_params={},
headers={},
)
return body["chatRequest"]
def test_reasoning_effort_lowercase_uppercased(self):
# gpt-5 accepts reasoningEffort; OCI's Grok 4.20 models reject it
# (see TestReasoningEffortGating at the end of this module).
chat_request = self._build_chat_request(
"openai.gpt-5",
{"reasoning_effort": "low"},
)
assert chat_request.get("reasoningEffort") == "LOW"
def test_reasoning_effort_disable_mapped_to_none(self):
# gpt-5 accepts reasoningEffort; OCI's Grok 4.20 models reject it
# (see TestReasoningEffortGating at the end of this module).
chat_request = self._build_chat_request(
"openai.gpt-5",
{"reasoning_effort": "disable"},
)
assert chat_request.get("reasoningEffort") == "NONE"
def test_reasoning_effort_already_uppercase_preserved(self):
chat_request = self._build_chat_request(
"openai.gpt-5",
{"reasoning_effort": "HIGH"},
)
assert chat_request.get("reasoningEffort") == "HIGH"
def test_reasoning_effort_unsupported_on_cohere_dropped(self):
"""drop_params=True → silently drop reasoning_effort for Cohere."""
from litellm.llms.oci.chat.transformation import OCIChatConfig
config = OCIChatConfig()
result = config.map_openai_params(
non_default_params={"reasoning_effort": "low"},
optional_params={},
model="cohere.command-latest",
drop_params=True,
)
assert "reasoning_effort" not in result
assert "reasoningEffort" not in result
def test_reasoning_effort_unsupported_on_cohere_raises(self):
"""drop_params=False → raise rather than ship a payload Cohere will reject."""
from litellm.llms.oci.chat.transformation import OCIChatConfig
from litellm.llms.oci.common_utils import OCIError
config = OCIChatConfig()
with pytest.raises(OCIError):
config.map_openai_params(
non_default_params={"reasoning_effort": "low"},
optional_params={},
model="cohere.command-latest",
drop_params=False,
)
def test_reasoning_tokens_extracted_from_usage(self):
"""OCI's completionTokensDetails.reasoningTokens flows into
Usage.completion_tokens_details.reasoning_tokens."""
from litellm.llms.oci.chat.generic import handle_generic_response
created_time = (
datetime.datetime.now(datetime.timezone.utc)
.isoformat()
.replace("+00:00", "Z")
)
oci_response = {
"modelId": "xai.grok-4-fast-reasoning",
"modelVersion": "1.0",
"chatResponse": {
"apiFormat": "GENERIC",
"choices": [
{
"index": 0,
"message": {
"role": "ASSISTANT",
"content": [{"type": "TEXT", "text": "ok"}],
},
"finishReason": "STOP",
}
],
"timeCreated": created_time,
"usage": {
"promptTokens": 5,
"completionTokens": 12,
"totalTokens": 17,
"completionTokensDetails": {"reasoningTokens": 7},
},
},
}
raw = httpx.Response(status_code=200, json=oci_response)
result = handle_generic_response(
json_data=oci_response,
model="xai.grok-4-fast-reasoning",
model_response=ModelResponse(),
raw_response=raw,
)
usage = result.usage # type: ignore[attr-defined]
assert usage.completion_tokens_details is not None
assert usage.completion_tokens_details.reasoning_tokens == 7
def test_reasoning_tokens_absent_when_no_details(self):
"""When OCI omits completionTokensDetails, Usage has no reasoning_tokens."""
from litellm.llms.oci.chat.generic import handle_generic_response
created_time = (
datetime.datetime.now(datetime.timezone.utc)
.isoformat()
.replace("+00:00", "Z")
)
oci_response = {
"modelId": "xai.grok-4",
"modelVersion": "1.0",
"chatResponse": {
"apiFormat": "GENERIC",
"choices": [
{
"index": 0,
"message": {
"role": "ASSISTANT",
"content": [{"type": "TEXT", "text": "ok"}],
},
"finishReason": "STOP",
}
],
"timeCreated": created_time,
"usage": {
"promptTokens": 5,
"completionTokens": 12,
"totalTokens": 17,
},
},
}
raw = httpx.Response(status_code=200, json=oci_response)
result = handle_generic_response(
json_data=oci_response,
model="xai.grok-4",
model_response=ModelResponse(),
raw_response=raw,
)
usage = result.usage # type: ignore[attr-defined]
assert usage.completion_tokens_details is None
class TestOCIStreamingSignedBody:
"""
Unit test for Bug 1: sync and async streaming paths must use signed_json_body
when provided, not re-serialize data with json.dumps().
"""
def test_get_custom_stream_wrapper_uses_signed_body(self, monkeypatch):
"""
When signed_json_body is provided, the POST must use that exact bytes object,
not json.dumps(data) — otherwise the RSA-SHA256 signature is invalid.
"""
from unittest.mock import MagicMock
config = OCIChatConfig()
signed_bytes = b'{"signed": true}'
posted_data = {}
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.iter_text.return_value = iter([])
mock_client = MagicMock()
mock_client.post.return_value = mock_response
def capture_post(url, **kwargs):
posted_data["data"] = kwargs.get("data")
return mock_response
mock_client.post.side_effect = capture_post
mock_logging = MagicMock()
config.get_sync_custom_stream_wrapper(
litellm_params={},
api_base="https://example.com",
headers={},
data={"key": "value"},
messages=[],
model="meta.llama-3.3-70b-instruct",
custom_llm_provider="oci",
logging_obj=mock_logging,
client=mock_client,
signed_json_body=signed_bytes,
)
assert (
posted_data["data"] == signed_bytes
), "Streaming must use signed_json_body, not re-serialize data"
def test_get_custom_stream_wrapper_fallback_without_signed_body(self, monkeypatch):
"""When signed_json_body is None, fall back to json.dumps(data)."""
import json
from unittest.mock import MagicMock
config = OCIChatConfig()
posted_data = {}
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.iter_text.return_value = iter([])
mock_client = MagicMock()
def capture_post(url, **kwargs):
posted_data["data"] = kwargs.get("data")
return mock_response
mock_client.post.side_effect = capture_post
mock_logging = MagicMock()
payload = {"key": "value"}
config.get_sync_custom_stream_wrapper(
litellm_params={},
api_base="https://example.com",
headers={},
data=payload,
messages=[],
model="meta.llama-3.3-70b-instruct",
custom_llm_provider="oci",
logging_obj=mock_logging,
client=mock_client,
signed_json_body=None,
)
assert posted_data["data"] == json.dumps(
payload
), "Without signed_json_body, must fall back to json.dumps(data)"
# ---------------------------------------------------------------------------
# Additional coverage: error paths in validate_environment, transform_request,
# transform_response, and map_openai_params
# ---------------------------------------------------------------------------
class TestOCIChatConfigErrorPaths:
def test_validate_environment_empty_messages_raises(self):
config = OCIChatConfig()
with pytest.raises(Exception, match="messages"):
config.validate_environment(
headers={},
model=TEST_MODEL_NAME,
messages=[],
optional_params={
"oci_signer": MagicMock(),
"oci_compartment_id": TEST_COMPARTMENT_ID,
},
litellm_params={},
)
def test_transform_request_missing_compartment_id_raises(self):
config = OCIChatConfig()
with pytest.raises(Exception, match="oci_compartment_id"):
config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params={},
litellm_params={},
headers={},
)
def test_transform_request_cohere_no_user_message_raises(self):
config = OCIChatConfig()
with pytest.raises(Exception, match="user message"):
config.transform_request(
model="cohere.command-latest",
messages=[{"role": "system", "content": "You are helpful."}], # type: ignore
optional_params={"oci_compartment_id": TEST_COMPARTMENT_ID},
litellm_params={},
headers={},
)
def test_transform_response_error_key_raises(self):
config = OCIChatConfig()
response = httpx.Response(
status_code=400,
json={"error": "model not found"},
)
with pytest.raises(Exception, match="model not found"):
config.transform_response(
model=TEST_MODEL_NAME,
raw_response=response,
model_response=ModelResponse(),
logging_obj={}, # type: ignore
request_data={},
messages=[],
optional_params={},
litellm_params={},
encoding={},
)
def test_map_openai_params_unsupported_param_raises_without_drop(self):
config = OCIChatConfig()
with pytest.raises(Exception, match="not supported on OCI"):
config.map_openai_params(
non_default_params={"audio": {"voice": "alloy"}},
optional_params={},
model=TEST_MODEL_NAME,
drop_params=False,
)
def test_map_openai_params_unsupported_param_dropped(self):
config = OCIChatConfig()
result = config.map_openai_params(
non_default_params={"audio": {"voice": "alloy"}},
optional_params={},
model=TEST_MODEL_NAME,
drop_params=True,
)
assert "audio" not in result
@pytest.mark.parametrize("model", ["cohere.command-latest", "xai.grok-4"])
def test_map_openai_params_max_retries_dropped_without_drop_params(self, model):
"""max_retries is a litellm control param, not a generation param. It
must be dropped silently (no raise) even when drop_params is False, so
the litellm proxy (which injects max_retries on every request) does not
500 every OCI call.
"""
config = OCIChatConfig()
result = config.map_openai_params(
non_default_params={"max_retries": 3},
optional_params={},
model=model,
drop_params=False,
)
assert "max_retries" not in result
def test_map_openai_params_cohere_n_default_dropped(self):
"""Cohere has no numGenerations field, but n=1 (and None) is the OpenAI
default single-generation request. It must be dropped silently rather
than raising, so standard clients that always send n=1 (e.g. the MLflow
gateway) are not rejected."""
config = OCIChatConfig()
for n in (1, None):
result = config.map_openai_params(
non_default_params={"n": n},
optional_params={},
model="cohere.command-latest",
drop_params=False,
)
assert "n" not in result and "numGenerations" not in result
def test_map_openai_params_cohere_n_gt_1_raises_without_drop(self):
"""n>1 is genuinely unsupported on Cohere and must raise without drop."""
config = OCIChatConfig()
with pytest.raises(Exception, match="not supported on OCI"):
config.map_openai_params(
non_default_params={"n": 3},
optional_params={},
model="cohere.command-latest",
drop_params=False,
)
def test_map_openai_params_cohere_n_gt_1_dropped_with_drop(self):
config = OCIChatConfig()
result = config.map_openai_params(
non_default_params={"n": 3},
optional_params={},
model="cohere.command-latest",
drop_params=True,
)
assert "n" not in result and "numGenerations" not in result
def test_map_openai_params_generic_n_maps_to_num_generations(self):
"""Generic models keep numGenerations, including n>1."""
config = OCIChatConfig()
result = config.map_openai_params(
non_default_params={"n": 2},
optional_params={},
model=TEST_MODEL_NAME,
drop_params=False,
)
assert result["numGenerations"] == 2
def test_transform_request_tool_choice_string_mapped(self):
config = OCIChatConfig()
result = config.transform_request(
model=TEST_MODEL_NAME,
messages=TEST_MESSAGES, # type: ignore
optional_params={
"oci_compartment_id": TEST_COMPARTMENT_ID,
"tool_choice": "auto",
"tools": [
{
"type": "function",
"function": {
"name": "fn",
"description": "d",
"parameters": {"type": "object", "properties": {}},
},
}
],
},
litellm_params={},
headers={},
)
assert result["chatRequest"]["toolChoice"] == {"type": "AUTO"}
import pytest
from unittest.mock import MagicMock
from litellm.llms.oci.common_utils import OCIError, sign_with_manual_credentials
@pytest.fixture
def config():
return OCIChatConfig()
@pytest.mark.usefixtures("without_ambient_oci_env")
class TestOCIKeyNormalization:
"""Tests for OCI private key content normalization."""
def test_oci_key_rejects_non_string_type(self, config):
"""Test that non-string oci_key values raise OCIError."""
optional_params = {
"oci_user": "ocid1.user.oc1..test",
"oci_fingerprint": "aa:bb:cc:dd",
"oci_tenancy": "ocid1.tenancy.oc1..test",
"oci_region": "us-ashburn-1",
"oci_key": {"invalid": "dict"}, # Wrong type
}
with pytest.raises(OCIError) as exc_info:
sign_with_manual_credentials(
headers={},
optional_params=optional_params,
request_data={"test": "data"},
api_base="https://test.oci.oraclecloud.com/api",
)
assert exc_info.value.status_code == 400
assert "must be a string" in str(exc_info.value.message)
assert "dict" in str(exc_info.value.message)
def test_oci_key_rejects_list_type(self, config):
"""Test that list oci_key values raise OCIError."""
optional_params = {
"oci_user": "ocid1.user.oc1..test",
"oci_fingerprint": "aa:bb:cc:dd",
"oci_tenancy": "ocid1.tenancy.oc1..test",
"oci_region": "us-ashburn-1",
"oci_key": ["invalid", "list"], # Wrong type
}
with pytest.raises(OCIError) as exc_info:
sign_with_manual_credentials(
headers={},
optional_params=optional_params,
request_data={"test": "data"},
api_base="https://test.oci.oraclecloud.com/api",
)
assert exc_info.value.status_code == 400
assert "must be a string" in str(exc_info.value.message)
assert "list" in str(exc_info.value.message)
@pytest.mark.usefixtures("without_ambient_oci_env")
class TestOCIValidateEnvironment:
"""Tests for OCI environment validation."""
def test_missing_required_credentials_raises_error(self, config):
"""Test that missing required credentials raise an error."""
with pytest.raises(Exception, match='Missing required parameters: oci_user, oci_fingerprint') as exc_info:
config.validate_environment(
headers={},
model="oci/xai.grok-3",
messages=[{"role": "user", "content": "Hello"}],
optional_params={}, # No credentials provided
litellm_params={},
api_key=None,
api_base=None,
)
error_message = str(exc_info.value)
assert "oci_user" in error_message
assert "oci_fingerprint" in error_message
assert "oci_tenancy" in error_message
def test_validate_environment_with_all_credentials(self, config):
"""Test that validation passes with all required credentials."""
headers = config.validate_environment(
headers={},
model="oci/xai.grok-3",
messages=[{"role": "user", "content": "Hello"}],
optional_params={
"oci_user": "ocid1.user.oc1..test",
"oci_fingerprint": "aa:bb:cc:dd",
"oci_tenancy": "ocid1.tenancy.oc1..test",
"oci_region": "us-ashburn-1",
"oci_compartment_id": "ocid1.compartment.oc1..test",
"oci_key": "-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----",
},
litellm_params={},
api_key=None,
api_base=None,
)
assert headers["content-type"] == "application/json"
assert "user-agent" in headers
@pytest.mark.usefixtures("without_ambient_oci_env")
class TestOCIGetCompleteUrl:
"""Tests for OCI URL generation."""
def test_get_complete_url_default_region(self, config):
"""Test URL generation with default region."""
url = config.get_complete_url(
api_base=None,
api_key=None,
model="oci/xai.grok-3",
optional_params={},
litellm_params={},
stream=False,
)
assert "us-ashburn-1" in url
assert "inference.generativeai" in url
assert "/20231130/actions/chat" in url
def test_get_complete_url_custom_region(self, config):
"""Test URL generation with custom region."""
url = config.get_complete_url(
api_base=None,
api_key=None,
model="oci/xai.grok-3",
optional_params={"oci_region": "eu-frankfurt-1"},
litellm_params={},
stream=False,
)
assert "eu-frankfurt-1" in url
assert "inference.generativeai" in url
@pytest.mark.usefixtures("without_ambient_oci_env")
class TestOCIImageUrlTransformation:
"""Tests for OCI image_url format handling in multimodal messages.
Fixes: https://github.com/BerriAI/litellm/issues/18270
Fixes: https://github.com/BerriAI/litellm/issues/19589
"""
def test_image_url_as_string(self):
"""Test that image_url as a plain string works."""
from litellm.llms.oci.chat.transformation import (
adapt_messages_to_generic_oci_standard,
)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": "https://example.com/image.png"},
],
}
]
result = adapt_messages_to_generic_oci_standard(messages)
assert len(result) == 1
assert result[0].role == "USER"
assert len(result[0].content) == 2
# imageUrl is now an OCIImageUrl object with a 'url' property
assert result[0].content[1].imageUrl.url == "https://example.com/image.png"
def test_image_url_as_openai_object(self):
"""Test that image_url as OpenAI-style object {"url": "..."} works."""
from litellm.llms.oci.chat.transformation import (
adapt_messages_to_generic_oci_standard,
)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.png"},
},
],
}
]
result = adapt_messages_to_generic_oci_standard(messages)
assert len(result) == 1
assert result[0].role == "USER"
assert len(result[0].content) == 2
# imageUrl is now an OCIImageUrl object with a 'url' property
assert result[0].content[1].imageUrl.url == "https://example.com/image.png"
def test_image_url_serializes_as_object(self):
"""Test that imageUrl serializes as {"url": "..."} for OCI API.
Fixes: https://github.com/BerriAI/litellm/issues/19589
OCI expects imageUrl to be an object with a 'url' property, not a plain string.
"""
from litellm.llms.oci.chat.transformation import (
adapt_messages_to_generic_oci_standard,
)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image."},
{
"type": "image_url",
"image_url": {"url": "data:image/png;base64,ABC123"},
},
],
}
]
result = adapt_messages_to_generic_oci_standard(messages)
image_part = result[0].content[1]
# Serialize as OCI would receive it (with exclude_none=True)
serialized = image_part.model_dump(exclude_none=True)
# Verify the structure matches OCI's expected format
assert serialized == {
"type": "IMAGE",
"imageUrl": {"url": "data:image/png;base64,ABC123"},
}
def test_image_url_invalid_type_raises_error(self):
"""Test that invalid image_url type raises an error."""
from litellm.llms.oci.chat.transformation import (
adapt_messages_to_generic_oci_standard,
)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": 12345}, # Invalid type
],
}
]
with pytest.raises(Exception, match='Prop `image_url` must be a string or an object with a `url`') as exc_info:
adapt_messages_to_generic_oci_standard(messages)
assert "image_url" in str(exc_info.value)
def test_image_url_object_missing_url_raises_error(self):
"""Test that object without 'url' property raises an error."""
from litellm.llms.oci.chat.transformation import (
adapt_messages_to_generic_oci_standard,
)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{
"type": "image_url",
"image_url": {"detail": "high"},
}, # Missing 'url'
],
}
]
with pytest.raises(Exception, match='Prop `image_url` must be a string or an object with a `url`') as exc_info:
adapt_messages_to_generic_oci_standard(messages)
assert "image_url" in str(exc_info.value)
import itertools
from unittest.mock import patch
from litellm.llms.oci.chat.transformation import OCIStreamWrapper, _iter_sse_events
_STREAM_GENERIC_MODEL = "xai.grok-4"
_STREAM_COHERE_MODEL = "cohere.command-latest"
_GENERIC_TEXT_EVENT = (
'data: {{"index":0,"message":{{"role":"ASSISTANT","content":[{{"type":"TEXT","text":"{text}"}}]}},"pad":"aaa"}}'
)
_GENERIC_TERMINAL_EVENT = (
'data: {"message":{"role":"ASSISTANT","content":[{"type":"TEXT","text":""}]},"finishReason":"stop","pad":"a"}'
)
_COHERE_TEXT_EVENT = 'data: {{"apiFormat":"COHERE","text":"{text}","pad":"aaaaaa"}}'
_COHERE_TERMINAL_EVENT = (
'data: {"apiFormat":"COHERE","text":"123","finishReason":"COMPLETE",'
'"chatHistory":[{"role":"USER","message":"count"},{"role":"CHATBOT","message":"123"}]}'
)
def _make_stream_wrapper(model: str) -> OCIStreamWrapper:
logging_obj = MagicMock()
logging_obj.model_call_details = {"custom_llm_provider": "oci", "litellm_params": {}}
return OCIStreamWrapper(
completion_stream=iter([]),
model=model,
custom_llm_provider="oci",
logging_obj=logging_obj,
)
def _ticking_clock():
"""A ``time.time`` stand-in that advances a full second on every call.
Without it the whole test runs inside one wall-clock second, so a per-chunk
``created`` would coincidentally match and the drift would go unnoticed.
"""
return itertools.count(1_700_000_000.0)
class TestOCIStreamWrapperIdentityPinning:
"""One OCI streaming completion must present one id, one created and the
wrapper's model on every chunk, the way every other provider does."""
def test_generic_stream_shares_one_id_created_and_model(self):
wrapper = _make_stream_wrapper(_STREAM_GENERIC_MODEL)
events = [
_GENERIC_TEXT_EVENT.format(text="1"),
_GENERIC_TEXT_EVENT.format(text="2"),
_GENERIC_TEXT_EVENT.format(text="3"),
_GENERIC_TERMINAL_EVENT,
]
with patch("time.time", side_effect=_ticking_clock()):
chunks = [wrapper.chunk_creator(event) for event in events]
assert len(chunks) == 4
assert len({chunk.id for chunk in chunks}) == 1
assert chunks[0].id.startswith("chatcmpl-")
assert len({chunk.created for chunk in chunks}) == 1
assert {chunk.model for chunk in chunks} == {_STREAM_GENERIC_MODEL}
assert [chunk.choices[0].delta.content for chunk in chunks[:3]] == ["1", "2", "3"]
assert chunks[-1].choices[0].finish_reason == "stop"
assert all(chunk._hidden_params["custom_llm_provider"] == "oci" for chunk in chunks)
def test_cohere_stream_shares_one_id_created_and_model(self):
"""Rebuilding each chunk through the shared creator must not disturb the
Cohere bookkeeping that suppresses the terminal event's repeated text."""
wrapper = _make_stream_wrapper(_STREAM_COHERE_MODEL)
events = [
_COHERE_TEXT_EVENT.format(text="1"),
_COHERE_TEXT_EVENT.format(text="2"),
_COHERE_TEXT_EVENT.format(text="3"),
_COHERE_TERMINAL_EVENT,
]
with patch("time.time", side_effect=_ticking_clock()):
chunks = [wrapper.chunk_creator(event) for event in events]
assert len(chunks) == 4
assert len({chunk.id for chunk in chunks}) == 1
assert len({chunk.created for chunk in chunks}) == 1
assert {chunk.model for chunk in chunks} == {_STREAM_COHERE_MODEL}
assert [chunk.choices[0].delta.content for chunk in chunks[:3]] == ["1", "2", "3"]
assert chunks[-1].choices[0].finish_reason == "stop"
assert chunks[-1].choices[0].delta.content is None
assert wrapper._cohere_text_emitted is True
def test_id_is_pinned_to_the_wrapper_response_id(self):
wrapper = _make_stream_wrapper(_STREAM_GENERIC_MODEL)
first = wrapper.chunk_creator(_GENERIC_TEXT_EVENT.format(text="1"))
assert wrapper.response_id == first.id
assert wrapper.created == first.created
class TestOCIStreamWrapperDoneSentinel:
"""OCI's GENERIC apiFormat closes the stream with a literal `[DONE]` line;
parsing it as JSON turned every streaming completion into a 500."""
@pytest.mark.parametrize("done_event", ["data: [DONE]", "data:[DONE]", "data: [DONE] "])
def test_done_sentinel_returns_none(self, done_event):
wrapper = _make_stream_wrapper(_STREAM_GENERIC_MODEL)
assert wrapper.chunk_creator(done_event) is None
def test_done_sentinel_off_the_sse_splitter_is_skipped(self):
wrapper = _make_stream_wrapper(_STREAM_GENERIC_MODEL)
wire = (
f"{_GENERIC_TEXT_EVENT.format(text='1')}\n\n"
f"{_GENERIC_TEXT_EVENT.format(text='2')}\n\n"
f"{_GENERIC_TERMINAL_EVENT}\n\n"
"data: [DONE]\n\n"
)
events = list(_iter_sse_events(iter([wire])))
assert events[-1] == "data: [DONE]"
chunks = [wrapper.chunk_creator(event) for event in events]
assert chunks[-1] is None
emitted = [chunk for chunk in chunks if chunk is not None]
assert len(emitted) == 3
assert len({chunk.id for chunk in emitted}) == 1
def test_unparseable_payload_still_raises_oci_error(self):
from litellm.llms.oci.common_utils import OCIError
wrapper = _make_stream_wrapper(_STREAM_GENERIC_MODEL)
with pytest.raises(OCIError, match="Chunk cannot be parsed as JSON"):
wrapper.chunk_creator("data: not-json-at-all")
def test_done_lookalike_payload_still_raises_oci_error(self):
from litellm.llms.oci.common_utils import OCIError
wrapper = _make_stream_wrapper(_STREAM_GENERIC_MODEL)
with pytest.raises(OCIError, match="Chunk cannot be parsed as JSON"):
wrapper.chunk_creator("data: [DONE] trailing garbage")
_GENERIC_TOOL_CALL_EVENT = (
'data: {"index":0,"message":{"role":"ASSISTANT","content":[],'
'"toolCalls":[{"type":"FUNCTION","id":"call_1","name":"get_weather","arguments":"{}"}]}}'
)
_GENERIC_TOOL_TERMINAL_EVENT = (
'data: {"index":0,"message":{"role":"ASSISTANT","content":[]},"finishReason":"TOOL_CALLS"}'
)
def _drain_stream(model: str, events: list[str]) -> list:
logging_obj = MagicMock()
logging_obj.model_call_details = {"custom_llm_provider": "oci", "litellm_params": {}}
wrapper = OCIStreamWrapper(
completion_stream=iter(events),
model=model,
custom_llm_provider="oci",
logging_obj=logging_obj,
)
return list(wrapper)
class TestOCIStreamWrapperTerminalChunk:
"""OCI's ``chunk_creator`` override bypasses the shared handler's
finish-reason bookkeeping, so the shared end-of-stream finalizer used to
append a synthetic ``stop`` chunk after OCI's own terminal chunk, silently
downgrading a ``tool_calls`` completion for any client that reads the
finish reason off the last chunk."""
def test_generic_tool_call_stream_ends_on_tool_calls(self):
chunks = _drain_stream(
_STREAM_GENERIC_MODEL,
[_GENERIC_TOOL_CALL_EVENT, _GENERIC_TOOL_TERMINAL_EVENT, "data: [DONE]"],
)
assert [chunk.choices[0].finish_reason for chunk in chunks] == [None, "tool_calls"]
assert len({chunk.id for chunk in chunks}) == 1
def test_generic_text_stream_emits_exactly_one_finish_reason(self):
chunks = _drain_stream(
_STREAM_GENERIC_MODEL,
[_GENERIC_TEXT_EVENT.format(text="1"), _GENERIC_TERMINAL_EVENT, "data: [DONE]"],
)
assert [chunk.choices[0].finish_reason for chunk in chunks] == [None, "stop"]
def test_cohere_stream_emits_exactly_one_finish_reason(self):
chunks = _drain_stream(
_STREAM_COHERE_MODEL,
[_COHERE_TEXT_EVENT.format(text="123"), _COHERE_TERMINAL_EVENT],
)
assert [chunk.choices[0].finish_reason for chunk in chunks] == [None, "stop"]
# ---------------------------------------------------------------------------
# BerriAI/litellm#31449: mixed tool lists and reasoning_effort gating
#
# 1. Non-function tools answer 400 without ``drop_params`` and are dropped
# with it, unless the request depends on them: when no function tool is
# left, or ``tool_choice`` forces a dropped tool, it still answers 400.
# 2. ``reasoning_effort`` is withheld from OCI models whose catalog entry
# declares no reasoning effort levels (xAI Grok 4.20), so ``drop_params``
# works and callers get a clear error otherwise.
# ---------------------------------------------------------------------------
from litellm.llms.oci.chat.transformation import get_vendor_from_model
FUNCTION_TOOL = {
"type": "function",
"function": {
"name": "get_weather",
"description": "Weather for a city",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
},
}
BUILTIN_TOOL = {"type": "web_search_preview"}
SERVER_TOOL = {"type": "computer_use_preview", "display_width": 1024}
FORCE_BUILTIN = {"type": "function", "function": {"name": "web_search_preview"}}
FORCE_FUNCTION = {"type": "function", "function": {"name": "get_weather"}}
GPT_OSS_MODELS = ["openai.gpt-oss-120b", "openai.gpt-oss-20b"]
def _oci_tool_params(model, tools, tool_choice, drop_params):
request = {"tools": tools} if tool_choice is None else {"tools": tools, "tool_choice": tool_choice}
config = OCIChatConfig()
mapped = config.map_openai_params(
non_default_params=request,
optional_params={},
model=model,
drop_params=drop_params,
)
return config._get_optional_params(get_vendor_from_model(model), mapped, model=model)
class TestNonFunctionTools:
@pytest.mark.parametrize("model", ["xai.grok-4.20", "cohere.command-latest"])
@pytest.mark.parametrize("non_function_tool", [BUILTIN_TOOL, SERVER_TOOL, {"name": "untyped"}])
def test_rejected_without_drop_params(self, monkeypatch, model, non_function_tool):
monkeypatch.setattr(litellm, "drop_params", False)
with pytest.raises(OCIError, match="drop_params") as exc_info:
_oci_tool_params(model, [non_function_tool, FUNCTION_TOOL], None, drop_params=False)
assert exc_info.value.status_code == 400
def test_global_drop_params_drops_them(self, monkeypatch):
monkeypatch.setattr(litellm, "drop_params", True)
params = _oci_tool_params("xai.grok-4.20", [BUILTIN_TOOL, FUNCTION_TOOL], "auto", drop_params=False)
assert [tool.name for tool in params["tools"]] == ["get_weather"]
@pytest.mark.parametrize("model", ["xai.grok-4.20", "cohere.command-latest"])
def test_drop_params_keeps_function_tools_only(self, model):
params = _oci_tool_params(model, [BUILTIN_TOOL, FUNCTION_TOOL, SERVER_TOOL], None, drop_params=True)
assert [tool.name for tool in params["tools"]] == ["get_weather"]
def test_drop_params_keeps_unforced_tool_choice(self):
params = _oci_tool_params("xai.grok-4.20", [BUILTIN_TOOL, FUNCTION_TOOL], "required", drop_params=True)
assert params["toolChoice"] == {"type": "REQUIRED"}
@pytest.mark.parametrize("tool_choice", [None, "auto", "required"])
def test_drop_params_rejects_a_request_with_no_function_tool_left(self, tool_choice):
with pytest.raises(OCIError, match="every tool") as exc_info:
_oci_tool_params("xai.grok-4.20", [BUILTIN_TOOL, SERVER_TOOL], tool_choice, drop_params=True)
assert exc_info.value.status_code == 400
@pytest.mark.parametrize(
"tool_choice",
[FORCE_BUILTIN, "web_search_preview", {"type": "web_search_preview"}],
)
def test_forcing_a_dropped_tool_is_rejected(self, tool_choice):
with pytest.raises(OCIError, match="tool_choice forces `web_search_preview`") as exc_info:
_oci_tool_params("xai.grok-4.20", [BUILTIN_TOOL, FUNCTION_TOOL], tool_choice, drop_params=True)
assert exc_info.value.status_code == 400
@pytest.mark.parametrize(
("dropped_tool", "tool_choice"),
[
({"type": "bash_20250124", "name": "bash"}, {"type": "function", "function": {"name": "bash"}}),
({"type": "custom", "custom": {"name": "code_exec"}}, "code_exec"),
({"type": "custom", "custom": {"name": "code_exec"}}, {"type": "function", "function": {"name": "code_exec"}}),
],
)
def test_forcing_a_dropped_tool_by_name_is_rejected(self, dropped_tool, tool_choice):
with pytest.raises(OCIError, match="tool_choice forces") as exc_info:
_oci_tool_params("xai.grok-4.20", [dropped_tool, FUNCTION_TOOL], tool_choice, drop_params=True)
assert exc_info.value.status_code == 400
@pytest.mark.parametrize("tool_choice", [FORCE_FUNCTION, "get_weather"])
def test_forcing_a_kept_tool_is_preserved(self, tool_choice):
params = _oci_tool_params("xai.grok-4.20", [BUILTIN_TOOL, FUNCTION_TOOL], tool_choice, drop_params=True)
assert params["toolChoice"] == {"type": "FUNCTION", "name": "get_weather"}
def test_forcing_a_kept_function_sharing_a_dropped_tool_name_is_preserved(self):
web_search_function = {**FUNCTION_TOOL, "function": {**FUNCTION_TOOL["function"], "name": "web_search"}}
force_web_search = {"type": "function", "function": {"name": "web_search"}}
params = _oci_tool_params(
"xai.grok-4.20", [{"type": "web_search"}, web_search_function], force_web_search, drop_params=True
)
assert [tool.name for tool in params["tools"]] == ["web_search"]
assert params["toolChoice"] == {"type": "FUNCTION", "name": "web_search"}
def test_misspelled_function_choice_is_left_for_the_service(self):
params = _oci_tool_params("xai.grok-4.20", [BUILTIN_TOOL, FUNCTION_TOOL], "get_weathr", drop_params=True)
assert params["toolChoice"] == {"type": "FUNCTION", "name": "get_weathr"}
def test_function_only_tools_are_untouched_without_drop_params(self, monkeypatch):
monkeypatch.setattr(litellm, "drop_params", False)
params = _oci_tool_params("xai.grok-4.20", [FUNCTION_TOOL], FORCE_FUNCTION, drop_params=False)
assert [tool.name for tool in params["tools"]] == ["get_weather"]
assert params["toolChoice"] == {"type": "FUNCTION", "name": "get_weather"}
SERVED_GROK_4_20_MODELS = [
"xai.grok-4.20-reasoning",
"xai.grok-4.20-non-reasoning",
"xai.grok-4.20-0309-reasoning",
"xai.grok-4.20-0309-non-reasoning",
]
class TestReasoningEffortGating:
@pytest.mark.parametrize("model", SERVED_GROK_4_20_MODELS + ["xai.grok-4.20"])
def test_models_declaring_no_levels_are_gated(self, local_model_cost_map, model):
supported = OCIChatConfig().get_supported_openai_params(model)
assert "reasoning_effort" not in supported
assert "tools" in supported
@pytest.mark.parametrize(
"model",
[
"meta.llama-3.3-70b-instruct",
"meta.llama-4-scout-17b-16e-instruct",
"xai.grok-4.20-multi-agent",
"openai.gpt-5",
"openai.gpt-oss-120b",
"google.gemini-2.5-flash",
"google.gemini-2.5-flash-lite",
],
)
def test_models_declaring_nothing_are_forwarded(self, local_model_cost_map, model):
assert "reasoning_effort" in OCIChatConfig().get_supported_openai_params(model)
def test_models_absent_from_catalog_are_forwarded(self, local_model_cost_map):
assert "oci/meta.llama-9-future-instruct" not in litellm.model_cost
assert "reasoning_effort" in OCIChatConfig().get_supported_openai_params("meta.llama-9-future-instruct")
def test_gate_is_catalog_driven(self, local_model_cost_map, monkeypatch):
entry = {**litellm.model_cost["oci/meta.llama-3.3-70b-instruct"], "reasoning_effort_levels": []}
monkeypatch.setitem(litellm.model_cost, "oci/meta.llama-3.3-70b-instruct", entry)
assert "reasoning_effort" not in OCIChatConfig().get_supported_openai_params("meta.llama-3.3-70b-instruct")
@pytest.mark.parametrize("model", SERVED_GROK_4_20_MODELS)
def test_reasoning_effort_dropped_under_drop_params(self, local_model_cost_map, model):
result = OCIChatConfig().map_openai_params(
non_default_params={"reasoning_effort": "low", "temperature": 0.1},
optional_params={},
model=model,
drop_params=True,
)
assert "reasoningEffort" not in result
assert "reasoning_effort" not in result
assert result["temperature"] == 0.1
def test_reasoning_effort_raises_without_drop_params_for_gated_grok(self, local_model_cost_map, monkeypatch):
monkeypatch.setattr(litellm, "drop_params", False)
with pytest.raises(OCIError, match=r"reasoning_effort.*xai\.grok-4\.20-reasoning"):
OCIChatConfig().map_openai_params(
non_default_params={"reasoning_effort": "low"},
optional_params={},
model="xai.grok-4.20-reasoning",
drop_params=False,
)
@pytest.mark.parametrize(
"model", ["openai.gpt-5", "google.gemini-2.5-flash", "openai.gpt-oss-120b", "meta.llama-3.3-70b-instruct"]
)
def test_reasoning_effort_forwarded_when_not_gated(self, local_model_cost_map, model):
result = OCIChatConfig().map_openai_params(
non_default_params={"reasoning_effort": "low"},
optional_params={},
model=model,
drop_params=False,
)
assert result["reasoningEffort"] == "low"
def _oci_chat_request(model, optional_params):
return OCIChatConfig().transform_request(
model=model,
messages=[{"role": "user", "content": "hi"}],
optional_params={**BASE_OCI_PARAMS, **optional_params},
litellm_params={},
headers={},
)["chatRequest"]
class TestOpenAIPrefixMaxTokensKey:
@pytest.mark.parametrize("model", GPT_OSS_MODELS)
def test_gpt_oss_keeps_max_tokens(self, local_model_cost_map, model):
chat_request = _oci_chat_request(model, {"max_tokens": 64})
assert chat_request["maxTokens"] == 64
assert "maxCompletionTokens" not in chat_request
def test_gpt5_still_routes_to_max_completion_tokens(self, local_model_cost_map):
chat_request = _oci_chat_request("openai.gpt-5", {"max_tokens": 64})
assert chat_request["maxCompletionTokens"] == 64
assert "maxTokens" not in chat_request