binary-husky/gpt_academic · error · RuntimeError
获得空的回复,可能原因:{finish_reason}
Error message
获得空的回复,可能原因:{finish_reason} What it means
In the streaming reader of predict_no_ui_long_connection (OpenAI-compatible template), when the SSE stream ends (StopIteration) and not a single content token was accumulated into result, it raises RuntimeError('获得空的回复,可能原因:{finish_reason}'). finish_reason is whatever the last decode_chunk produced (or '' if none arrived), so the message hints at whether the model stopped abnormally.
Source
Thrown at request_llms/oai_std_model_template.py:224
retry += 1
traceback.print_exc()
if retry > MAX_RETRY:
raise TimeoutError
if MAX_RETRY != 0:
logger.error(f"请求超时,正在重试 ({retry}/{MAX_RETRY}) ……")
result = ""
finish_reason = ""
if reasoning:
reasoning_buffer = ""
stream_response = response.iter_lines()
while True:
try:
chunk = next(stream_response)
except StopIteration:
if result == "":
raise RuntimeError(f"获得空的回复,可能原因:{finish_reason}")
break
except requests.exceptions.ConnectionError:
chunk = next(stream_response) # 失败了,重试一次?再失败就没办法了。
response_text, reasoning_content, finish_reason, decoded_chunk = decode_chunk(chunk)
# 返回的数据流第一次为空,继续等待
if response_text == "" and (reasoning == False or reasoning_content == "") and finish_reason != "False":
continue
if response_text == "API_ERROR" and (
finish_reason != "False" or finish_reason != "stop"
):
chunk = get_full_error(chunk, stream_response)
chunk_decoded = chunk.decode()
logger.error(chunk_decoded)
raise RuntimeError(
f"API异常,请检测终端输出。可能的原因是:{finish_reason}"
)
if chunk:
try:View on GitHub (pinned to d6bde0fa54)
Solutions
- Check terminal/log output — sibling branches log chunk_decoded for API errors; the raw stream explains the empty reply.
- Verify the API key and API base URL for the selected model in config.py.
- Shorten input/history: near-token-limit requests can yield empty completions.
- If using a reasoning model, enable the reasoning flag so reasoning_content chunks are not skipped.
- Retry once: transient upstream failures often produce a single empty response.
Defensive patterns
Strategy: retry
Try / catch
for attempt in range(2):
try:
return predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window)
except RuntimeError as e:
if '获得空的回复' in str(e) and attempt == 0:
continue # transient empty stream, retry once
raise Prevention
- Log finish_reason and the raw last chunk when a stream ends empty.
- Keep input+history comfortably under the model's context limit.
- Verify provider status/key with a minimal 1-token request before long jobs.
When it happens
Trigger: The HTTP stream completes without any usable delta: upstream returned only an error object, the model returned an empty completion, content filtering stopped it immediately, or every chunk was skipped by the 'empty text, no reasoning, no finish' continue branch.
Common situations: Invalid/quota-exhausted API key producing an error stream the loop skips; wrong API base URL returning HTML/empty body; context overflow so the model emits nothing; provider outage where the stream closes immediately; reasoning models whose content only appears in reasoning_content while reasoning is disabled.
Related errors
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/946fffb882971483.
Report an issue: GitHub.