diff --git a/litellm/__init__.py b/litellm/__init__.py index 5c6b2400a..645a0bccd 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -757,6 +757,7 @@ openai_image_generation_models = ["dall-e-2", "dall-e-3"] from .timeout import timeout from .cost_calculator import completion_cost from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.litellm_core_utils.core_helpers import remove_index_from_tool_calls from litellm.litellm_core_utils.token_counter import get_modified_max_tokens from .utils import ( client, diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index d8d551048..d6d6495d0 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -39,3 +39,18 @@ def map_finish_reason( elif finish_reason == "content_filtered": return "content_filter" return finish_reason + + +def remove_index_from_tool_calls(messages, tool_calls): + for tool_call in tool_calls: + if "index" in tool_call: + tool_call.pop("index") + + for message in messages: + if "tool_calls" in message: + tool_calls = message["tool_calls"] + for tool_call in tool_calls: + if "index" in tool_call: + tool_call.pop("index") + + return diff --git a/litellm/llms/openai.py b/litellm/llms/openai.py index 0a2b6a3fa..0a40ab3fe 100644 --- a/litellm/llms/openai.py +++ b/litellm/llms/openai.py @@ -953,6 +953,12 @@ class OpenAIChatCompletion(BaseLLM): new_messages = messages new_messages.append({"role": "user", "content": ""}) messages = new_messages + elif ( + "unknown field: parameter index is not a valid field" in str(e) + ) and "tools" in data: + litellm.remove_index_from_tool_calls( + tool_calls=data["tools"], messages=messages + ) else: raise e except OpenAIError as e: