refactor(oci/chat/transformation.py): lazy load package imports

This commit is contained in:
Krrish Dholakia 2025-08-04 17:12:36 -07:00
parent 368c9b75b9
commit 37cb443010
2 changed files with 60 additions and 42 deletions

View file

@ -1,15 +1,13 @@
import base64
import datetime
import hashlib
from urllib.parse import urlparse
import litellm
import json
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding, rsa
from urllib.parse import urlparse
import httpx
import litellm
from litellm.litellm_core_utils.logging_utils import track_llm_api_timing
from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException
from litellm.llms.custom_httpx.http_handler import (
@ -19,14 +17,7 @@ from litellm.llms.custom_httpx.http_handler import (
get_async_httpx_client,
version,
)
from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import LlmProviders
from litellm.utils import (
ChatCompletionMessageToolCall,
CustomStreamWrapper,
ModelResponse,
Usage,
)
from litellm.llms.oci.common_utils import OCIError
from litellm.types.llms.oci import (
OCIChatRequestPayload,
OCICompletionPayload,
@ -42,12 +33,19 @@ from litellm.types.llms.oci import (
OCIToolDefinition,
OCIVendors,
)
from litellm.llms.oci.common_utils import OCIError
from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import (
Delta,
LlmProviders,
ModelResponseStream,
StreamingChoices,
)
from litellm.utils import (
ChatCompletionMessageToolCall,
CustomStreamWrapper,
ModelResponse,
Usage,
)
if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
@ -74,6 +72,15 @@ def build_signature_string(method, path, headers, signed_headers):
def load_private_key_from_str(key_str: str):
try:
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
except ImportError as e:
raise ImportError(
"cryptography package is required for OCI authentication. "
"Please install it with: pip install cryptography"
) from e
key = serialization.load_pem_private_key(
key_str.encode("utf-8"),
password=None,
@ -267,6 +274,15 @@ class OCIChatConfig(BaseConfig):
method, path, headers_to_sign, signed_headers
)
try:
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
except ImportError as e:
raise ImportError(
"cryptography package is required for OCI authentication. "
"Please install it with: pip install cryptography"
) from e
private_key = load_private_key_from_str(oci_key)
signature = private_key.sign(
signing_string.encode("utf-8"),
@ -837,9 +853,11 @@ class OCIStreamWrapper(CustomStreamWrapper):
index=typed_chunk.index if typed_chunk.index else 0,
delta=Delta(
content=text,
tool_calls=[tool.model_dump() for tool in tool_calls]
if tool_calls
else None,
tool_calls=(
[tool.model_dump() for tool in tool_calls]
if tool_calls
else None
),
provider_specific_fields=None, # OCI does not have provider specific fields in the response
thinking_blocks=None, # OCI does not have thinking blocks in the response
reasoning_content=None, # OCI does not have reasoning content in the response

View file

@ -3750,7 +3750,7 @@
"max_input_tokens": 131072,
"max_output_tokens": 131072,
"input_cost_per_token": 3.3e-06,
"output_cost_per_token": 16.5e-06,
"output_cost_per_token": 1.65e-05,
"litellm_provider": "azure_ai",
"mode": "chat",
"supports_function_calling": true,
@ -3764,7 +3764,7 @@
"max_input_tokens": 131072,
"max_output_tokens": 131072,
"input_cost_per_token": 3e-06,
"output_cost_per_token": 15e-06,
"output_cost_per_token": 1.5e-05,
"litellm_provider": "azure_ai",
"mode": "chat",
"supports_function_calling": true,
@ -3777,7 +3777,7 @@
"max_tokens": 131072,
"max_input_tokens": 131072,
"max_output_tokens": 131072,
"input_cost_per_token": 0.25e-06,
"input_cost_per_token": 2.5e-07,
"output_cost_per_token": 1.27e-06,
"litellm_provider": "azure_ai",
"mode": "chat",
@ -3792,7 +3792,7 @@
"max_tokens": 131072,
"max_input_tokens": 131072,
"max_output_tokens": 131072,
"input_cost_per_token": 0.275e-06,
"input_cost_per_token": 2.75e-07,
"output_cost_per_token": 1.38e-06,
"litellm_provider": "azure_ai",
"mode": "chat",
@ -7337,12 +7337,12 @@
"max_audio_length_hours": 8.4,
"max_audio_per_prompt": 1,
"max_pdf_size_mb": 30,
"input_cost_per_token": 3.5e-07,
"input_cost_per_token": 3.5e-07,
"input_cost_per_audio_token": 2.1e-06,
"input_cost_per_image": 2.1e-06,
"input_cost_per_video_per_second": 2.1e-06,
"output_cost_per_token": 1.5e-06,
"output_cost_per_audio_token": 8.5e-06,
"output_cost_per_audio_token": 8.5e-06,
"litellm_provider": "gemini",
"mode": "chat",
"rpm": 10,
@ -9039,9 +9039,9 @@
"supports_tool_choice": true
},
"vertex_ai/meta/llama-4-scout-17b-16e-instruct-maas": {
"max_tokens": 10000000.0,
"max_input_tokens": 10000000.0,
"max_output_tokens": 10000000.0,
"max_tokens": 10000000,
"max_input_tokens": 10000000,
"max_output_tokens": 10000000,
"input_cost_per_token": 2.5e-07,
"output_cost_per_token": 7e-07,
"litellm_provider": "vertex_ai-llama_models",
@ -9059,9 +9059,9 @@
]
},
"vertex_ai/meta/llama-4-scout-17b-128e-instruct-maas": {
"max_tokens": 10000000.0,
"max_input_tokens": 10000000.0,
"max_output_tokens": 10000000.0,
"max_tokens": 10000000,
"max_input_tokens": 10000000,
"max_output_tokens": 10000000,
"input_cost_per_token": 2.5e-07,
"output_cost_per_token": 7e-07,
"litellm_provider": "vertex_ai-llama_models",
@ -9079,9 +9079,9 @@
]
},
"vertex_ai/meta/llama-4-maverick-17b-128e-instruct-maas": {
"max_tokens": 1000000.0,
"max_input_tokens": 1000000.0,
"max_output_tokens": 1000000.0,
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 1000000,
"input_cost_per_token": 3.5e-07,
"output_cost_per_token": 1.15e-06,
"litellm_provider": "vertex_ai-llama_models",
@ -9099,9 +9099,9 @@
]
},
"vertex_ai/meta/llama-4-maverick-17b-16e-instruct-maas": {
"max_tokens": 1000000.0,
"max_input_tokens": 1000000.0,
"max_output_tokens": 1000000.0,
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 1000000,
"input_cost_per_token": 3.5e-07,
"output_cost_per_token": 1.15e-06,
"litellm_provider": "vertex_ai-llama_models",
@ -9174,7 +9174,7 @@
"max_input_tokens": 128000,
"max_output_tokens": 2048,
"input_cost_per_token": 5e-06,
"output_cost_per_token": 16e-06,
"output_cost_per_token": 1.6e-05,
"litellm_provider": "vertex_ai-llama_models",
"mode": "chat",
"supports_system_messages": true,
@ -10480,7 +10480,7 @@
"supports_tool_choice": true,
"supports_prompt_caching": true
},
"openrouter/x-ai/grok-4":{
"openrouter/x-ai/grok-4": {
"max_tokens": 256000,
"max_input_tokens": 256000,
"max_output_tokens": 256000,
@ -10494,12 +10494,12 @@
"source": "https://openrouter.ai/x-ai/grok-4",
"supports_web_search": true
},
"openrouter/bytedance/ui-tars-1.5-7b":{
"openrouter/bytedance/ui-tars-1.5-7b": {
"max_tokens": 2048,
"max_input_tokens": 131072,
"max_output_tokens": 2048,
"input_cost_per_token": 0.1e-06,
"output_cost_per_token": 0.2e-06,
"input_cost_per_token": 1e-07,
"output_cost_per_token": 2e-07,
"litellm_provider": "openrouter",
"mode": "chat",
"source": "https://openrouter.ai/api/v1/models/bytedance/ui-tars-1.5-7b",
@ -11178,8 +11178,8 @@
"max_tokens": 8192,
"max_input_tokens": 8192,
"max_output_tokens": 2048,
"input_cost_per_token": 0.21e-06,
"output_cost_per_token": 0.63e-06,
"input_cost_per_token": 2.1e-07,
"output_cost_per_token": 6.3e-07,
"litellm_provider": "openrouter",
"mode": "chat",
"supports_tool_choice": true
@ -14763,7 +14763,7 @@
"max_tokens": 131072,
"max_input_tokens": 131072,
"max_output_tokens": 16384,
"input_cost_per_token": 0.6e-06,
"input_cost_per_token": 6e-07,
"output_cost_per_token": 2.5e-06,
"litellm_provider": "fireworks_ai",
"mode": "chat",