binary-husky/gpt_academic · error · ConnectionAbortedError

正常结束,但显示Token不足,导致输出不完整,请削减单次输入的文本量。

Error message

正常结束,但显示Token不足,导致输出不完整,请削减单次输入的文本量。

What it means

Raised as ConnectionAbortedError when the error body recovered from the stream contains type":"upstream_error","param":"307". This signature comes from api2d / one-api style relay gateways: the relay accepted your request but its upstream (the real OpenAI account behind it) ran out of quota or token capacity, so output was cut off. The bridge translates it into the same 'token insufficiency, reduce input' message as a native length error.

Source

Thrown at request_llms/bridge_chatgpt.py:185

        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 # 对于不符合规范的狗屎接口,这里需要继续

        if (not has_content) and has_role: continue
        if (not has_content) and (not has_role): continue # raise RuntimeError("发现不标准的第三方接口:"+delta)

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Log in to your api2d/one-api dashboard and check the balance/quota of the key and its upstream channel; top up or switch channel.
  2. Retry with a shorter input — genuinely oversized requests also strain low-quota upstreams.
  3. Switch to a different API key or direct OpenAI/Azure endpoint to confirm whether the relay is the failing layer.
  4. If you operate one-api yourself, replace or top up the upstream OpenAI key bound to that channel.
Defensive patterns

Strategy: fallback

Try / catch

try:
    reply = predict_no_ui_long_connection(...)
except ConnectionAbortedError as e:
    if 'upstream_error' in str(e) or 'Token不足' in str(e):
        switch_to_backup_api_key_or_endpoint()  # relay upstream quota exhausted
    else:
        raise

Prevention

When it happens

Trigger: Using gpt_academic through an api2d or one-api relay endpoint whose upstream account has exhausted its quota/balance; the relay returns a non-SSE chunk whose decoded body matches the upstream_error/307 pattern; typically happens mid-conversation or on long inputs routed through the relay.

Common situations: Third-party relay keys purchased with limited quota; shared one-api channels whose upstream key hit its rate/spend cap; message tells you to reduce input but the true cause is upstream quota, not your prompt size.

Related errors


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