fix: prevent list index out of range in chat streaming (#10238)

### What problem does this PR solve?
issue:
[Bug]: ERROR: list index out of range #10188
change:
fix a potential list index out of range error in chat response parsing
by adding explicit checks for empty choices.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
buua436
2025-09-23 19:59:39 +08:00
committed by GitHub
parent 65a06d62d8
commit 38be53cf31

View File

@ -146,7 +146,7 @@ class Base(ABC):
response = self.client.chat.completions.create(model=self.model_name, messages=history, **gen_conf, **kwargs) response = self.client.chat.completions.create(model=self.model_name, messages=history, **gen_conf, **kwargs)
if any([not response.choices, not response.choices[0].message, not response.choices[0].message.content]): if (not response.choices or not response.choices[0].message or not response.choices[0].message.content):
return "", 0 return "", 0
ans = response.choices[0].message.content.strip() ans = response.choices[0].message.content.strip()
if response.choices[0].finish_reason == "length": if response.choices[0].finish_reason == "length":