From 9290d63d209d3b71851eaa74edfa423f3b2fbc4f Mon Sep 17 00:00:00 2001 From: Oleksii Nikiforov Date: Sun, 13 Apr 2025 09:30:34 +0300 Subject: [PATCH] fix: handle empty choices fixes: https://github.com/epam/ai-dial-core/issues/789 --- aidial_assistant/model/model_client.py | 31 +++++++++++++------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/aidial_assistant/model/model_client.py b/aidial_assistant/model/model_client.py index 198d3f5..3df3fea 100644 --- a/aidial_assistant/model/model_client.py +++ b/aidial_assistant/model/model_client.py @@ -98,21 +98,22 @@ async def agenerate( else discarded_messages ) - choice = chunk.choices[0] - delta = choice.delta - if delta.content: - yield delta.content - - if delta.tool_calls: - tool_calls_chunks.append( - [ - tool_call_chunk.dict() - for tool_call_chunk in delta.tool_calls - ] - ) - - if choice.finish_reason == "length": - finish_reason_length = True + if len(chunk.choices) > 0: + choice = chunk.choices[0] + delta = choice.delta + if delta.content: + yield delta.content + + if delta.tool_calls: + tool_calls_chunks.append( + [ + tool_call_chunk.dict() + for tool_call_chunk in delta.tool_calls + ] + ) + + if choice.finish_reason == "length": + finish_reason_length = True if finish_reason_length: raise ReasonLengthException()