binary-husky/gpt_academic · error · RuntimeError

Json解析不合常规

Error message

Json解析不合常规

What it means

RuntimeError ('Json解析不合常规') from the except branch of the streaming loop in Claude predict_no_ui_long_connection. Any exception while iterating/decoding the SSE stream (JSONDecodeError from a malformed event, connection drop mid-frame, AttributeError when a chunk lacks expected keys) lands here; the handler pulls the raw error bytes via get_full_error, logs them, and re-raises with this generic message. So the message means 'the Anthropic event stream broke in a way that is not a clean watchdog cancel', with the real cause in the log.

Source

Thrown at request_llms/bridge_claude.py:138

                    # logger.info(f'[response] {result}')
                    break
                else:
                    if chunkjson and chunkjson['type'] == 'content_block_delta':
                        result += chunkjson['delta']['text']
                        if observe_window is not None:
                            # 观测窗,把已经获取的数据显示出去
                            if len(observe_window) >= 1:
                                observe_window[0] += chunkjson['delta']['text']
                            # 看门狗,如果超过期限没有喂狗,则终止
                            if len(observe_window) >= 2:
                                if (time.time()-observe_window[1]) > watch_dog_patience:
                                    raise RuntimeError("用户取消了程序。")
            except Exception as e:
                chunk = get_full_error(chunk, stream_response)
                chunk_decoded = chunk.decode()
                error_msg = chunk_decoded
                logger.error(error_msg)
                raise RuntimeError("Json解析不合常规")

    return result

def make_media_input(history,inputs,image_paths):
    for image_path in image_paths:
        inputs = inputs + f'<br/><br/><div align="center"><img src="file={os.path.abspath(image_path)}"></div>'
    return inputs

def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):
    """
    发送至chatGPT,流式获取输出。
    用于基础的对话功能。
    inputs 是本次问询的输入
    top_p, temperature是chatGPT的内部调优参数
    history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误)
    chatbot 为WebUI中显示的对话列表,修改它,然后yield出去,可以直接修改对话界面内容
    additional_fn代表点击的哪个按钮,按钮见functional.py
    """

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Check the logged error_msg (logger.error prints the raw chunk) — it distinguishes rate limit, auth, and truncation causes.
  2. Fix/verify the proxy: ANTHROPIC endpoint must be reachable and must not truncate SSE frames; try disabling proxies for api.anthropic.com.
  3. On 429 rate limits, slow down request frequency or upgrade your Anthropic plan tier.
  4. Update gpt_academic — newer bridge_claude.py versions harden the event parsing for schema changes.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    result = predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window)
except RuntimeError as e:
    if 'Json解析不合常规' in str(e):
        check_logged_raw_chunk()      # real cause is in logger.error output
        if was_rate_limit_or_transient():
            time.sleep(backoff); retry_request()
        else:
            raise

Prevention

When it happens

Trigger: Anthropic API returns an error event or HTML body mid-stream; proxy cuts the connection and a partial SSE frame fails json.loads; chunkjson structure deviates (missing 'type' or 'delta') causing a KeyError inside the try; rate-limit payload returned as non-JSON after the response object was already obtained.

Common situations: Unstable proxies to api.anthropic.com; hitting Anthropic rate limits (429 bodies interleaved into the stream); very long generations dropped by intermediary proxies; API version changes altering event schemas the parser expects.

Related errors


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