{"record":{"id":"c51598ddc6227d4f","repo":"FoundationAgents/MetaGPT","slug":"request-failed-msg-self-event-source-decode-u","errorCode":null,"errorMessage":"Request failed, msg: {self._event_source.decode('utf-8')}, please ref to `https://open.bigmodel.cn/dev/api#error-code-v3`","messagePattern":"Request failed, msg: (.+?), please ref to `https://open\\.bigmodel\\.cn/dev/api#error-code-v3`","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"metagpt/provider/zhipuai/async_sse_client.py","lineNumber":16,"sourceCode":"#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n# @Desc   : async_sse_client to make keep the use of Event to access response\n#           refs to `zhipuai/core/_sse_client.py`\n\nimport json\nfrom typing import Any, Iterator\n\n\nclass AsyncSSEClient(object):\n    def __init__(self, event_source: Iterator[Any]):\n        self._event_source = event_source\n\n    async def stream(self) -> dict:\n        if isinstance(self._event_source, bytes):\n            raise RuntimeError(\n                f\"Request failed, msg: {self._event_source.decode('utf-8')}, please ref to `https://open.bigmodel.cn/dev/api#error-code-v3`\"\n            )\n        async for chunk in self._event_source:\n            line = chunk.data.decode(\"utf-8\")\n            if line.startswith(\":\") or not line:\n                return\n\n            field, _p, value = line.partition(\":\")\n            if value.startswith(\" \"):\n                value = value[1:]\n            if field == \"data\":\n                if value.startswith(\"[DONE]\"):\n                    break\n                data = json.loads(value)\n                yield data\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/provider/zhipuai/async_sse_client.py#L1-L32","documentation":"ZhipuAI's AsyncSSEClient.stream() expects an async iterator of SSE events; when the HTTP layer returns raw bytes instead (which happens when the server responds with a non-SSE error body), it raises RuntimeError embedding the decoded error message and a link to BigModel's error-code docs. It indicates the streaming request failed server-side before any events were produced.","triggerScenarios":"Calling zhipuai acreate_stream where the API returns an error status/body (invalid API key, rate limit, bad model name, malformed request); arequest hands back bytes, and the isinstance(self._event_source, bytes) check fires immediately on the first stream() call.","commonSituations":"Expired or wrong ZhipuAI API key, hitting QPS/quota limits, specifying a retired model id, or parameter errors (e.g. invalid messages payload) on the GLM endpoints.","solutions":["Decode and read the embedded msg in the RuntimeError; it contains Zhipu's official error code.","Validate/refresh the ZhipuAI api_key and confirm the account has quota.","Check the model name against current GLM model ids.","Catch the RuntimeError around stream consumption and surface the upstream error code to your monitoring."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    sse = await api.acreate_stream(**kwargs)\n    async for event in sse.stream():\n        ...\nexcept RuntimeError as e:\n    msg = str(e)\n    if \"error-code-v3\" in msg:\n        code = extract_upstream_error_code(msg)  # parse embedded JSON\n        handle_zhipu_error(code)  # 401 -> refresh key, 429 -> backoff\n    raise","preventionTips":["Pre-validate API key and model name before opening a stream","Parse the embedded msg for the official Zhipu error code","Add rate-limit backoff around streaming calls"],"tags":["zhipuai","sse","streaming","api-error","authentication"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}