xtekky/gpt4free · error · MissingAuthError

Missing "_U" cookie

Error message

Missing "_U" cookie

What it means

Thrown by g4f/Provider/needs_auth/BingCreateImages.py when image generation cannot proceed because there is no Bing session: self.cookies is empty AND the cookie jar loaded from disk (get_cookies(".bing.com", False)) is either None or lacks the "_U" cookie. The "_U" cookie is Bing's authentication cookie; without it the create_images API call would be unauthorized, so the provider fails fast before making any request.

Source

Thrown at g4f/Provider/needs_auth/BingCreateImages.py:56

        proxy: str = None,
        **kwargs,
    ) -> AsyncResult:
        session = BingCreateImages(cookies, proxy, api_key)
        yield await session.generate(format_media_prompt(messages, prompt))

    async def generate(self, prompt: str) -> ImageResponse:
        """
        Asynchronously creates a markdown formatted string with images based on the prompt.

        Args:
            prompt (str): Prompt to generate images.

        Returns:
            str: Markdown formatted string with images.
        """
        cookies = self.cookies or get_cookies(".bing.com", False)
        if cookies is None or "_U" not in cookies:
            raise MissingAuthError('Missing "_U" cookie')
        async with create_session(cookies, self.proxy) as session:
            images = await create_images(session, prompt)
            return ImageResponse(
                images,
                prompt,
                {"preview": "{image}?w=200&h=200"} if len(images) > 1 else {},
            )

View on GitHub (pinned to 973504e177)

Solutions

  1. Log in to bing.com in your browser, export cookies (e.g. via a cookie-export extension or a HAR capture) and place the file in g4f's har_and_cookies directory so get_cookies finds a valid "_U" cookie.
  2. Or pass the cookies explicitly: instantiate the provider and set cookies={"_U": "..."} before calling generate.
  3. Re-export cookies if the existing file is stale; confirm the "_U" key is actually present in the file.

Example fix

# before
provider = BingCreateImages()
images = await provider.generate("a cat")  # raises Missing "_U" cookie

# after
provider = BingCreateImages(cookies={"_U": "<your bing _U cookie value>"})
images = await provider.generate("a cat")
Defensive patterns

Strategy: validation

Validate before calling

from g4f.requests import get_cookies

def has_bing_auth(explicit_cookies=None):
    cookies = explicit_cookies or get_cookies(".bing.com", False) or {}
    return "_U" in cookies

if not has_bing_auth(provider.cookies):
    raise SystemExit("Export .bing.com cookies containing _U into har_and_cookies/ first")

Try / catch

try:
    images = await provider.generate(prompt)
except MissingAuthError as e:
    if '_U' in str(e):
        refresh_bing_cookies()

Prevention

When it happens

Trigger: Calling generate(prompt) on BingCreateImages without passing cookies while no .bing.com HAR/cookie file has been imported into g4f's har_and_cookies storage, or with a cookie file that does not contain the "_U" entry. Only the False (no-raise) variant of get_cookies is used, so a missing jar resolves to None and triggers the error.

Common situations: Fresh g4f install with no exported Bing cookies; expired cookie export where "_U" was not captured; user logged into bing.com in a different browser than the one used to export cookies/HAR.

Related errors


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