From 704ef16ce3c58bd354a8633366405e09202463ce Mon Sep 17 00:00:00 2001 From: Enoughappens <155134541+Enoughappens@users.noreply.github.com> Date: Sat, 19 Apr 2025 12:57:08 +0800 Subject: [PATCH] fix streaming "list index out of range" --- lightrag/llm/openai.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lightrag/llm/openai.py b/lightrag/llm/openai.py index 8b05c61b..9fbec5e1 100644 --- a/lightrag/llm/openai.py +++ b/lightrag/llm/openai.py @@ -200,6 +200,15 @@ async def openai_complete_if_cache( async def inner(): try: async for chunk in response: + # Check if choices exists and is not empty + if not hasattr(chunk, 'choices') or not chunk.choices: + logger.warning(f"Received chunk without choices: {chunk}") + continue + + # Check if delta exists and has content + if not hasattr(chunk.choices[0], 'delta') or not hasattr(chunk.choices[0].delta, 'content'): + logger.warning(f"Received chunk without delta content: {chunk.choices[0]}") + continue content = chunk.choices[0].delta.content if content is None: continue