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
Azure `api_version="preview"` support + Bedrock cost tracking via Anthropic `/v1/messages` (#13072)
Krish Dholakia039c8a922ce3
diff --git a/litellm/llms/azure/chat/gpt_transformation.py b/litellm/llms/azure/chat/gpt_transformation.pyindex 78fa91bbd77..0ae6fad7300 100644--- a/litellm/llms/azure/chat/gpt_transformation.py+++ b/litellm/llms/azure/chat/gpt_transformation.py@@ -159,9 +159,16 @@ class AzureOpenAIConfig(BaseConfig):supported_openai_params = self.get_supported_openai_params(model)api_version_times = api_version.split("-")- api_version_year = api_version_times[0]- api_version_month = api_version_times[1]- api_version_day = api_version_times[2]++ if len(api_version_times) >= 3:+ api_version_year = api_version_times[0]+ api_version_month = api_version_times[1]+ api_version_day = api_version_times[2]+ else:+ api_version_year = None+ api_version_month = None+ api_version_day = None+for param, value in non_default_params.items():if param == "tool_choice":"""@@ -171,47 +178,57 @@ class AzureOpenAIConfig(BaseConfig):"""## check if api version supports this param ##if (- api_version_year < "2023"- or (api_version_year == "2023" and api_version_month < "12")- or (- api_version_year == "2023"- and api_version_month == "12"- and api_version_day < "01"- )+ api_version_year is None+ or api_version_month is None+ or api_version_day is None):- if litellm.drop_params is True or (- drop_params is not None and drop_params is True- ):- pass- else:- raise UnsupportedParamsError(- status_code=400,- message=f"""Azure does not support 'tool_choice', for api_version={api_version}. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions""",+ optional_params["tool_choice"] = value+ else:+ if (+ api_version_year < "2023"+ or (api_version_year == "2023" and api_version_month < "12")+ or (+ api_version_year == "2023"+ and api_version_month == "12"+ and api_version_day < "01")- elif value == "required" and (- api_version_year == "2024" and api_version_month <= "05"- ): ## check if tool_choice value is supported ##- if litellm.drop_params is True or (- drop_params is not None and drop_params is True):- pass+ if litellm.drop_params is True or (+ drop_params is not None and drop_params is True+ ):+ pass+ else:+ raise UnsupportedParamsError(+ status_code=400,+ message=f"""Azure does not support 'tool_choice', for api_version={api_version}. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions""",+ )+ elif value == "required" and (+ api_version_year == "2024" and api_version_month <= "05"+ ): ## check if tool_choice value is supported ##+ if litellm.drop_params is True or (+ drop_params is not None and drop_params is True+ ):+ pass+ else:+ raise UnsupportedParamsError(+ status_code=400,+ message=f"Azure does not support '{value}' as a {param} param, for api_version={api_version}. To drop 'tool_choice=required' for calls with this Azure API version, set `litellm.drop_params=True` or for proxy:\n\n`litellm_settings:\n drop_params: true`\nAzure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions",+ )else:- raise UnsupportedParamsError(- status_code=400,- message=f"Azure does not support '{value}' as a {param} param, for api_version={api_version}. To drop 'tool_choice=required' for calls with this Azure API version, set `litellm.drop_params=True` or for proxy:\n\n`litellm_settings:\n drop_params: true`\nAzure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions",- )- else:- optional_params["tool_choice"] = value+ optional_params["tool_choice"] = valueelif param == "response_format" and isinstance(value, dict):_is_response_format_supported_model = (self._is_response_format_supported_model(model))- is_response_format_supported_api_version = (- self._is_response_format_supported_api_version(- api_version_year, api_version_month+ if api_version_year is None or api_version_month is None:+ is_response_format_supported_api_version = True+ else:+ is_response_format_supported_api_version = (+ self._is_response_format_supported_api_version(+ api_version_year, api_version_month+ ))- )is_response_format_supported = (is_response_format_supported_api_versionand _is_response_format_supported_modeldiff --git a/litellm/proxy/_experimental/out/model_hub_table.html b/litellm/proxy/_experimental/out/model_hub_table/index.htmlsimilarity index 100%rename from litellm/proxy/_experimental/out/model_hub_table.htmlrename to litellm/proxy/_experimental/out/model_hub_table/index.htmldiff --git a/litellm/proxy/_experimental/out/onboarding.html b/litellm/proxy/_experimental/out/onboarding.htmldeleted file mode 100644index bd800bb0db6..00000000000--- a/litellm/proxy/_experimental/out/onboarding.html+++ /dev/null@@ -1 +0,0 @@-<!DOCTYPE html><html id="__next_error__"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="preload" as="script" fetchPriority="low" href="/litellm-asset-prefix/_next/static/chunks/webpack-a426aae3231a8df1.js"/><script src="/litellm-asset-prefix/_next/static/chunks/fd9d1056-205af899b895cbac.js" async=""></script><script src="/litellm-asset-prefix/_next/static/chunks/117-a0da667066d322b6.js" async=""></script><script src="/litellm-asset-prefix/_next/static/chunks/main-app-4f7318ae681a6d94.js" async=""></script><title>LiteLLM Dashboard</title><meta name="description" content="LiteLLM Proxy Admin UI"/><link rel="icon" href="/favicon.ico" type="image/x-icon" sizes="16x16"/><link rel="icon" href="./favicon.ico"/><meta name="next-size-adjust"/><script src="/litellm-asset-prefix/_next/static/chunks/polyfills-42372ed130431b0a.js" noModule=""></script></head><body><script src="/litellm-asset-prefix/_next/static/chunks/webpack-a426aae3231a8df1.js" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0]);self.__next_f.push([2,null])</script><script>self.__next_f.push([1,"1:HL[\"/litellm-asset-prefix/_next/static/media/e4af272ccee01ff0-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n2:HL[\"/litellm-asset-prefix/_next/static/css/31b7f215e119031e.css\",\"style\"]\n3:HL[\"/litellm-asset-prefix/_next/static/css/dd3d607f47531000.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"4:I[12846,[],\"\"]\n6:I[19107,[],\"ClientPageRoot\"]\n7:I[12011,[\"665\",\"static/chunks/3014691f-b7b79b78e27792f3.js\",\"445\",\"static/chunks/445-e14a4d059f7305d5.js\",\"634\",\"static/chunks/634-74ee1cee62277068.js\",\"338\",\"static/chunks/338-5db4fbaf3c5da53d.js\",\"461\",\"static/chunks/app/onboarding/page-acdf578c06ba6714.js\"],\"default\",1]\n8:I[4707,[],\"\"]\n9:I[36423,[],\"\"]\nb:I[61060,[],\"\"]\nc:[]\n"])</script><script>self.__next_f.push([1,"0:[\"$\",\"$L4\",null,{\"buildId\":\"N-wLM4VjZ1bVvg2NAx9GX\",\"assetPrefix\":\"/litellm-asset-prefix\",\"urlParts\":[\"\",\"onboarding\"],\"initialTree\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{}]}]},\"$undefined\",\"$undefined\",true],\"initialSeedData\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{},[[\"$L5\",[\"$\",\"$L6\",null,{\"props\":{\"params\":{},\"searchParams\":{}},\"Component\":\"$7\"}],null],null],null]},[null,[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"onboarding\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\"}]],null]},[[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/litellm-asset-prefix/_next/static/css/31b7f215e119031e.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/litellm-asset-prefix/_next/static/css/dd3d607f47531000.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],[\"$\",\"html\",null,{\"lang\":\"en\",\"children\":[\"$\",\"body\",null,{\"className\":\"__className_b0dd8a\",\"children\":[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[\"$\",\"title\",null,{\"children\":\"404: This page could not be found.\"}],[\"$\",\"div\",null,{\"style\":{\"fontFamily\":\"system-ui,\\\"Segoe UI\\\",Roboto,Helvetica,Arial,sans-serif,\\\"Apple Color Emoji\\\",\\\"Segoe UI Emoji\\\"\",\"height\":\"100vh\",\"textAlign\":\"center\",\"display\":\"flex\",\"flexDirection\":\"column\",\"alignItems\":\"center\",\"justifyContent\":\"center\"},\"children\":[\"$\",\"div\",null,{\"children\":[[\"$\",\"style\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}\"}}],[\"$\",\"h1\",null,{\"className\":\"next-error-h1\",\"style\":{\"display\":\"inline-block\",\"margin\":\"0 20px 0 0\",\"padding\":\"0 23px 0 0\",\"fontSize\":24,\"fontWeight\":500,\"verticalAlign\":\"top\",\"lineHeight\":\"49px\"},\"children\":\"404\"}],[\"$\",\"div\",null,{\"style\":{\"display\":\"inline-block\"},\"children\":[\"$\",\"h2\",null,{\"style\":{\"fontSize\":14,\"fontWeight\":400,\"lineHeight\":\"49px\",\"margin\":0},\"children\":\"This page could not be found.\"}]}]]}]}]],\"notFoundStyles\":[]}]}]}]],null],null],\"couldBeIntercepted\":false,\"initialHead\":[null,\"$La\"],\"globalErrorComponent\":\"$b\",\"missingSlots\":\"$Wc\"}]\n"])</script><script>self.__next_f.push([1,"a:[[\"$\",\"meta\",\"0\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"}],[\"$\",\"meta\",\"1\",{\"charSet\":\"utf-8\"}],[\"$\",\"title\",\"2\",{\"children\":\"LiteLLM Dashboard\"}],[\"$\",\"meta\",\"3\",{\"name\":\"description\",\"content\":\"LiteLLM Proxy Admin UI\"}],[\"$\",\"link\",\"4\",{\"rel\":\"icon\",\"href\":\"/favicon.ico\",\"type\":\"image/x-icon\",\"sizes\":\"16x16\"}],[\"$\",\"link\",\"5\",{\"rel\":\"icon\",\"href\":\"./favicon.ico\"}],[\"$\",\"meta\",\"6\",{\"name\":\"next-size-adjust\"}]]\n5:null\n"])</script></body></html>\ No newline at end of filediff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex c9d8c4f7f51..af11bd8aa52 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -1,18 +1,5 @@model_list:- - model_name: claude-sonnet-4+ - model_name: bedrock-claude-3.7-sonnetlitellm_params:- model: anthropic/claude-sonnet-4-20250514- api_key: os.environ/ANTHROPIC_API_KEY- api_base: https://api.anthropic.com/v1- guardrails: ["azure-text-moderation"]- - model_name: openai-gpt-4o- litellm_params:- model: openai/gpt-4o+ model: bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0-guardrails:- - guardrail_name: azure-text-moderation- litellm_params:- guardrail: azure/text_moderations- mode: "post_call"- api_key: os.environ/AZURE_GUARDRAIL_API_KEY- api_base: os.environ/AZURE_GUARDRAIL_API_BASEdiff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.pyindex 0f1f215139b..a564419c797 100644--- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py+++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py@@ -1,6 +1,6 @@import jsonfrom datetime import datetime-from typing import TYPE_CHECKING, Any, List, Optional, Union+from typing import TYPE_CHECKING, Any, List, Optional, Union, castimport httpx@@ -96,10 +96,12 @@ class AnthropicPassthroughLoggingHandler:handles streaming and non-streaming responses"""try:+response_cost = litellm.completion_cost(completion_response=litellm_model_response,model=model,)+kwargs["response_cost"] = response_costkwargs["model"] = modelpassthrough_logging_payload: Optional[PassthroughStandardLoggingPayload] = ( # type: ignore@@ -125,9 +127,10 @@ class AnthropicPassthroughLoggingHandler:litellm_model_response.id = logging_obj.litellm_call_idlitellm_model_response.model = modellogging_obj.model_call_details["model"] = model- logging_obj.model_call_details["custom_llm_provider"] = (- litellm.LlmProviders.ANTHROPIC.value- )+ if not logging_obj.model_call_details.get("custom_llm_provider"):+ logging_obj.model_call_details["custom_llm_provider"] = (+ litellm.LlmProviders.ANTHROPIC.value+ )return kwargsexcept Exception as e:verbose_proxy_logger.exception(@@ -155,6 +158,19 @@ class AnthropicPassthroughLoggingHandler:"""model = request_body.get("model", "")+ # Dheck if it's available in the logging object+ if (+ not model+ and hasattr(litellm_logging_obj, "model_call_details")+ and litellm_logging_obj.model_call_details.get("model")+ ):+ model = cast(str, litellm_logging_obj.model_call_details.get("model"))+ custom_llm_provider = litellm_logging_obj.model_call_details.get(+ "custom_llm_provider"+ )++ if custom_llm_provider and not model.startswith(custom_llm_provider):+ model = f"{custom_llm_provider}/{model}"complete_streaming_response = (AnthropicPassthroughLoggingHandler._build_complete_streaming_response(all_chunks=all_chunks,diff --git a/tests/pass_through_unit_tests/test_anthropic_messages_passthrough.py b/tests/pass_through_unit_tests/test_anthropic_messages_passthrough.pyindex 6dc0c4a83e1..098daf78938 100644--- a/tests/pass_through_unit_tests/test_anthropic_messages_passthrough.py+++ b/tests/pass_through_unit_tests/test_anthropic_messages_passthrough.py@@ -25,6 +25,7 @@ from litellm.router import Routerimport importlibfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom base_anthropic_unified_messages_test import BaseAnthropicMessagesTest+# Load environment variablesload_dotenv()@@ -70,59 +71,58 @@ def _validate_anthropic_response(response: Dict[str, Any]):assert response["role"] == "assistant"-class TestAnthropicDirectAPI(BaseAnthropicMessagesTest):"""Tests for direct Anthropic API calls"""+@propertydef model_config(self) -> Dict[str, Any]:return {"model": "claude-3-haiku-20240307","api_key": os.getenv("ANTHROPIC_API_KEY"),}-+@propertydef expected_model_name_in_logging(self) -> str:"""This is the model name that is expected to be in the logging payload"""return "claude-3-haiku-20240307"--+class TestAnthropicBedrockAPI(BaseAnthropicMessagesTest):"""Tests for Anthropic via Bedrock"""+@propertydef model_config(self) -> Dict[str, Any]:return {"model": "bedrock/us.anthropic.claude-3-5-sonnet-20240620-v1:0",}-@propertydef expected_model_name_in_logging(self) -> str:"""This is the model name that is expected to be in the logging payload"""- return "us.anthropic.claude-3-5-sonnet-20240620-v1:0"-+ return "bedrock/us.anthropic.claude-3-5-sonnet-20240620-v1:0"class TestAnthropicOpenAIAPI(BaseAnthropicMessagesTest):"""Tests for OpenAI via Anthropic messages interface"""+@propertydef model_config(self) -> Dict[str, Any]:return {"model": "openai/gpt-4o-mini","client": None,}-+@propertydef expected_model_name_in_logging(self) -> str:"""This is the model name that is expected to be in the logging payload"""return "gpt-4o-mini"-+@pytest.mark.asyncioasync def test_anthropic_messages_litellm_router_streaming_with_logging(self):"""@@ -151,8 +151,8 @@ async def test_anthropic_messages_streaming_with_bad_request():except Exception as e:print("got exception", e)print("vars", vars(e))- if hasattr(e, 'status_code'):- assert getattr(e, 'status_code') == 400+ if hasattr(e, "status_code"):+ assert getattr(e, "status_code") == 400else:assert isinstance(e, Exception)@@ -188,8 +188,8 @@ async def test_anthropic_messages_router_streaming_with_bad_request():except Exception as e:print("got exception", e)print("vars", vars(e))- if hasattr(e, 'status_code'):- assert getattr(e, 'status_code') == 400+ if hasattr(e, "status_code"):+ assert getattr(e, "status_code") == 400else:assert isinstance(e, Exception)@@ -231,6 +231,7 @@ async def test_anthropic_messages_litellm_router_non_streaming():print(f"Non-streaming response: {json.dumps(response, indent=2)}")return response+@pytest.mark.asyncioasync def test_anthropic_messages_litellm_router_routing_strategy():"""@@ -260,7 +261,7 @@ async def test_anthropic_messages_litellm_router_routing_strategy():max_tokens=100,metadata={"user_id": "hello",- }+ },)# Verify response@@ -276,10 +277,10 @@ async def test_anthropic_messages_litellm_router_routing_strategy():@pytest.mark.asyncioasync def test_anthropic_messages_litellm_router_latency_metadata_tracking():"""- Test the anthropic_messages with routing strategy and verify that _latency_per_deployment+ Test the anthropic_messages with routing strategy and verify that _latency_per_deploymentfield is passed in litellm_metadata when calling litellm.anthropic_messages"""- with unittest.mock.patch('litellm.anthropic_messages') as mock_anthropic_messages:+ with unittest.mock.patch("litellm.anthropic_messages") as mock_anthropic_messages:# Mock the return valuemock_response = {"id": "msg_123456",@@ -293,7 +294,7 @@ async def test_anthropic_messages_litellm_router_latency_metadata_tracking():mock_anthropic_messages.return_value = mock_response# Set the __name__ attribute that the router expectsmock_anthropic_messages.__name__ = "anthropic_messages"-+MODEL_GROUP = "claude-special-alias"router = Router(model_list=[@@ -318,51 +319,61 @@ async def test_anthropic_messages_litellm_router_latency_metadata_tracking():max_tokens=100,metadata={"user_id": "hello",- }+ },)# Verify responseassert response == mock_response-+# Verify that litellm.anthropic_messages was calledmock_anthropic_messages.assert_called_once()-+# Get the call argumentscall_args = mock_anthropic_messages.call_argscall_kwargs = call_args.kwargs-+print("Call kwargs:", json.dumps(call_kwargs, indent=2, default=str))-+# Verify that litellm_metadata was passed and contains _latency_per_deployment- assert "litellm_metadata" in call_kwargs, "litellm_metadata should be passed to anthropic_messages"-+ assert (+ "litellm_metadata" in call_kwargs+ ), "litellm_metadata should be passed to anthropic_messages"+litellm_metadata = call_kwargs["litellm_metadata"]assert litellm_metadata is not None, "litellm_metadata should not be None"- assert isinstance(litellm_metadata, dict), "litellm_metadata should be a dictionary"-+ assert isinstance(+ litellm_metadata, dict+ ), "litellm_metadata should be a dictionary"+# Verify _latency_per_deployment is present- assert "_latency_per_deployment" in litellm_metadata, "litellm_metadata should contain _latency_per_deployment field"-+ assert (+ "_latency_per_deployment" in litellm_metadata+ ), "litellm_metadata should contain _latency_per_deployment field"+# Verify the structure of _latency_per_deploymentlatency_per_deployment = litellm_metadata["_latency_per_deployment"]- assert isinstance(latency_per_deployment, dict), "_latency_per_deployment should be a dictionary"-+ assert isinstance(+ latency_per_deployment, dict+ ), "_latency_per_deployment should be a dictionary"+print(f"✅ Latency per deployment data: {latency_per_deployment}")-+# Verify other expected fields in litellm_metadataassert "model_group" in litellm_metadataassert litellm_metadata["model_group"] == MODEL_GROUPassert "deployment" in litellm_metadataassert "model_info" in litellm_metadata-+# Verify other call parametersassert call_kwargs["model"] == "claude-3-haiku-20240307"assert call_kwargs["messages"] == messagesassert call_kwargs["max_tokens"] == 100assert call_kwargs["metadata"] == {"user_id": "hello"}-- print("✅ Successfully verified that _latency_per_deployment is passed in litellm_metadata to anthropic_messages")-++ print(+ "✅ Successfully verified that _latency_per_deployment is passed in litellm_metadata to anthropic_messages"+ )+return response@@ -417,9 +428,16 @@ async def test_anthropic_messages_litellm_router_non_streaming_with_logging():print(f"Non-streaming response: {json.dumps(response, indent=2)}")await asyncio.sleep(1)-- assert test_custom_logger.logged_standard_logging_payload is not None, "Logging payload should not be None"- print("tracked standard logging payload", json.dumps(test_custom_logger.logged_standard_logging_payload, indent=4, default=str))++ assert (+ test_custom_logger.logged_standard_logging_payload is not None+ ), "Logging payload should not be None"+ print(+ "tracked standard logging payload",+ json.dumps(+ test_custom_logger.logged_standard_logging_payload, indent=4, default=str+ ),+ )assert test_custom_logger.logged_standard_logging_payload["messages"] == messagesassert test_custom_logger.logged_standard_logging_payload["response"] is not Noneassert (@@ -439,8 +457,9 @@ async def test_anthropic_messages_litellm_router_non_streaming_with_logging():)# assert model_group- assert test_custom_logger.logged_standard_logging_payload["model_group"] == MODEL_GROUP-+ assert (+ test_custom_logger.logged_standard_logging_payload["model_group"] == MODEL_GROUP+ )@pytest.mark.asyncio@@ -510,7 +529,6 @@ async def test_anthropic_messages_with_extra_headers():return response-@pytest.mark.asyncioasync def test_anthropic_messages_with_thinking():"""@@ -522,7 +540,6 @@ async def test_anthropic_messages_with_thinking():# Set up test parametersmessages = [{"role": "user", "content": "Hello, can you tell me a short joke?"}]-# Create a mock responsemock_response = MagicMock()mock_response.raise_for_status = MagicMock()@@ -568,7 +585,6 @@ async def test_anthropic_messages_with_thinking():assert request_body["messages"] == messagesassert request_body["thinking"] == {"budget_tokens": 100}-# Verify the response was processed correctlyassert response == mock_response.json.return_value@@ -582,18 +598,22 @@ async def test_anthropic_messages_bedrock_credentials_passthrough():when using anthropic.messages.acreate with a bedrock model"""# Mock the get_credentials method- with unittest.mock.patch.object(BaseAWSLLM, 'get_credentials') as mock_get_credentials:+ with unittest.mock.patch.object(+ BaseAWSLLM, "get_credentials"+ ) as mock_get_credentials:# Create a proper mock for credentials with the necessary attributesmock_credentials = unittest.mock.MagicMock()mock_credentials.access_key = "mock_access_key"mock_credentials.secret_key = "mock_secret_key"mock_credentials.token = "mock_session_token"mock_get_credentials.return_value = mock_credentials-+# We also need to mock the actual AWS request signing to avoid real API calls- with unittest.mock.patch('botocore.auth.SigV4Auth.add_auth'):+ with unittest.mock.patch("botocore.auth.SigV4Auth.add_auth"):# Set up mock for AsyncHTTPHandler.post to avoid actual API calls- with unittest.mock.patch('litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post') as mock_post:+ with unittest.mock.patch(+ "litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post"+ ) as mock_post:# Configure mock responsemock_response = unittest.mock.MagicMock()mock_response.raise_for_status = unittest.mock.MagicMock()@@ -607,7 +627,7 @@ async def test_anthropic_messages_bedrock_credentials_passthrough():"usage": {"input_tokens": 10, "output_tokens": 20},}mock_post.return_value = mock_response-+# Test AWS credentials parameters - separate from function call parametersaws_params = {"aws_access_key_id": "test_access_key",@@ -620,7 +640,7 @@ async def test_anthropic_messages_bedrock_credentials_passthrough():"aws_web_identity_token": "test_web_identity_token","aws_sts_endpoint": "https://sts.test-region.amazonaws.com",}-+# Call the function with AWS credentialsawait litellm.anthropic.messages.acreate(messages=[{"role": "user", "content": "Hello, test credentials"}],@@ -628,15 +648,16 @@ async def test_anthropic_messages_bedrock_credentials_passthrough():max_tokens=100,**aws_params,)-+# Verify get_credentials was called with the correct parametersmock_get_credentials.assert_called_once()call_args = mock_get_credentials.call_args[1]-+# Assert that our test credentials were passed correctlyfor param_name, param_value in aws_params.items():- assert call_args[param_name] == param_value, f"Parameter {param_name} was not passed correctly"-+ assert (+ call_args[param_name] == param_value+ ), f"Parameter {param_name} was not passed correctly"@pytest.mark.asyncio@@ -662,19 +683,22 @@ async def test_anthropic_messages_bedrock_dynamic_region():mock_client.post = AsyncMock(return_value=mock_response)# Patch necessary AWS components- with unittest.mock.patch('botocore.auth.SigV4Auth.add_auth'), \- unittest.mock.patch.object(BaseAWSLLM, 'get_credentials') as mock_get_credentials:-+ with unittest.mock.patch(+ "botocore.auth.SigV4Auth.add_auth"+ ), unittest.mock.patch.object(+ BaseAWSLLM, "get_credentials"+ ) as mock_get_credentials:+# Setup mock credentialsmock_credentials = unittest.mock.MagicMock()mock_credentials.access_key = "test_access_key"mock_credentials.secret_key = "test_secret_key"mock_credentials.token = "test_session_token"mock_get_credentials.return_value = mock_credentials-+# Test with specific regiontest_region = "us-east-1"-+# Call anthropic.messages.acreate with aws_region_nameresponse = await litellm.anthropic.messages.acreate(messages=[{"role": "user", "content": "Hello, test region"}],@@ -683,22 +707,24 @@ async def test_anthropic_messages_bedrock_dynamic_region():aws_region_name=test_region,client=mock_client,)-+# Verify responseassert response == mock_response.json.return_value-+# Verify the post method was called with the correct URL containing the regionmock_client.post.assert_called_once()call_args = mock_client.post.call_args-+# Check that the URL contains the correct region- url = call_args.kwargs.get('url', '')- assert f"bedrock-runtime.{test_region}.amazonaws.com" in url, f"URL does not contain the correct region. URL: {url}"-+ url = call_args.kwargs.get("url", "")+ assert (+ f"bedrock-runtime.{test_region}.amazonaws.com" in url+ ), f"URL does not contain the correct region. URL: {url}"+# Verify get_credentials was called with the correct regionmock_get_credentials.assert_called_once()credentials_args = mock_get_credentials.call_args.kwargs- assert credentials_args.get('aws_region_name') == test_region+ assert credentials_args.get("aws_region_name") == test_regiondef test_sync_openai_messages():@@ -716,4 +742,3 @@ def test_sync_openai_messages():assert response is not Noneassert isinstance(response, dict)assert response["content"][0].text is not None-diff --git a/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py b/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.pyindex ac873710d48..8df35a37514 100644--- a/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py+++ b/tests/test_litellm/llms/azure/chat/test_azure_chat_gpt_transformation.py@@ -28,3 +28,17 @@ class TestAzureOpenAIConfig:assert not config._is_response_format_supported_model("gpt-3-5-turbo-suffix")assert not config._is_response_format_supported_model("gpt-35-turbo-suffix")assert not config._is_response_format_supported_model("gpt-35-turbo")+++def test_map_openai_params_with_preview_api_version():+ config = AzureOpenAIConfig()+ non_default_params = {+ "response_format": {"type": "json_object"},+ }+ optional_params = {}+ model = "azure/gpt-4-1"+ drop_params = False+ api_version = "preview"+ assert config.map_openai_params(+ non_default_params, optional_params, model, drop_params, api_version+ )diff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.pynew file mode 100644index 00000000000..e54e537eed1--- /dev/null+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py@@ -0,0 +1,154 @@+import json+import os+import sys+from datetime import datetime+from typing import Any, Dict, List+from unittest.mock import MagicMock, patch++import pytest++sys.path.insert(+ 0, os.path.abspath("../../..")+) # Adds the parent directory to the system path++from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.proxy.pass_through_endpoints.llm_provider_handlers.anthropic_passthrough_logging_handler import (+ AnthropicPassthroughLoggingHandler,+)+++class TestAnthropicLoggingHandlerModelFallback:+ """Test the model fallback logic in the anthropic passthrough logging handler."""++ def setup_method(self):+ """Set up test fixtures"""+ self.start_time = datetime.now()+ self.end_time = datetime.now()+ self.mock_chunks = [+ '{"type": "message_start", "message": {"id": "msg_123", "model": "claude-3-haiku-20240307"}}',+ '{"type": "content_block_delta", "delta": {"text": "Hello"}}',+ '{"type": "content_block_delta", "delta": {"text": " world"}}',+ '{"type": "message_stop"}',+ ]++ def _create_mock_logging_obj(self, model_in_details: str = None) -> LiteLLMLoggingObj:+ """Create a mock logging object with optional model in model_call_details"""+ mock_logging_obj = MagicMock()++ if model_in_details:+ # Create a dict-like mock that returns the model for the 'model' key+ mock_model_call_details = {'model': model_in_details}+ mock_logging_obj.model_call_details = mock_model_call_details+ else:+ # Create empty dict or None+ mock_logging_obj.model_call_details = {}++ return mock_logging_obj++ def _create_mock_passthrough_handler(self):+ """Create a mock passthrough success handler"""+ mock_handler = MagicMock()+ return mock_handler++++ @patch.object(AnthropicPassthroughLoggingHandler, '_build_complete_streaming_response')+ @patch.object(AnthropicPassthroughLoggingHandler, '_create_anthropic_response_logging_payload')+ def test_model_from_request_body_used_when_present(self, mock_create_payload, mock_build_response):+ """Test that model from request_body is used when present"""+ # Arrange+ request_body = {"model": "claude-3-sonnet-20240229"}+ logging_obj = self._create_mock_logging_obj(model_in_details="claude-3-haiku-20240307")+ passthrough_handler = self._create_mock_passthrough_handler()++ # Mock successful response building+ mock_build_response.return_value = MagicMock()+ mock_create_payload.return_value = {"test": "payload"}++ # Act+ result = AnthropicPassthroughLoggingHandler._handle_logging_anthropic_collected_chunks(+ litellm_logging_obj=logging_obj,+ passthrough_success_handler_obj=passthrough_handler,+ url_route="/anthropic/v1/messages",+ request_body=request_body,+ endpoint_type="messages",+ start_time=self.start_time,+ all_chunks=self.mock_chunks,+ end_time=self.end_time,+ )++ # Assert+ assert result is not None+ # Verify that _build_complete_streaming_response was called with the request_body model+ mock_build_response.assert_called_once()+ call_args = mock_build_response.call_args+ assert call_args[1]['model'] == "claude-3-sonnet-20240229" # Should use request_body model++ def test_model_fallback_logic_isolated(self):+ """Test just the model fallback logic in isolation"""+ # Test case 1: Model from request body+ request_body = {"model": "claude-3-sonnet-20240229"}+ logging_obj = self._create_mock_logging_obj(model_in_details="claude-3-haiku-20240307")++ # Extract the logic directly from the function+ model = request_body.get("model", "")+ if not model and hasattr(logging_obj, 'model_call_details') and logging_obj.model_call_details.get('model'):+ model = logging_obj.model_call_details.get('model')++ assert model == "claude-3-sonnet-20240229" # Should use request_body model++ # Test case 2: Fallback to logging obj+ request_body = {}+ logging_obj = self._create_mock_logging_obj(model_in_details="claude-3-haiku-20240307")++ model = request_body.get("model", "")+ if not model and hasattr(logging_obj, 'model_call_details') and logging_obj.model_call_details.get('model'):+ model = logging_obj.model_call_details.get('model')++ assert model == "claude-3-haiku-20240307" # Should use fallback model++ # Test case 3: Empty string in request body, fallback to logging obj+ request_body = {"model": ""}+ logging_obj = self._create_mock_logging_obj(model_in_details="claude-3-opus-20240229")++ model = request_body.get("model", "")+ if not model and hasattr(logging_obj, 'model_call_details') and logging_obj.model_call_details.get('model'):+ model = logging_obj.model_call_details.get('model')++ assert model == "claude-3-opus-20240229" # Should use fallback model++ # Test case 4: Both empty+ request_body = {}+ logging_obj = self._create_mock_logging_obj()++ model = request_body.get("model", "")+ if not model and hasattr(logging_obj, 'model_call_details') and logging_obj.model_call_details.get('model'):+ model = logging_obj.model_call_details.get('model')++ assert model == "" # Should be empty++ def test_edge_case_missing_model_call_details_attribute(self):+ """Test fallback behavior when logging_obj doesn't have model_call_details attribute"""+ # Case where logging_obj doesn't have the attribute at all+ request_body = {"model": ""} # Empty model in request body+ logging_obj = MagicMock()+ # Remove the attribute to simulate it not existing+ if hasattr(logging_obj, 'model_call_details'):+ delattr(logging_obj, 'model_call_details')++ # Extract the logic directly from the function+ model = request_body.get("model", "")+ if not model and hasattr(logging_obj, 'model_call_details') and logging_obj.model_call_details.get('model'):+ model = logging_obj.model_call_details.get('model')++ assert model == "" # Should remain empty since no fallback available++ # Case where model_call_details exists but get returns None+ request_body = {"model": ""}+ logging_obj = self._create_mock_logging_obj() # Empty dict++ model = request_body.get("model", "")+ if not model and hasattr(logging_obj, 'model_call_details') and logging_obj.model_call_details.get('model'):+ model = logging_obj.model_call_details.get('model')++ assert model == "" # Should remain empty\ No newline at end of file