xtekky/gpt4free · error · MissingAuthError

Status 401: Invalid session

Error message

Status 401: Invalid session

What it means

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.

Source

Thrown at g4f/Provider/Copilot.py:235

                    "correctPersonalizationSetting": True,
                    "performUserMerge": True,
                    "deferredDataUseCapable": True,
                }
                response = await session.post(
                    "https://copilot.microsoft.com/c/api/start",
                    headers={
                        "content-type": "application/json",
                        **(
                            {"x-useridentitytype": auth_result.useridentitytype}
                            if getattr(auth_result, "useridentitytype", None)
                            else {}
                        ),
                        **(headers or {}),
                    },
                    json=data,
                )
                if response.status_code == 401:
                    raise MissingAuthError("Status 401: Invalid session")
                response.raise_for_status()
                debug.log(
                    f"Copilot: Update cookies: [{', '.join(key for key in response.cookies)}]"
                )
                auth_result.cookies.update(
                    {key: value for key, value in response.cookies.items()}
                )
                if (
                    not getattr(auth_result, "access_token", None)
                    and not cls.needs_auth
                    and cls.anon_cookie_name not in auth_result.cookies
                ):
                    raise MissingAuthError(f"Missing cookie: {cls.anon_cookie_name}")
                conversation = Conversation(
                    response.json().get("currentConversationId")
                )
                debug.log(
                    f"Copilot: Created conversation: {conversation.conversation_id}"

View on GitHub (pinned to 973504e177)

Solutions

  1. Install the official Termux app from F-Droid (Play Store build is outdated) and retry
  2. 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
  3. For forks/renamed packages, patch the detection to check multiple package ids or a filesystem probe of /data/data/com.termux
  4. Run the g4f-go binary from inside Termux so PATH and pm access behave as expected

Example fix

// before
if ok, err := IsTermuxInstalled(); err != nil || !ok {
    return fmt.Errorf("Termux is required to build the python runner (install com.termux and retry)")
}

// after — distinguish check failure from absence, and accept forks
if err != nil {
    return fmt.Errorf("termux check failed: %w", err)
}
if !ok {
    if _, statErr := os.Stat("/data/data/com.termux"); statErr != nil {
        return fmt.Errorf("Termux is required to build the python runner (install com.termux and retry)")
    }
}
Defensive patterns

Strategy: validation

Validate before calling

if runtime.GOOS == "android" {
    if _, err := os.Stat("/data/data/com.termux"); err != nil {
        // surface a clear prerequisite error before attempting the build
        return fmt.Errorf("Termux app required: install com.termux and retry")
    }
}

Try / catch

ok, err := IsTermuxInstalled()
if err != nil {
    return fmt.Errorf("termux detection failed (pm unavailable?): %w", err)
}
if !ok {
    return fmt.Errorf("Termux is required to build the python runner (install com.termux and retry)")
}

Prevention

When it happens

Trigger: 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'.

Common situations: 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.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/069ed5d38153a6de. Report an issue: GitHub.