{"record":{"id":"22fdc5825e7d67d8","repo":"xtekky/gpt4free","slug":"error-code-error-details","errorCode":null,"errorMessage":"{error[\"code\"]}: {error[\"details\"]}","messagePattern":"\\{error\\[\"code\"\\]\\}: \\{error\\[\"details\"\\]\\}","errorType":"exception","errorClass":"ResponseError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/Qwen.py","lineNumber":681,"sourceCode":"                                \"data\", {}\r\n                            ).get(\"code\"):\r\n                                raise RuntimeError(f\"Response: {resp_json}\")\r\n                            else:\r\n                                # cant stream resp after `resp_json = await resp.json()`, so it stick\r\n                                raise RuntimeError(f\"Response: {resp_json}\")\r\n                        # args[\"cookies\"] = merge_cookies(args.get(\"cookies\"), resp)\r\n                        thinking_started = False\r\n                        usage = None\r\n                        async for chunk in sse_stream(resp):\r\n                            try:\r\n                                if \"response.created\" in chunk:\r\n                                    conversation.parent_id = chunk.get(\r\n                                        \"response.created\", {}\r\n                                    ).get(\"response_id\")\r\n                                    yield conversation\r\n                                error = chunk.get(\"error\", {})\r\n                                if error:\r\n                                    raise ResponseError(\r\n                                        f'{error[\"code\"]}: {error[\"details\"]}'\r\n                                    )\r\n                                usage = chunk.get(\"usage\", usage)\r\n                                choices = chunk.get(\"choices\", [])\r\n                                if not choices:\r\n                                    continue\r\n                                delta = choices[0].get(\"delta\", {})\r\n                                phase = delta.get(\"phase\")\r\n                                content = delta.get(\"content\")\r\n                                status = delta.get(\"status\")\r\n                                extra = delta.get(\"extra\", {})\r\n                                if phase == \"think\" and not thinking_started:\r\n                                    thinking_started = True\r\n                                elif phase == \"answer\" and thinking_started:\r\n                                    thinking_started = False\r\n                                elif phase == \"image_gen\" and status == \"typing\":\r\n                                    yield ImageResponse(content, prompt, extra)\r\n                                    continue\r","sourceCodeStart":663,"sourceCodeEnd":699,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/Qwen.py#L663-L699","documentation":"Raised as ResponseError when an SSE chunk inside Qwen's completion stream contains an 'error' object; the message is '{code}: {details}' from the server. Streaming had started (response.created received) but the backend aborted mid-stream with a structured error.","triggerScenarios":"Mid-stream failures: content moderation tripping on the prompt partway through, model overloaded (server-side error chunks), free-tier token-bucket exhausted mid-generation, or image-generation phase failing.","commonSituations":"Long generations hitting limits near completion; sensitive prompts; peak-hour load on chat.qwen.ai; image models failing on blocked content.","solutions":["Check code:details — moderation failures need prompt changes; capacity/limit errors need backoff and retry.","Retry with the same conversation (parent_id is preserved per chunk) or a fresh one.","Shorten the prompt / split the task for moderation-triggered errors.","Update g4f for new error-code handling."],"exampleFix":"# before\nasync for chunk in Qwen.create_async_generator(model, msgs):\n    print(chunk)\n\n# after\nfrom g4f.errors import ResponseError\ntry:\n    async for chunk in Qwen.create_async_generator(model, msgs):\n        print(chunk)\nexcept ResponseError as e:\n    if 'content' in str(e).lower():\n        msgs[-1]['content'] = soften(msgs[-1]['content'])\n    async for chunk in Qwen.create_async_generator(model, msgs):\n        print(chunk)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def is_moderation_error(exc: Exception) -> bool:\n    from g4f.errors import ResponseError\n    return isinstance(exc, ResponseError) and any(\n        k in str(exc).lower() for k in ('content', 'sensitive', 'moderation', 'policy')\n    )","tryCatchPattern":"from g4f.errors import ResponseError\ntry:\n    async for chunk in Qwen.create_async_generator(model, msgs):\n        handle(chunk)\nexcept ResponseError as e:\n    if 'rate' in str(e).lower():\n        await asyncio.sleep(120)\n        async for chunk in Qwen.create_async_generator(model, msgs):\n            handle(chunk)\n    else:  # moderation / policy\n        raise","preventionTips":["Act on chunks as they arrive — mid-stream death loses the rest","Branch on code:details (moderation vs capacity vs limits)","Shorten sensitive prompts to reduce moderation triggers","Retry capacity errors after backoff"],"tags":["qwen","sse","mid-stream-error","moderation","rate-limit"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}