1
0
Эх сурвалжийг харах

fix(chapter4): skip empty choices chunks in streaming response

Some OpenAI-compatible APIs (e.g. AIHubMix) append a final tail packet
with choices=[] containing only usage statistics. Accessing chunk.choices[0]
on this packet raises IndexError, which is caught by the outer try/except
and returns None — silently discarding all previously streamed content.

Adding `if not chunk.choices: continue` skips these packets while
preserving the full response.

Fixes #522
Jijie Zhou 1 сар өмнө
parent
commit
ece18a49fa

+ 2 - 0
code/chapter4/llm_client.py

@@ -42,6 +42,8 @@ class HelloAgentsLLM:
             print("✅ 大语言模型响应成功:")
             collected_content = []
             for chunk in response:
+                if not chunk.choices:
+                    continue
                 content = chunk.choices[0].delta.content or ""
                 print(content, end="", flush=True)
                 collected_content.append(content)