binary-husky/gpt_academic · error · RuntimeError

未知的自动上下文裁剪策略: {policy}。

Error message

未知的自动上下文裁剪策略: {policy}。

What it means

Raised by auto_context_clip in toolbox.py:729 when the policy argument is neither 'each_message' nor 'search_optimal'. The function is a small dispatcher over per-strategy implementations (auto_context_clip_each_message, auto_context_clip_search_optimal) used to trim conversation history before it exceeds the model context window; an unknown policy string is rejected immediately as a programming/config error rather than silently skipping clipping.

Source

Thrown at toolbox.py:729

    from fastapi import FastAPI

    app = FastAPI()
    if custom_path != "/":

        @app.get("/")
        def read_main():
            return {"message": f"Gradio is running at: {custom_path}"}

    app = gr.mount_gradio_app(app, demo, path=custom_path)
    uvicorn.run(app, host="0.0.0.0", port=port)  # , auth=auth

def auto_context_clip(current, history, policy='search_optimal'):
    if policy == 'each_message':
        return auto_context_clip_each_message(current, history)
    elif policy == 'search_optimal':
        return auto_context_clip_search_optimal(current, history)
    else:
        raise RuntimeError(f"未知的自动上下文裁剪策略: {policy}。")



"""
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
第三部分
其他小工具:
    - zip_folder:    把某个路径下所有文件压缩,然后转移到指定的另一个路径中(gpt写的)
    - gen_time_str:  生成时间戳
    - ProxyNetworkActivate: 临时地启动代理网络(如果有)
    - objdump/objload: 快捷的调试函数
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"""


def zip_folder(source_folder, dest_folder, zip_name):
    import zipfile
    import os

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Set policy to one of the two supported values: 'each_message' or 'search_optimal' (the latter is the default and generally preferred).
  2. Check the config key feeding this call (e.g. AUTO_CONTEXT_CLIP_POLICY) in config_private.py for typos, casing, hyphens-vs-underscores, and stale values from older versions.
  3. If you need a custom strategy, implement it as a new auto_context_clip_* function and add an elif branch in the dispatcher instead of passing an unknown name.
  4. Pass no policy argument at all to get the default 'search_optimal' behavior.

Example fix

# before
auto_context_clip(current, history, policy='search-optimal')  # hyphen -> RuntimeError

# after
auto_context_clip(current, history, policy='search_optimal')
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED_CLIP_POLICIES = ('each_message', 'search_optimal')

def normalize_clip_policy(policy):
    if policy is None:
        return 'search_optimal'
    p = str(policy).strip().lower().replace('-', '_')
    if p not in SUPPORTED_CLIP_POLICIES:
        raise ValueError(f'policy must be one of {SUPPORTED_CLIP_POLICIES}, got {policy!r}')
    return p

policy = normalize_clip_policy(AUTO_CONTEXT_CLIP_POLICY)

Type guard

def is_supported_clip_policy(policy) -> bool:
    return policy in ('each_message', 'search_optimal')

Try / catch

try:
    current, history = auto_context_clip(current, history, policy=policy)
except RuntimeError as e:
    if '未知的自动上下文裁剪策略' in str(e):
        # fall back to the default strategy rather than crashing the chat turn
        current, history = auto_context_clip(current, history, policy='search_optimal')
    else:
        raise

Prevention

When it happens

Trigger: Calling auto_context_clip(current, history, policy='balanced') or any policy string outside the two supported values. Typically caused by a typo ('search-optimal', 'Search_Optimal'), a config value like AUTO_CONTEXT_CLIP_POLICY set to a deprecated/renamed strategy name after a version change, or new plugin code assuming a policy that was never implemented.

Common situations: Upgrading gpt_academic versions where a policy name was renamed but an old config_private.py still carries the previous name. Plugin authors passing an invented policy. Locale/case differences in config strings. Passing None as policy via a config default that was never filled in.

Related errors


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