From 31e25a596577615c998342950282186c3bfb149d Mon Sep 17 00:00:00 2001 From: Makonike Date: Wed, 9 Jul 2025 15:39:15 +0800 Subject: [PATCH] refactor(ai_handler): improve streaming response handling robustness --- pr_agent/algo/ai_handlers/litellm_ai_handler.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pr_agent/algo/ai_handlers/litellm_ai_handler.py b/pr_agent/algo/ai_handlers/litellm_ai_handler.py index 787575c9..32d1420c 100644 --- a/pr_agent/algo/ai_handlers/litellm_ai_handler.py +++ b/pr_agent/algo/ai_handlers/litellm_ai_handler.py @@ -448,11 +448,13 @@ class LiteLLMAIHandler(BaseAiHandler): try: async for chunk in response: if chunk.choices and len(chunk.choices) > 0: - delta = chunk.choices[0].delta - if hasattr(delta, 'content') and delta.content: - full_response += delta.content - if chunk.choices[0].finish_reason: - finish_reason = chunk.choices[0].finish_reason + choice = chunk.choices[0] + delta = choice.delta + content = getattr(delta, 'content', None) + if content: + full_response += content + if choice.finish_reason: + finish_reason = choice.finish_reason except Exception as e: get_logger().error(f"Error handling streaming response: {e}") raise