Browse Source

fix: guard against empty/filtered LLM responses in chat_api.py

Add null check before accessing choices[0].message.content in the
non-streaming path. An empty choices list causes IndexError; a None
message (returned by some providers on content-filtered responses
with HTTP 200) causes AttributeError.
Jonathan Hill 6 tháng trước cách đây
mục cha
commit
182bff1bd8
1 tập tin đã thay đổi với 2 bổ sung và 0 xóa
  1. 2 0
      scripts/chat_api.py

+ 2 - 0
scripts/chat_api.py

@@ -21,6 +21,8 @@ while True:
         extra_body={"chat_template_kwargs": {"open_thinking": True}, "reasoning_effort": "medium"} # 思考开关
     )
     if not stream:
+        if not response.choices or response.choices[0].message is None:
+            raise ValueError("LLM returned empty or filtered response")
         assistant_res = response.choices[0].message.content
         print('[A]: ', assistant_res)
     else: