binary-husky/gpt_academic · error · ConnectionAbortedError

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

Error message

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

What it means

ConnectionAbortedError from bridge_openrouter when the error body drained from the stream contains type":"upstream_error","param":"307 - OpenRouter's signal that the upstream provider finished but the response was truncated because the token budget ran out. The bridge converts it into a 'normally finished but token-insufficient' user message.

Source

Thrown at request_llms/bridge_openrouter.py:179

        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"]
                # 看门狗,如果超过期限没有喂狗,则终止
                if len(observe_window) >= 2:

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Raise the max_tokens / max_output_tokens value in llm_kwargs for this model
  2. Reduce the prompt size so more of the budget is available for output
  3. Switch to a different OpenRouter provider for the same model (the :nitx/:free variants often have tighter limits)
  4. Catch ConnectionAbortedError and re-ask for a shorter, chunked answer

Example fix

# before
llm_kwargs['max_tokens'] = 512

# after
llm_kwargs['max_tokens'] = 4096
Defensive patterns

Strategy: try-catch

Validate before calling

assert int(llm_kwargs.get('max_tokens', 0) or 0) >= 2048, \
    'max_tokens too low for OpenRouter upstream, truncation likely'

Try / catch

try:
    result = predict_no_ui_long_connection(...)
except ConnectionAbortedError:
    # upstream_error 307: output truncated at token budget
    llm_kwargs['max_tokens'] = min(llm_kwargs['max_tokens'] * 2, model_max)
    result = predict_no_ui_long_connection(...)

Prevention

When it happens

Trigger: OpenRouter routing to an upstream that hits its max_tokens mid-generation; max_tokens set lower than needed for the answer; upstream provider quota/budget exhaustion surfaced as upstream_error 307.

Common situations: Using OpenRouter with default low max_tokens; long code-generation answers cut off; upstream free-tier providers with tight output limits.

Related errors


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