binary-husky/gpt_academic · error · ConnectionAbortedError
OpenAI拒绝了请求:{error_msg}
Error message
OpenAI拒绝了请求:{error_msg} What it means
ConnectionAbortedError raised in bridge_openrouter.predict_no_ui_long_connection when a non-'data:' stream frame is received and the drained error text contains 'reduce the length' - the upstream's token-limit rejection. The bridge maps that substring to this exception so callers can treat it as context-overflow rather than a generic failure.
Source
Thrown at request_llms/bridge_openrouter.py:177
chunkjson = json.loads(response.content.decode())
gpt_replying_buffer = chunkjson['choices'][0]["message"]["content"]
return gpt_replying_buffer
stream_response = response.iter_lines()
result = ''
json_data = None
while True:
try: chunk = next(stream_response)
except StopIteration:
break
except requests.exceptions.ConnectionError:
chunk = next(stream_response) # 失败了,重试一次?再失败就没办法了。
chunk_decoded, chunkjson, has_choices, choice_valid, has_content, has_role = decode_chunk(chunk)
if len(chunk_decoded)==0 or chunk_decoded.startswith(':'): continue
if not chunk_decoded.startswith('data:'):
error_msg = get_full_error(chunk, stream_response).decode()
if "reduce the length" in error_msg:
raise ConnectionAbortedError("OpenAI拒绝了请求:" + error_msg)
elif """type":"upstream_error","param":"307""" in error_msg:
raise ConnectionAbortedError("正常结束,但显示Token不足,导致输出不完整,请削减单次输入的文本量。")
else:
raise RuntimeError("OpenAI拒绝了请求:" + error_msg)
if ('data: [DONE]' in chunk_decoded): break # api2d 正常完成
# 提前读取一些信息 (用于判断异常)
json_data = chunkjson['choices'][0]
delta = json_data["delta"]
if len(delta) == 0: break
if (not has_content) and has_role: continue
if (not has_content) and (not has_role): continue # raise RuntimeError("发现不标准的第三方接口:"+delta)
if has_content: # has_role = True/False
result += delta["content"]
if not console_silence: print(delta["content"], end='')
if observe_window is not None:
# 观测窗,把已经获取的数据显示出去
if len(observe_window) >= 1:
observe_window[0] += delta["content"]View on GitHub (pinned to d6bde0fa54)
Solutions
- Trim history or shorten inputs so total tokens fit the model's context window
- Switch llm_kwargs['llm_model'] to a model with a larger context window
- Use the project's history-compression / split-input plugin instead of one giant prompt
- Catch ConnectionAbortedError and show a 'reduce input' message to the user instead of retrying (retrying the same payload will fail identically)
Example fix
# before history += [long_doc, reply] # keeps growing # after MAX_TURNS = 8 history = history[-MAX_TURNS*2:] # bound the context
Defensive patterns
Strategy: try-catch
Validate before calling
# estimate token count before sending
approx_tokens = len(inputs) // 2 + sum(len(a) + len(b) for a, b in history) // 2
max_ctx = model_info[llm_kwargs['llm_model']].get('max_context', 8000)
assert approx_tokens < max_ctx * 0.9, f'input ~{approx_tokens} tokens exceeds safe budget {max_ctx}' Try / catch
try:
result = predict_no_ui_long_connection(inputs, llm_kwargs, history)
except ConnectionAbortedError as e:
if 'reduce the length' in str(e):
history = history[-6:] # shrink context and retry once
result = predict_no_ui_long_connection(inputs, llm_kwargs, history)
else:
raise Prevention
- Bound conversation history length before each request
- Split long documents into chunked summarization instead of one prompt
- Never blind-retry on this error - the identical payload will fail again
When it happens
Trigger: history plus inputs exceeding the model's context window so the API rejects the request; a long document plugin (e.g. paper reading) feeding too much text; a third-party OpenAI-compatible relay that forwards OpenAI's 'Please reduce the length of the messages' error verbatim.
Common situations: Summarizing very long PDFs; accumulated chat history growing past the model context; routing through OpenRouter to a model with a smaller window than expected.
Related errors
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/ea43b08560371faa.
Report an issue: GitHub.