fix(openai.py): drop invalid params if drop_params: true for azure ai

Fixes https://github.com/BerriAI/litellm/issues/4800
This commit is contained in:
Krrish Dholakia 2024-07-20 15:08:15 -07:00
parent 576cccaade
commit 86c9e05c10
2 changed files with 78 additions and 43 deletions

View file

@ -984,6 +984,9 @@ class OpenAIChatCompletion(BaseLLM):
headers=None,
):
response = None
for _ in range(
2
): # if call fails due to alternating messages, retry with reformatted message
try:
openai_aclient = self._get_openai_client(
is_async=True,
@ -1000,7 +1003,9 @@ class OpenAIChatCompletion(BaseLLM):
input=data["messages"],
api_key=openai_aclient.api_key,
additional_args={
"headers": {"Authorization": f"Bearer {openai_aclient.api_key}"},
"headers": {
"Authorization": f"Bearer {openai_aclient.api_key}"
},
"api_base": openai_aclient._base_url._uri_reference,
"acompletion": True,
"complete_input_dict": data,
@ -1023,6 +1028,33 @@ class OpenAIChatCompletion(BaseLLM):
model_response_object=model_response,
hidden_params={"headers": headers},
)
except openai.UnprocessableEntityError as e:
## check if body contains unprocessable params - related issue https://github.com/BerriAI/litellm/issues/4800
if litellm.drop_params is True:
if e.body is not None and e.body.get("detail"): # type: ignore
detail = e.body.get("detail") # type: ignore
invalid_params: List[str] = []
if (
isinstance(detail, List)
and len(detail) > 0
and isinstance(detail[0], dict)
):
for error_dict in detail:
if (
error_dict.get("loc")
and isinstance(error_dict.get("loc"), list)
and len(error_dict.get("loc")) == 2
):
invalid_params.append(error_dict["loc"][1])
new_data = {}
for k, v in data.items():
if k not in invalid_params:
new_data[k] = v
data = new_data
else:
raise e
# e.message
except Exception as e:
raise e

View file

@ -1,6 +1,9 @@
model_list:
- model_name: azure-chatgpt
- model_name: azure-mistral
litellm_params:
model: azure/chatgpt-v-2
api_key: os.environ/AZURE_API_KEY
api_base: os.environ/AZURE_API_BASE
model: azure_ai/mistral
api_key: os.environ/AZURE_AI_MISTRAL_API_KEY
api_base: os.environ/AZURE_AI_MISTRAL_API_BASE
litellm_settings:
drop_params: true