xtekky/gpt4free · error · MissingAuthError

Missing "api_key"

Error message

Missing "api_key"

What it means

GigaChat.create_async_generator raises MissingAuthError when the api_key argument is falsy. The GigaChat API requires a client API key (base64 client credentials) that is exchanged for a short-lived access token with the selected scope (default GIGACHAT_API_PERS). Unlike some providers there is no anonymous fallback.

Source

Thrown at g4f/Provider/needs_auth/GigaChat.py:96

    ]

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        stream: bool = True,
        proxy: str = None,
        api_key: str = None,
        connector: BaseConnector = None,
        scope: str = "GIGACHAT_API_PERS",
        update_interval: float = 0,
        **kwargs,
    ) -> AsyncResult:
        global access_token, token_expires_at
        model = cls.get_model(model)
        if not api_key:
            raise MissingAuthError('Missing "api_key"')

        # Create certificate file in cookies directory
        cookies_dir = Path(get_cookies_dir())
        cert_file = cookies_dir / "russian_trusted_root_ca.crt"

        # Write certificate if it doesn't exist
        if not cert_file.exists():
            cert_file.write_text(RUSSIAN_CA_CERT)

        if has_ssl and connector is None:
            ssl_context = ssl.create_default_context(cafile=str(cert_file))
            connector = TCPConnector(ssl_context=ssl_context)

        async with ClientSession(connector=get_connector(connector, proxy)) as session:
            if token_expires_at - int(time.time() * 1000) < 60000:
                async with session.post(
                    url="https://ngw.devices.sberbank.ru:9443/api/v2/oauth",
                    headers={

View on GitHub (pinned to 973504e177)

Solutions

  1. Pass the key explicitly: await client.chat.completions.create(model='...', messages=..., provider=g4f.Provider.GigaChat, api_key='<GIGACHAT_KEY>')
  2. Or add the key in g4f settings so it is injected automatically
  3. Confirm you are using a GigaChat-issued client key and the right scope (GIGACHAT_API_PERS vs GIGACHAT_API_B2B or API_CORP) for your account

Example fix

// before
response = await client.chat.completions.create(model='GigaChat', messages=messages, provider=g4f.Provider.GigaChat)

// after
response = await client.chat.completions.create(model='GigaChat', messages=messages, provider=g4f.Provider.GigaChat, api_key=os.environ['GIGACHAT_API_KEY'])
Defensive patterns

Strategy: validation

Validate before calling

import os

api_key = os.environ.get("GIGACHAT_API_KEY")
if not api_key:
    raise ValueError("GigaChat requires an api_key; set GIGACHAT_API_KEY or configure it in g4f settings")
response = await client.chat.completions.create(
    model=model, messages=messages, provider=g4f.Provider.GigaChat, api_key=api_key
)

Try / catch

from g4f.errors import MissingAuthError
try:
    async for chunk in GigaChat.create_async_generator(model, messages):
        ...
except MissingAuthError as e:
    if 'api_key' in str(e):
        raise ConfigurationError("supply GigaChat api_key") from e

Prevention

When it happens

Trigger: Calling the GigaChat provider (directly or via the g4f client) without api_key in kwargs and without a configured default key in settings.

Common situations: Provider selected by name without credentials; API key configured in a different settings field or env var so g4f does not see it; key passed under the wrong kwarg name.

Related errors


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