From 4a239efc7782cc0c1614ccf885e3fe189ea70f3a Mon Sep 17 00:00:00 2001 From: 0xnxxh <0xnxxh@users.noreply.github.com> Date: Mon, 9 Feb 2026 03:24:17 +0000 Subject: [PATCH] fix: handle empty string finish_reason in API responses Some OpenAI-compatible API providers (e.g., packyapi) return an empty string "" for the finish_reason field instead of a valid value like "stop". This causes pydantic validation errors when litellm tries to parse the response. This fix changes the condition from `if finish_reason is None:` to `if not finish_reason:` to handle both None and empty string cases, defaulting to "stop" when finish_reason is falsy. Fixes the following error: pydantic_core._pydantic_core.ValidationError: 1 validation error for Choices finish_reason Input should be 'stop', 'content_filter', 'function_call', 'tool_calls', 'length', 'guardrail_intervened', 'eos', 'finish_reason_unspecified' or 'malformed_function_call' --- .../llm_response_utils/convert_dict_to_response.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py index 25ad0a570cb..6d6e21686bd 100644 --- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py +++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py @@ -588,7 +588,7 @@ def convert_to_model_response_object( # noqa: PLR0915 images=choice["message"].get("images", None), ) finish_reason = choice.get("finish_reason", None) - if finish_reason is None: + if not finish_reason: # Handle both None and empty string # gpt-4 vision can return 'finish_reason' or 'finish_details' finish_reason = choice.get("finish_details") or "stop" if (