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'
This commit is contained in:
0xnxxh 2026-02-09 03:24:17 +00:00
parent aaa48f8ad2
commit 4a239efc77

View file

@ -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 (