binary-husky/gpt_academic · error · ConnectionAbortedError

OpenAI拒绝了请求:

Error message

OpenAI拒绝了请求:

What it means

Raised as ConnectionAbortedError when the OpenAI-compatible stream returns a non-SSE frame and get_full_error recovers an error body containing 'reduce the length'. This is OpenAI's context-length rejection: the combined prompt (inputs + history + system prompt) exceeds the model's token limit, so the API refuses the request before generating. The bridge surfaces it as a distinct exception type so callers can react to overflow specifically.

Source

Thrown at request_llms/bridge_chatgpt.py:183

        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: 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 & one-api 正常完成
        # 提前读取一些信息 (用于判断异常)
        if has_choices and not choice_valid:
            # 一些垃圾第三方接口的出现这样的错误
            continue
        json_data = chunkjson['choices'][0]
        delta = json_data["delta"]

        if len(delta) == 0:
            is_termination_certain = False
            if (has_choices) and (chunkjson['choices'][0].get('finish_reason', 'null') == 'stop'): is_termination_certain = True
            if is_termination_certain: break
            else: continue # 对于不符合规范的狗屎接口,这里需要继续

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Shorten the input: clear or trim the chat history, or reduce the document chunk size the plugin feeds.
  2. Switch to a larger-context model (e.g. gpt-4-32k, gpt-4-turbo, gpt-4o) via the model dropdown / LLM_MODEL in config_private.py.
  3. If you maintain the calling plugin, implement sliding-window truncation of history so total tokens stay under the model limit.
  4. Check model_info[ELEMENT]['max_token'] in request_llms/bridge_all.py matches the model you actually deployed, especially on Azure/relay endpoints.

Example fix

# before
inputs = full_paper_text  # 100k tokens into a 4k model

# after
max_tokens = model_info[llm_kwargs['llm_model']]['max_token']
inputs = full_paper_text[: max_tokens * 3]  # rough chars-per-token cut, keep under limit
Defensive patterns

Strategy: fallback

Validate before calling

from request_llms.bridge_all import model_info
info = model_info[llm_kwargs['llm_model']]
# rough guard: chars/3 approximates tokens for mixed text
est_tokens = (len(inputs) + sum(len(a)+len(b) for a, b in history) + len(sys_prompt)) // 3
assert est_tokens < info['max_token'] * 0.9, f"input ~{est_tokens} tokens near limit {info['max_token']}"

Try / catch

try:
    reply = predict_no_ui_long_connection(...)
except ConnectionAbortedError as e:
    if 'reduce the length' in str(e):
        history = history[-2:]  # shrink context and retry once
        reply = predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window)
    else:
        raise

Prevention

When it happens

Trigger: Calling predict_no_ui_long_connection / predict with a history + inputs + system_prompt whose total tokens exceed the selected model's context window (e.g. 4096/8192/16384 depending on model); long-document plugins (arXiv translation, full-text reading) feeding oversized chunks; the non-data first chunk from the stream carries the error and 'reduce the length' appears in the body.

Common situations: Long conversations accumulated in history; using gpt-3.5-turbo-0301/0613 style 4k models against big paper PDFs; third-party one-api relays that map upstream overflow errors into plain-text bodies; forgetting that system_prompt also counts toward the limit.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/685b065bc2fe357f. Report an issue: GitHub.