{"record":{"id":"6304e5d061212396","repo":"xtekky/gpt4free","slug":"line-get-error","errorCode":null,"errorMessage":"line.get(\"error\")","messagePattern":"line\\.get\\(\"error\"\\)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/OpenaiChat.py","lineNumber":1338,"sourceCode":"                        fields.is_thinking = True\n                        yield Reasoning(\n                            status=m.get(\"metadata\", {}).get(\"initial_text\")\n                        )\n                    # if c.get(\"content_type\") == \"multimodal_text\":\n                    #    for part in c.get(\"parts\"):\n                    #        if isinstance(part, dict) and part.get(\"content_type\") == \"image_asset_pointer\":\n                    #            yield await cls.get_generated_image(session, auth_result, part, fields.prompt, fields.conversation_id)\n                    if m.get(\"author\", {}).get(\"role\") == \"assistant\":\n                        if fields.parent_message_id is None:\n                            fields.parent_message_id = v.get(\"message\", {}).get(\"id\")\n                        fields.message_id = v.get(\"message\", {}).get(\"id\")\n                    if m.get(\"status\") == \"finished_successfully\" and m.get(\n                        \"metadata\", {}\n                    ).get(\"image_gen_task_id\"):\n                        fields.task = v\n            return\n        if \"error\" in line and line.get(\"error\"):\n            raise RuntimeError(line.get(\"error\"))\n\n    @classmethod\n    async def synthesize(cls, params: dict) -> AsyncIterator[bytes]:\n        async with StreamSession(impersonate=\"chrome\", timeout=0) as session:\n            async with session.get(\n                f\"{cls.url}/backend-api/synthesize\", params=params, headers=cls._headers\n            ) as response:\n                await raise_for_status(response)\n                async for chunk in response.iter_content():\n                    yield chunk\n\n    @classmethod\n    async def login_generator(\n        cls,\n        proxy: str = None,\n        api_key: str = None,\n        proof_token: str = None,\n        cookies: Cookies = None,","sourceCodeStart":1320,"sourceCodeEnd":1356,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/OpenaiChat.py#L1320-L1356","documentation":"While processing each line of the OpenAI chat event stream, a line containing a truthy top-level 'error' field causes g4f to raise RuntimeError with that value (as written the message is the literal expression line.get(\"error\") — the effective payload is the error content). It surfaces in-band errors the server embeds in the stream rather than as HTTP status.","triggerScenarios":"During iter_lines() of the conversation POST, any NDJSON line like {\"error\": {...}} — e.g. expired session mid-stream, content policy blocks, model errors, or deactivation of the conversation after it started.","commonSituations":"Long streams where the token expires before completion; moderation triggers mid-response; backend errors surfacing after a 200 OK start; concurrent use of the same session from two clients.","solutions":["Log the error value — it carries the upstream reason string and dictates the remedy","If session-related, re-authenticate and retry the prompt in a new conversation","Shorten very long conversations/requests if the error fires near stream end","Update g4f for the current stream error format"],"exampleFix":"// before\n# error lost, no context\nexcept RuntimeError: raise\n\n// after\ntry:\n    async for chunk in stream: process(chunk)\nexcept RuntimeError as e:\n    logging.error('OpenaiChat stream error payload: %s', e.args)\n    raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def stream_line_has_error(line: dict) -> bool:\n    return bool(line.get('error'))","tryCatchPattern":"try:\n    async for chunk in stream:\n        yield chunk\nexcept RuntimeError as e:\n    # e.args carries the in-band error payload from the stream\n    logging.error('OpenaiChat stream aborted with: %s', e.args)\n    if is_auth_related(e.args):\n        await OpenaiChat.nodriver_auth()\n    raise","preventionTips":["Log the error payload from the exception args — it carries the upstream reason","Re-authenticate when mid-stream session expiry is suspected","Keep requests short enough to finish before token rotation"],"tags":["openai-chat","streaming","in-band-error","runtime"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}