diff --git a/litellm/litellm_core_utils/llm_response_utils/get_formatted_prompt.py b/litellm/litellm_core_utils/llm_response_utils/get_formatted_prompt.py index 2c1befb7ac3..2db92812803 100644 --- a/litellm/litellm_core_utils/llm_response_utils/get_formatted_prompt.py +++ b/litellm/litellm_core_utils/llm_response_utils/get_formatted_prompt.py @@ -7,10 +7,17 @@ def get_formatted_prompt( "acompletion", "completion", "embedding", + "aembedding", + "embeddings", "image_generation", + "aimage_generation", "audio_transcription", + "transcription", + "atranscription", "moderation", + "amoderation", "text_completion", + "atext_completion", ], ) -> str: """ @@ -19,7 +26,7 @@ def get_formatted_prompt( Returns a string. """ prompt = "" - if call_type == "acompletion" or call_type == "completion": + if call_type in {"acompletion", "completion"}: for message in data["messages"]: if message.get("content", None) is not None: content = message.get("content") @@ -34,17 +41,20 @@ def get_formatted_prompt( if "function" in tool_call: function_arguments = tool_call["function"]["arguments"] prompt += function_arguments - elif call_type == "text_completion": + elif call_type in {"text_completion", "atext_completion"}: prompt = data["prompt"] - elif call_type == "embedding" or call_type == "moderation": - if isinstance(data["input"], str): - prompt = data["input"] - elif isinstance(data["input"], list): - for m in data["input"]: - prompt += m - elif call_type == "image_generation": + elif call_type in {"embedding", "aembedding", "embeddings", "moderation", "amoderation"}: + if "input" in data: + if isinstance(data["input"], str): + prompt = data["input"] + elif isinstance(data["input"], list): + for m in data["input"]: + if isinstance(m, str): + prompt += m + elif ( + call_type in {"image_generation", "aimage_generation"} + or (call_type in {"audio_transcription", "transcription", "atranscription"}) + and "prompt" in data + ): prompt = data["prompt"] - elif call_type == "audio_transcription": - if "prompt" in data: - prompt = data["prompt"] return prompt diff --git a/tests/unit/litellm_core_utils/llm_response_utils/test_get_formatted_prompt.py b/tests/unit/litellm_core_utils/llm_response_utils/test_get_formatted_prompt.py index 64dd79bb918..02b89d15236 100644 --- a/tests/unit/litellm_core_utils/llm_response_utils/test_get_formatted_prompt.py +++ b/tests/unit/litellm_core_utils/llm_response_utils/test_get_formatted_prompt.py @@ -6,6 +6,18 @@ from litellm.litellm_core_utils.llm_response_utils.get_formatted_prompt import ( get_formatted_prompt, ) +CallType = Literal[ + "transcription", + "atranscription", + "audio_transcription", + "aembedding", + "embedding", + "embeddings", + "amoderation", + "aimage_generation", + "atext_completion", +] + @pytest.mark.parametrize("call_type", ["acompletion", "completion"]) def test_null_tool_calls_are_skipped(call_type: Literal["acompletion", "completion"]) -> None: @@ -22,3 +34,22 @@ def test_null_tool_calls_are_skipped(call_type: Literal["acompletion", "completi } assert get_formatted_prompt(data=data, call_type=call_type) == 'pingpong{"x":1}' + + +@pytest.mark.parametrize( + ("data", "call_type", "expected"), + [ + ({"prompt": "transcribe speech"}, "transcription", "transcribe speech"), + ({"prompt": "async speech"}, "atranscription", "async speech"), + ({"prompt": "audio legacy"}, "audio_transcription", "audio legacy"), + ({"input": "embed async"}, "aembedding", "embed async"), + ({"input": ["embed", "dings"]}, "embeddings", "embeddings"), + ({"input": [0.1, 0.2]}, "aembedding", ""), + ({"input": [101, 2054]}, "embedding", ""), + ({"input": "mod async"}, "amoderation", "mod async"), + ({"prompt": "gen image async"}, "aimage_generation", "gen image async"), + ({"prompt": "complete async"}, "atext_completion", "complete async"), + ], +) +def test_get_formatted_prompt_call_types(data: dict[str, object], call_type: CallType, expected: str) -> None: + assert get_formatted_prompt(data=data, call_type=call_type) == expected