{"record":{"id":"3a386b6931110cd6","repo":"binary-husky/gpt_academic","slug":"policy","errorCode":null,"errorMessage":"未知的自动上下文裁剪策略: {policy}。","messagePattern":"未知的自动上下文裁剪策略: (.+?)。","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"toolbox.py","lineNumber":729,"sourceCode":"    from fastapi import FastAPI\n\n    app = FastAPI()\n    if custom_path != \"/\":\n\n        @app.get(\"/\")\n        def read_main():\n            return {\"message\": f\"Gradio is running at: {custom_path}\"}\n\n    app = gr.mount_gradio_app(app, demo, path=custom_path)\n    uvicorn.run(app, host=\"0.0.0.0\", port=port)  # , auth=auth\n\ndef auto_context_clip(current, history, policy='search_optimal'):\n    if policy == 'each_message':\n        return auto_context_clip_each_message(current, history)\n    elif policy == 'search_optimal':\n        return auto_context_clip_search_optimal(current, history)\n    else:\n        raise RuntimeError(f\"未知的自动上下文裁剪策略: {policy}。\")\n\n\n\n\"\"\"\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n第三部分\n其他小工具:\n    - zip_folder:    把某个路径下所有文件压缩，然后转移到指定的另一个路径中（gpt写的）\n    - gen_time_str:  生成时间戳\n    - ProxyNetworkActivate: 临时地启动代理网络（如果有）\n    - objdump/objload: 快捷的调试函数\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n\"\"\"\n\n\ndef zip_folder(source_folder, dest_folder, zip_name):\n    import zipfile\n    import os","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/toolbox.py#L711-L747","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set policy to one of the two supported values: 'each_message' or 'search_optimal' (the latter is the default and generally preferred).","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.","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.","Pass no policy argument at all to get the default 'search_optimal' behavior."],"exampleFix":"# before\nauto_context_clip(current, history, policy='search-optimal')  # hyphen -> RuntimeError\n\n# after\nauto_context_clip(current, history, policy='search_optimal')","handlingStrategy":"validation","validationCode":"SUPPORTED_CLIP_POLICIES = ('each_message', 'search_optimal')\n\ndef normalize_clip_policy(policy):\n    if policy is None:\n        return 'search_optimal'\n    p = str(policy).strip().lower().replace('-', '_')\n    if p not in SUPPORTED_CLIP_POLICIES:\n        raise ValueError(f'policy must be one of {SUPPORTED_CLIP_POLICIES}, got {policy!r}')\n    return p\n\npolicy = normalize_clip_policy(AUTO_CONTEXT_CLIP_POLICY)","typeGuard":"def is_supported_clip_policy(policy) -> bool:\n    return policy in ('each_message', 'search_optimal')","tryCatchPattern":"try:\n    current, history = auto_context_clip(current, history, policy=policy)\nexcept RuntimeError as e:\n    if '未知的自动上下文裁剪策略' in str(e):\n        # fall back to the default strategy rather than crashing the chat turn\n        current, history = auto_context_clip(current, history, policy='search_optimal')\n    else:\n        raise","preventionTips":["Treat the policy string as an enum: validate it against ('each_message', 'search_optimal') at config load time.","After upgrading gpt_academic, grep the changelog/config template for renamed AUTO_CONTEXT_CLIP_POLICY values.","When adding a new strategy, extend the dispatcher's elif chain and the supported set together so validation and dispatch cannot drift."],"tags":["configuration","context-window","dispatch","typo"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}