{"record":{"id":"15517ef5f6e46158","repo":"binary-husky/gpt_academic","slug":"token","errorCode":null,"errorMessage":"正常结束，但显示Token不足，导致输出不完整，请削减单次输入的文本量。","messagePattern":"正常结束，但显示Token不足，导致输出不完整，请削减单次输入的文本量。","errorType":"exception","errorClass":"ConnectionAbortedError","httpStatus":null,"severity":"error","filePath":"request_llms/bridge_chatgpt.py","lineNumber":185,"sourceCode":"        return gpt_replying_buffer\n\n    stream_response = response.iter_lines()\n    result = ''\n    json_data = None\n    while True:\n        try: chunk = next(stream_response)\n        except StopIteration:\n            break\n        except requests.exceptions.ConnectionError:\n            chunk = next(stream_response) # 失败了，重试一次？再失败就没办法了。\n        chunk_decoded, chunkjson, has_choices, choice_valid, has_content, has_role = decode_chunk(chunk)\n        if len(chunk_decoded)==0: continue\n        if not chunk_decoded.startswith('data:'):\n            error_msg = get_full_error(chunk, stream_response).decode()\n            if \"reduce the length\" in error_msg:\n                raise ConnectionAbortedError(\"OpenAI拒绝了请求:\" + error_msg)\n            elif \"\"\"type\":\"upstream_error\",\"param\":\"307\"\"\" in error_msg:\n                raise ConnectionAbortedError(\"正常结束，但显示Token不足，导致输出不完整，请削减单次输入的文本量。\")\n            else:\n                raise RuntimeError(\"OpenAI拒绝了请求：\" + error_msg)\n        if ('data: [DONE]' in chunk_decoded): break # api2d & one-api 正常完成\n        # 提前读取一些信息 （用于判断异常）\n        if has_choices and not choice_valid:\n            # 一些垃圾第三方接口的出现这样的错误\n            continue\n        json_data = chunkjson['choices'][0]\n        delta = json_data[\"delta\"]\n\n        if len(delta) == 0:\n            is_termination_certain = False\n            if (has_choices) and (chunkjson['choices'][0].get('finish_reason', 'null') == 'stop'): is_termination_certain = True\n            if is_termination_certain: break\n            else: continue # 对于不符合规范的狗屎接口，这里需要继续\n\n        if (not has_content) and has_role: continue\n        if (not has_content) and (not has_role): continue # raise RuntimeError(\"发现不标准的第三方接口：\"+delta)","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_chatgpt.py#L167-L203","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Retry with a shorter input — genuinely oversized requests also strain low-quota upstreams.","Switch to a different API key or direct OpenAI/Azure endpoint to confirm whether the relay is the failing layer.","If you operate one-api yourself, replace or top up the upstream OpenAI key bound to that channel."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    reply = predict_no_ui_long_connection(...)\nexcept ConnectionAbortedError as e:\n    if 'upstream_error' in str(e) or 'Token不足' in str(e):\n        switch_to_backup_api_key_or_endpoint()  # relay upstream quota exhausted\n    else:\n        raise","preventionTips":["Configure multiple API keys (comma-separated API_KEY) so select_api_key can rotate when one relay runs dry.","Monitor relay dashboard quota and alert before exhaustion.","Keep a direct OpenAI/Azure fallback endpoint configured for relay outages.","Recognize that 'reduce input' wording here is misleading — verify upstream quota first."],"tags":["relay","api2d","one-api","quota","upstream-error"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}