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
Litellm dev 12 11 2024 v2 (#7215)
Krish Dholakia7643b3087c8c
diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.pyindex 5291f408260..370258a6677 100644--- a/litellm/litellm_core_utils/prompt_templates/common_utils.py+++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py@@ -81,6 +81,13 @@ def convert_content_list_to_str(message: AllMessageValues) -> str:return texts+def is_non_content_values_set(message: AllMessageValues) -> bool:+ ignore_keys = ["content", "role", "name"]+ return any(+ message.get(key, None) is not None for key in message if key not in ignore_keys+ )++def _audio_or_image_in_message_content(message: AllMessageValues) -> bool:"""Checks if message content contains an image or audiodiff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.pyindex 13b85a3dc2c..9877c683c92 100644--- a/litellm/litellm_core_utils/prompt_templates/factory.py+++ b/litellm/litellm_core_utils/prompt_templates/factory.py@@ -38,9 +38,11 @@ from litellm.types.llms.openai import (ChatCompletionToolCallFunctionChunk,ChatCompletionToolMessage,ChatCompletionUserMessage,+ OpenAIMessageContentListBlock,)from litellm.types.utils import GenericImageParsingChunk+from .common_utils import convert_content_list_to_str, is_non_content_values_setfrom .image_handling import async_convert_url_to_base64, convert_url_to_base64@@ -61,14 +63,15 @@ DEFAULT_USER_CONTINUE_MESSAGE = {} # similar to autogen. Only used if `litellm.modify_params=True`.# used to interweave assistant messages, to ensure user/assistant alternating-DEFAULT_ASSISTANT_CONTINUE_MESSAGE = {- "role": "assistant",- "content": [+DEFAULT_ASSISTANT_CONTINUE_MESSAGE = ChatCompletionAssistantMessage(+ role="assistant",+ content=[{+ "type": "text","text": "Please continue.",}],-} # similar to autogen. Only used if `litellm.modify_params=True`.+) # similar to autogen. Only used if `litellm.modify_params=True`.def map_system_message_pt(messages: list) -> list:@@ -2404,7 +2407,9 @@ def _convert_to_bedrock_tool_call_result(def _insert_assistant_continue_message(messages: List[BedrockMessageBlock],- assistant_continue_message: Optional[str] = None,+ assistant_continue_message: Optional[+ Union[str, ChatCompletionAssistantMessage]+ ] = None,) -> List[BedrockMessageBlock]:"""Add dummy message between user/tool result blocks.@@ -2412,23 +2417,248 @@ def _insert_assistant_continue_message(Conversation blocks and tool result blocks cannot be provided in the same turn. Issue: https://github.com/BerriAI/litellm/issues/6053"""if assistant_continue_message is not None:+ if isinstance(assistant_continue_message, str):+ messages.append(+ BedrockMessageBlock(+ role="assistant",+ content=[BedrockContentBlock(text=assistant_continue_message)],+ )+ )+ elif isinstance(assistant_continue_message, dict):+ text = convert_content_list_to_str(assistant_continue_message)+ messages.append(+ BedrockMessageBlock(+ role="assistant",+ content=[BedrockContentBlock(text=text)],+ )+ )+ elif litellm.modify_params:+ text = convert_content_list_to_str(+ cast(ChatCompletionAssistantMessage, DEFAULT_ASSISTANT_CONTINUE_MESSAGE)+ )messages.append(BedrockMessageBlock(role="assistant",- content=[BedrockContentBlock(text=assistant_continue_message)],+ content=[+ BedrockContentBlock(text=text),+ ],))- elif litellm.modify_params:- messages.append(BedrockMessageBlock(**DEFAULT_ASSISTANT_CONTINUE_MESSAGE)) # type: ignorereturn messages+def get_user_message_block_or_continue_message(+ message: ChatCompletionUserMessage,+ user_continue_message: Optional[ChatCompletionUserMessage] = None,+) -> ChatCompletionUserMessage:+ """+ Returns the user content block+ if content block is an empty string, then return the default continue message++ Relevant Issue: https://github.com/BerriAI/litellm/issues/7169+ """+ content_block = message.get("content", None)++ # Handle None case+ if content_block is None or (+ user_continue_message is None and litellm.modify_params is False+ ):+ return message++ # Handle string case+ if isinstance(content_block, str):+ # check if content is empty+ if content_block.strip():+ return message+ else:+ return ChatCompletionUserMessage(+ **(user_continue_message or DEFAULT_USER_CONTINUE_MESSAGE) # type: ignore+ )++ # Handle list case+ if isinstance(content_block, list):+ """+ CHECK FOR+ "content": [+ {+ "type": "text",+ "text": ""+ }+ ],+ """+ if not content_block:+ return ChatCompletionUserMessage(+ **(user_continue_message or DEFAULT_USER_CONTINUE_MESSAGE) # type: ignore+ )+ # Create a copy of the message to avoid modifying the original+ modified_content_block = content_block.copy()++ for item in modified_content_block:+ # Check if the list is empty+ if item["type"] == "text":++ if not item["text"].strip():+ # Replace empty text with continue message+ _user_continue_message = ChatCompletionUserMessage(+ **(user_continue_message or DEFAULT_USER_CONTINUE_MESSAGE) # type: ignore+ )+ text = convert_content_list_to_str(_user_continue_message)+ item["text"] = text+ break+ modified_message = message.copy()+ modified_message["content"] = modified_content_block+ return modified_message++ # Handle unsupported type+ raise ValueError(f"Unsupported content type: {type(content_block)}")+++def return_assistant_continue_message(+ assistant_continue_message: Optional[+ Union[str, ChatCompletionAssistantMessage]+ ] = None+) -> ChatCompletionAssistantMessage:+ if assistant_continue_message and isinstance(assistant_continue_message, str):+ return ChatCompletionAssistantMessage(+ role="assistant",+ content=assistant_continue_message,+ )+ elif assistant_continue_message and isinstance(assistant_continue_message, dict):+ return ChatCompletionAssistantMessage(**assistant_continue_message)+ else:+ return DEFAULT_ASSISTANT_CONTINUE_MESSAGE+++def skip_empty_text_blocks(+ message: ChatCompletionAssistantMessage,+) -> ChatCompletionAssistantMessage:+ """+ Skips empty text blocks in message content text blocks.++ Do not insert content here. This is a helper function, which can also be used in base case.+ """+ content_block = message.get("content", None)+ if content_block is None:+ return message+ if (+ isinstance(content_block, str)+ and not content_block.strip()+ and is_non_content_values_set(message)+ ):+ modified_message = message.copy()+ modified_message["content"] = None+ return modified_message+ elif isinstance(content_block, list):+ modified_content_block = [+ item+ for item in content_block+ if not (item["type"] == "text" and not item["text"].strip())+ ]+ modified_message = message.copy()+ modified_message["content"] = modified_content_block+ return modified_message++ return message+++def process_empty_text_blocks(+ message: ChatCompletionAssistantMessage,+ assistant_continue_message: Optional[+ Union[str, ChatCompletionAssistantMessage]+ ] = None,+) -> ChatCompletionAssistantMessage:+ modified_content_block = message.get("content", None)+ ## BASE CASE ##+ if modified_content_block is None or not isinstance(modified_content_block, list):+ return message++ # Check if all items are empty text blocks+ if all(+ item["type"] == "text" and not item["text"].strip()+ for item in modified_content_block+ ):+ # Replace with a single continue message+ _assistant_continue_message = return_assistant_continue_message(+ assistant_continue_message+ )+ modified_content_block = [+ {+ "type": "text",+ "text": convert_content_list_to_str(_assistant_continue_message),+ }+ ]+ else:+ # Filter out only empty text blocks, keeping non-empty text and other block types+ modified_content_block = [+ item+ for item in modified_content_block+ if not (item["type"] == "text" and not item["text"].strip())+ ]++ modified_message = message.copy()+ modified_message["content"] = modified_content_block+ return modified_message+++def get_assistant_message_block_or_continue_message(+ message: ChatCompletionAssistantMessage,+ assistant_continue_message: Optional[+ Union[str, ChatCompletionAssistantMessage]+ ] = None,+) -> ChatCompletionAssistantMessage:+ """+ Returns the user content block+ if content block is an empty string, then return the default continue message++ Relevant Issue: https://github.com/BerriAI/litellm/issues/7169+ """+ content_block = message.get("content", None)++ # Handle Base case+ if content_block is None or (+ assistant_continue_message is None and litellm.modify_params is False+ ):+ return skip_empty_text_blocks(message=message)++ # Handle string case+ if isinstance(content_block, str):+ # check if content is empty+ if content_block.strip():+ return message+ else:+ if is_non_content_values_set(message):+ modified_message = message.copy()+ modified_message["content"] = None+ return modified_message+ return return_assistant_continue_message(assistant_continue_message)++ # Handle list case+ if isinstance(content_block, list):+ """+ CHECK FOR+ "content": [+ {+ "type": "text",+ "text": ""+ }+ ],+ """+ return process_empty_text_blocks(+ message=message, assistant_continue_message=assistant_continue_message+ )++ # Handle unsupported type+ raise ValueError(f"Unsupported content type: {type(content_block)}")++def _bedrock_converse_messages_pt( # noqa: PLR0915messages: List,model: str,llm_provider: str,- user_continue_message: Optional[dict] = None,- assistant_continue_message: Optional[str] = None,+ user_continue_message: Optional[ChatCompletionUserMessage] = None,+ assistant_continue_message: Optional[+ Union[str, ChatCompletionAssistantMessage]+ ] = None,) -> List[BedrockMessageBlock]:"""Converts given messages from OpenAI format to Bedrock format@@ -2469,9 +2699,13 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915init_msg_i = msg_i## MERGE CONSECUTIVE USER CONTENT ##while msg_i < len(messages) and messages[msg_i]["role"] == "user":- if isinstance(messages[msg_i]["content"], list):+ message_block = get_user_message_block_or_continue_message(+ message=messages[msg_i],+ user_continue_message=user_continue_message,+ )+ if isinstance(message_block["content"], list):_parts: List[BedrockContentBlock] = []- for element in messages[msg_i]["content"]:+ for element in message_block["content"]:if isinstance(element, dict):if element["type"] == "text":_part = BedrockContentBlock(text=element["text"])@@ -2487,17 +2721,20 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915_parts.append(_part) # type: ignore_cache_point_block = (litellm.AmazonConverseConfig()._get_cache_point_block(- element, block_type="content_block"+ message_block=cast(+ OpenAIMessageContentListBlock, element+ ),+ block_type="content_block",))if _cache_point_block is not None:_parts.append(_cache_point_block)user_content.extend(_parts)- else:+ elif message_block["content"] and isinstance(message_block["content"], str):_part = BedrockContentBlock(text=messages[msg_i]["content"])_cache_point_block = (litellm.AmazonConverseConfig()._get_cache_point_block(- messages[msg_i], block_type="content_block"+ message_block, block_type="content_block"))user_content.append(_part)@@ -2559,11 +2796,15 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915assistant_content: List[BedrockContentBlock] = []## MERGE CONSECUTIVE ASSISTANT CONTENT ##while msg_i < len(messages) and messages[msg_i]["role"] == "assistant":- if messages[msg_i].get("content", None) is not None and isinstance(- messages[msg_i]["content"], list- ):+ assistant_message_block = get_assistant_message_block_or_continue_message(+ message=messages[msg_i],+ assistant_continue_message=assistant_continue_message,+ )+ _assistant_content = assistant_message_block.get("content", None)++ if _assistant_content is not None and isinstance(_assistant_content, list):assistants_parts: List[BedrockContentBlock] = []- for element in messages[msg_i]["content"]:+ for element in _assistant_content:if isinstance(element, dict):if element["type"] == "text":assistants_part = BedrockContentBlock(text=element["text"])@@ -2578,19 +2819,12 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915)assistants_parts.append(assistants_part)assistant_content.extend(assistants_parts)- elif messages[msg_i].get("content", None) is not None and isinstance(- messages[msg_i]["content"], str- ):- assistant_text = (- messages[msg_i].get("content") or ""- ) # either string or none- if assistant_text:- assistant_content.append(BedrockContentBlock(text=assistant_text))- if messages[msg_i].get(- "tool_calls", []- ): # support assistant tool invoke convertion [TODO]:+ elif _assistant_content is not None and isinstance(_assistant_content, str):+ assistant_content.append(BedrockContentBlock(text=_assistant_content))+ _tool_calls = assistant_message_block.get("tool_calls", [])+ if _tool_calls:assistant_content.extend(- _convert_to_bedrock_tool_call_invoke(messages[msg_i]["tool_calls"])+ _convert_to_bedrock_tool_call_invoke(_tool_calls))msg_i += 1diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.pyindex 34c8b43657d..b1f6ef3da94 100644--- a/litellm/llms/bedrock/chat/converse_transformation.py+++ b/litellm/llms/bedrock/chat/converse_transformation.py@@ -12,19 +12,25 @@ import httpximport litellmfrom litellm.litellm_core_utils.core_helpers import map_finish_reasonfrom litellm.litellm_core_utils.litellm_logging import Logging+from litellm.litellm_core_utils.prompt_templates.factory import (+ _bedrock_converse_messages_pt,+ _bedrock_tools_pt,+)from litellm.types.llms.bedrock import *from litellm.types.llms.openai import (AllMessageValues,ChatCompletionResponseMessage,+ ChatCompletionSystemMessage,ChatCompletionToolCallChunk,ChatCompletionToolCallFunctionChunk,ChatCompletionToolParam,ChatCompletionToolParamFunctionChunk,+ ChatCompletionUserMessage,+ OpenAIMessageContentListBlock,)from litellm.types.utils import ModelResponse, Usagefrom litellm.utils import CustomStreamWrapper, add_dummy_tool, has_tool_call_blocks-from litellm.litellm_core_utils.prompt_templates.factory import _bedrock_converse_messages_pt, _bedrock_tools_ptfrom ..common_utils import BedrockError, get_bedrock_tool_name@@ -38,6 +44,7 @@ class AmazonConverseConfig:stopSequences: Optional[List[str]]temperature: Optional[int]topP: Optional[int]+ topK: Optional[int]def __init__(self,@@ -45,6 +52,7 @@ class AmazonConverseConfig:stopSequences: Optional[List[str]] = None,temperature: Optional[int] = None,topP: Optional[int] = None,+ topK: Optional[int] = None,) -> None:locals_ = locals()for key, value in locals_.items():@@ -258,18 +266,36 @@ class AmazonConverseConfig:@overloaddef _get_cache_point_block(- self, message_block: dict, block_type: Literal["system"]+ self,+ message_block: Union[+ OpenAIMessageContentListBlock,+ ChatCompletionUserMessage,+ ChatCompletionSystemMessage,+ ],+ block_type: Literal["system"],) -> Optional[SystemContentBlock]:pass@overloaddef _get_cache_point_block(- self, message_block: dict, block_type: Literal["content_block"]+ self,+ message_block: Union[+ OpenAIMessageContentListBlock,+ ChatCompletionUserMessage,+ ChatCompletionSystemMessage,+ ],+ block_type: Literal["content_block"],) -> Optional[ContentBlock]:passdef _get_cache_point_block(- self, message_block: dict, block_type: Literal["system", "content_block"]+ self,+ message_block: Union[+ OpenAIMessageContentListBlock,+ ChatCompletionUserMessage,+ ChatCompletionSystemMessage,+ ],+ block_type: Literal["system", "content_block"],) -> Optional[Union[SystemContentBlock, ContentBlock]]:if message_block.get("cache_control", None) is None:return None@@ -290,7 +316,7 @@ class AmazonConverseConfig:if isinstance(message["content"], str) and len(message["content"]) > 0:_system_content_block = SystemContentBlock(text=message["content"])_cache_point_block = self._get_cache_point_block(- cast(dict, message), block_type="system"+ message, block_type="system")elif isinstance(message["content"], list):for m in message["content"]:@@ -309,6 +335,11 @@ class AmazonConverseConfig:messages.pop(idx)return messages, system_content_blocks+ def _transform_inference_params(self, inference_params: dict) -> InferenceConfig:+ if "top_k" in inference_params:+ inference_params["topK"] = inference_params.pop("top_k")+ return InferenceConfig(**inference_params)+def _transform_request(self,model: str,@@ -320,7 +351,9 @@ class AmazonConverseConfig:inference_params = copy.deepcopy(optional_params)additional_request_keys = []additional_request_params = {}- supported_converse_params = AmazonConverseConfig.__annotations__.keys()+ supported_converse_params = list(+ AmazonConverseConfig.__annotations__.keys()+ ) + ["top_k"]supported_tool_call_params = ["tools", "tool_choice"]supported_guardrail_params = ["guardrailConfig"]inference_params.pop("json_mode", None) # used for handling json_schema@@ -363,7 +396,9 @@ class AmazonConverseConfig:"messages": bedrock_messages,"additionalModelRequestFields": additional_request_params,"system": system_content_blocks,- "inferenceConfig": InferenceConfig(**inference_params),+ "inferenceConfig": self._transform_inference_params(+ inference_params=inference_params+ ),}# Guardrail Configdiff --git a/litellm/router.py b/litellm/router.pyindex d54e63e8abf..fab6fe355b4 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -79,6 +79,7 @@ from litellm.router_utils.cooldown_handlers import (_set_cooldown_deployments,)from litellm.router_utils.fallback_event_handlers import (+ get_fallback_model_group,log_failure_fallback_event,log_success_fallback_event,run_async_fallback,@@ -2753,19 +2754,14 @@ class Router:)e.message += "\n{}".format(error_message)- if fallbacks is not None:+ if fallbacks is not None and model_group is not None:verbose_router_logger.debug(f"inside model fallbacks: {fallbacks}")- generic_fallback_idx: Optional[int] = None- ## check for specific model group-specific fallbacks- for idx, item in enumerate(fallbacks):- if isinstance(item, dict):- if list(item.keys())[0] == model_group:- fallback_model_group = item[model_group]- break- elif list(item.keys())[0] == "*":- generic_fallback_idx = idx- elif isinstance(item, str):- fallback_model_group = [fallbacks.pop(idx)]+ fallback_model_group, generic_fallback_idx = (+ get_fallback_model_group(+ fallbacks=fallbacks,+ model_group=cast(str, model_group),+ )+ )## if none, check for generic fallbackif (fallback_model_group is Nonediff --git a/litellm/router_utils/fallback_event_handlers.py b/litellm/router_utils/fallback_event_handlers.pyindex 41c3080e9a0..2845ec4769b 100644--- a/litellm/router_utils/fallback_event_handlers.py+++ b/litellm/router_utils/fallback_event_handlers.py@@ -1,6 +1,8 @@+from enum import Enumfrom typing import TYPE_CHECKING, Any, Dict, List, Optional, Tupleimport litellm+from litellm import LlmProvidersfrom litellm._logging import verbose_router_loggerfrom litellm.integrations.custom_logger import CustomLoggerfrom litellm.main import verbose_logger@@ -13,6 +15,71 @@ else:LitellmRouter = Any+def _check_stripped_model_group(model_group: str, fallback_key: str) -> bool:+ """+ Handles wildcard routing scenario++ where fallbacks set like:+ [{"gpt-3.5-turbo": ["claude-3-haiku"]}]++ but model_group is like:+ "openai/gpt-3.5-turbo"++ Returns:+ - True if the stripped model group == fallback_key+ """+ for provider in litellm.provider_list:+ if isinstance(provider, Enum):+ _provider = provider.value+ else:+ _provider = provider+ if model_group.startswith(f"{_provider}/"):+ stripped_model_group = model_group.replace(f"{_provider}/", "")+ if stripped_model_group == fallback_key:+ return True+ return False+++def get_fallback_model_group(+ fallbacks: List[Any], model_group: str+) -> Tuple[Optional[List[str]], Optional[int]]:+ """+ Returns:+ - fallback_model_group: List[str] of fallback model groups. example: ["gpt-4", "gpt-3.5-turbo"]+ - generic_fallback_idx: int of the index of the generic fallback in the fallbacks list.++ Checks:+ - exact match+ - stripped model group match+ - generic fallback+ """+ generic_fallback_idx: Optional[int] = None+ stripped_model_fallback: Optional[List[str]] = None+ fallback_model_group: Optional[List[str]] = None+ ## check for specific model group-specific fallbacks+ for idx, item in enumerate(fallbacks):+ if isinstance(item, dict):+ if list(item.keys())[0] == model_group: # check exact match+ fallback_model_group = item[model_group]+ break+ elif _check_stripped_model_group(+ model_group=model_group, fallback_key=list(item.keys())[0]+ ): # check generic fallback+ stripped_model_fallback = item[list(item.keys())[0]]+ elif list(item.keys())[0] == "*": # check generic fallback+ generic_fallback_idx = idx+ elif isinstance(item, str):+ fallback_model_group = [fallbacks.pop(idx)]+ ## if none, check for generic fallback+ if fallback_model_group is None:+ if stripped_model_fallback is not None:+ fallback_model_group = stripped_model_fallback+ elif generic_fallback_idx is not None:+ fallback_model_group = fallbacks[generic_fallback_idx]["*"]++ return fallback_model_group, generic_fallback_idx++async def run_async_fallback(*args: Tuple[Any],litellm_router: LitellmRouter,diff --git a/litellm/types/llms/bedrock.py b/litellm/types/llms/bedrock.pyindex e94ffd80a3b..8d43243dcb2 100644--- a/litellm/types/llms/bedrock.py+++ b/litellm/types/llms/bedrock.py@@ -136,6 +136,7 @@ class InferenceConfig(TypedDict, total=False):stopSequences: List[str]temperature: floattopP: float+ topK: intclass ToolBlockDeltaEvent(TypedDict):diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.pyindex eb8f308ef08..26c0eab3a09 100644--- a/litellm/types/llms/openai.py+++ b/litellm/types/llms/openai.py@@ -374,15 +374,15 @@ class ChatCompletionAudioObject(ChatCompletionContentPartInputAudioParam):pass+OpenAIMessageContentListBlock = Union[+ ChatCompletionTextObject,+ ChatCompletionImageObject,+ ChatCompletionAudioObject,+]+OpenAIMessageContent = Union[str,- Iterable[- Union[- ChatCompletionTextObject,- ChatCompletionImageObject,- ChatCompletionAudioObject,- ]- ],+ Iterable[OpenAIMessageContentListBlock],]# The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.diff --git a/tests/llm_translation/test_bedrock_completion.py b/tests/llm_translation/test_bedrock_completion.pyindex 651424b313f..6a59d813b90 100644--- a/tests/llm_translation/test_bedrock_completion.py+++ b/tests/llm_translation/test_bedrock_completion.py@@ -1262,7 +1262,9 @@ def test_bedrock_get_base_model(model, expected_base_model):assert litellm.AmazonConverseConfig()._get_base_model(model) == expected_base_model-from litellm.litellm_core_utils.prompt_templates.factory import _bedrock_converse_messages_pt+from litellm.litellm_core_utils.prompt_templates.factory import (+ _bedrock_converse_messages_pt,+)def test_bedrock_converse_translation_tool_message():@@ -1603,7 +1605,9 @@ def test_bedrock_completion_test_3():Check if content in tool result is formatted correctly"""from litellm.types.utils import ChatCompletionMessageToolCall, Function, Message- from litellm.litellm_core_utils.prompt_templates.factory import _bedrock_converse_messages_pt+ from litellm.litellm_core_utils.prompt_templates.factory import (+ _bedrock_converse_messages_pt,+ )messages = [{@@ -1625,7 +1629,7 @@ def test_bedrock_completion_test_3():)],function_call=None,- ),+ ).model_dump(),{"tool_call_id": "tooluse_EF8PwJ1dSMSh6tLGKu9VdA","role": "tool",@@ -2106,3 +2110,122 @@ class TestBedrockRerank(BaseLLMRerankTest):return {"model": "bedrock/arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0",}+++ "messages, continue_message_index",+ [+ (+ [+ {"role": "user", "content": [{"type": "text", "text": ""}]},+ {"role": "assistant", "content": [{"type": "text", "text": "Hello!"}]},+ ],+ 0,+ ),+ (+ [+ {"role": "user", "content": [{"type": "text", "text": "Hello!"}]},+ {"role": "assistant", "content": [{"type": "text", "text": " "}]},+ ],+ 1,+ ),+ ],+)+def test_bedrock_empty_content_handling(messages, continue_message_index):+ """+ Test that empty content in messages is handled correctly with default messages+ """+ # Test with default behavior (modify_params=True)+ litellm.modify_params = True+ formatted_messages = _bedrock_converse_messages_pt(+ messages=messages,+ model="anthropic.claude-3-sonnet-20240229-v1:0",+ llm_provider="bedrock",+ )+ print(formatted_messages)+ # Verify assistant message with default text was inserted+ assert formatted_messages[0]["role"] == "user"+ assert formatted_messages[1]["role"] == "assistant"+ assert (+ formatted_messages[continue_message_index]["content"][0]["text"]+ == "Please continue."+ )+++def test_bedrock_custom_continue_message():+ """+ Test that custom continue messages are used when provided+ """+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "Hello!"}]},+ {"role": "assistant", "content": [{"type": "text", "text": " "}]},+ ]++ custom_continue = {+ "role": "assistant",+ "content": [{"text": "Custom continue message", "type": "text"}],+ }++ formatted_messages = _bedrock_converse_messages_pt(+ messages=messages,+ model="anthropic.claude-3-sonnet-20240229-v1:0",+ llm_provider="bedrock",+ assistant_continue_message=custom_continue,+ )++ # Verify custom message was used+ assert formatted_messages[1]["role"] == "assistant"+ assert formatted_messages[1]["content"][0]["text"] == "Custom continue message"+++def test_bedrock_no_default_message():+ """+ Test that empty content is handled correctly when modify_params=False+ """+ messages = [+ {"role": "user", "content": "Hello!"},+ {"role": "assistant", "content": ""},+ {"role": "user", "content": "Hi again"},+ {"role": "assistant", "content": "Valid response"},+ ]++ litellm.modify_params = False+ formatted_messages = _bedrock_converse_messages_pt(+ messages=messages,+ model="anthropic.claude-3-sonnet-20240229-v1:0",+ llm_provider="bedrock",+ )++ # Verify empty message is present and valid message remains+ assistant_messages = [+ msg for msg in formatted_messages if msg["role"] == "assistant"+ ]+ assert len(assistant_messages) == 2 # Both empty and valid messages present+ assert assistant_messages[0]["content"][0]["text"] == "" # First message is empty+ assert (+ assistant_messages[1]["content"][0]["text"] == "Valid response"+ ) # Second message is valid++[email protected]("top_k_param", ["top_k", "topK"])+def test_bedrock_nova_topk(top_k_param):+ litellm.set_verbose = True+ data = {+ "model": "bedrock/us.amazon.nova-pro-v1:0",+ "messages": [{"role": "user", "content": "Hello, world!"}],+ top_k_param: 10,+ }+ litellm.completion(**data)+++def test_bedrock_process_empty_text_blocks():+ from litellm.litellm_core_utils.prompt_templates.factory import (+ process_empty_text_blocks,+ )++ message = {+ "message": {"role": "assistant", "content": [{"type": "text", "text": " "}]},+ "assistant_continue_message": None,+ }+ modified_message = process_empty_text_blocks(**message)+ assert modified_message["content"][0]["text"] == "Please continue."diff --git a/tests/local_testing/test_router_fallbacks.py b/tests/local_testing/test_router_fallbacks.pyindex e555b51d726..41b2b9c9cd5 100644--- a/tests/local_testing/test_router_fallbacks.py+++ b/tests/local_testing/test_router_fallbacks.py@@ -1521,3 +1521,49 @@ def test_router_fallbacks_with_model_id():messages=[{"role": "user", "content": "hi"}],mock_testing_fallbacks=True,)+++def test_router_fallbacks_with_wildcard_model_name():+ router = Router(+ model_list=[+ {+ "model_name": "openai/*",+ "litellm_params": {+ "model": "openai/*",+ "api_key": os.getenv("OPENAI_API_KEY"),+ },+ },+ {+ "model_name": "claude-3-haiku",+ "litellm_params": {+ "model": "claude-3-haiku-20240307",+ "api_key": os.getenv("ANTHROPIC_API_KEY"),+ "mock_response": "Hi this is claude!",+ },+ },+ ],+ fallbacks=[{"gpt-3.5-turbo": ["claude-3-haiku"]}],+ )++ response = router.completion(+ model="openai/gpt-3.5-turbo",+ messages=[{"role": "user", "content": "Hey, how's it going?"}],+ mock_testing_fallbacks=True,+ )++ print(response)+ assert response["choices"][0]["message"]["content"] == "Hi this is claude!"+++def test_get_fallback_model_group():+ from litellm.router_utils.fallback_event_handlers import get_fallback_model_group++ args = {+ "fallbacks": [+ {"gpt-3.5-turbo": ["claude-3-haiku"]},+ {"*": ["claude-3-sonnet"]},+ ],+ "model_group": "openai/gpt-3.5-turbo",+ }+ fallback_model_group, _ = get_fallback_model_group(**args)+ assert fallback_model_group == ["claude-3-haiku"]