{"record":{"id":"069ed5d38153a6de","repo":"xtekky/gpt4free","slug":"status-401-invalid-session","errorCode":null,"errorMessage":"Status 401: Invalid session","messagePattern":"Status 401: Invalid session","errorType":"http","errorClass":"MissingAuthError","httpStatus":401,"severity":"error","filePath":"g4f/Provider/Copilot.py","lineNumber":235,"sourceCode":"                    \"correctPersonalizationSetting\": True,\n                    \"performUserMerge\": True,\n                    \"deferredDataUseCapable\": True,\n                }\n                response = await session.post(\n                    \"https://copilot.microsoft.com/c/api/start\",\n                    headers={\n                        \"content-type\": \"application/json\",\n                        **(\n                            {\"x-useridentitytype\": auth_result.useridentitytype}\n                            if getattr(auth_result, \"useridentitytype\", None)\n                            else {}\n                        ),\n                        **(headers or {}),\n                    },\n                    json=data,\n                )\n                if response.status_code == 401:\n                    raise MissingAuthError(\"Status 401: Invalid session\")\n                response.raise_for_status()\n                debug.log(\n                    f\"Copilot: Update cookies: [{', '.join(key for key in response.cookies)}]\"\n                )\n                auth_result.cookies.update(\n                    {key: value for key, value in response.cookies.items()}\n                )\n                if (\n                    not getattr(auth_result, \"access_token\", None)\n                    and not cls.needs_auth\n                    and cls.anon_cookie_name not in auth_result.cookies\n                ):\n                    raise MissingAuthError(f\"Missing cookie: {cls.anon_cookie_name}\")\n                conversation = Conversation(\n                    response.json().get(\"currentConversationId\")\n                )\n                debug.log(\n                    f\"Copilot: Created conversation: {conversation.conversation_id}\"","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/Copilot.py#L217-L253","documentation":"buildAndroidRunner gates on IsTermuxInstalled(); any error from the pm query OR a negative result returns this error, since the runner must be compiled by Termux's clang against the downloaded python headers. The error deliberately conflates 'check failed' and 'not installed' — both block the build.","triggerScenarios":"Calling the runtime setup on Android without the Termux app installed; running in an adb shell where pm is unavailable (so err != nil); Termux installed under a different package name (F-Droid build com.termux vs forks); pm output not containing 'com.termux'.","commonSituations":"Users on stock Android without Termux expecting the Go binary to be self-contained; Termux forks (e.g. com.termux.x11 variants) not matching the hardcoded package string; rooted-ROM users where pm is restricted.","solutions":["Install the official Termux app from F-Droid (Play Store build is outdated) and retry","If Termux IS installed, debug the pm query itself — the wrapped err path also lands here (see the 'failed to query package manager' error) so test `pm list packages | grep termux` in the same shell","For forks/renamed packages, patch the detection to check multiple package ids or a filesystem probe of /data/data/com.termux","Run the g4f-go binary from inside Termux so PATH and pm access behave as expected"],"exampleFix":"// before\nif ok, err := IsTermuxInstalled(); err != nil || !ok {\n    return fmt.Errorf(\"Termux is required to build the python runner (install com.termux and retry)\")\n}\n\n// after — distinguish check failure from absence, and accept forks\nif err != nil {\n    return fmt.Errorf(\"termux check failed: %w\", err)\n}\nif !ok {\n    if _, statErr := os.Stat(\"/data/data/com.termux\"); statErr != nil {\n        return fmt.Errorf(\"Termux is required to build the python runner (install com.termux and retry)\")\n    }\n}","handlingStrategy":"validation","validationCode":"if runtime.GOOS == \"android\" {\n    if _, err := os.Stat(\"/data/data/com.termux\"); err != nil {\n        // surface a clear prerequisite error before attempting the build\n        return fmt.Errorf(\"Termux app required: install com.termux and retry\")\n    }\n}","typeGuard":null,"tryCatchPattern":"ok, err := IsTermuxInstalled()\nif err != nil {\n    return fmt.Errorf(\"termux detection failed (pm unavailable?): %w\", err)\n}\nif !ok {\n    return fmt.Errorf(\"Termux is required to build the python runner (install com.termux and retry)\")\n}","preventionTips":["Distinguish detection failure from absence so users with Termux installed are not told to reinstall","Document the Termux prerequisite prominently for Android users before they reach this code path"],"tags":["android","termux","build","prerequisites"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}