Skip to content

Mistral() silently accepts api_key=None with no env var fallback — error only at call time #519

@YousefZahran1

Description

@YousefZahran1

Bug

Mistral() with no api_key and no MISTRAL_API_KEY env var constructs successfully with api_key=None. No env var lookup happens at init time. Misconfiguration is only discovered when the first API call fails — the opposite of how OpenAI and Anthropic SDKs behave.

Reproduce (mistralai 2.4.5, no API key needed)

import os, inspect
from mistralai.client import Mistral

os.environ.pop('MISTRAL_API_KEY', None)

client = Mistral()                          # No exception raised
print('Client created with no key')        # This prints

sig = inspect.signature(Mistral.__init__)
print(sig.parameters['api_key'].default)   # None — no env var fallback

Expected

Check MISTRAL_API_KEY at construction time. Raise ValueError immediately if no key found — matching OpenAI and Anthropic SDK behavior.

Suggested Fix

def __init__(self, api_key=None, ...):
    if api_key is None:
        api_key = os.environ.get('MISTRAL_API_KEY')
    if api_key is None:
        raise ValueError(
            'No API key provided. Pass api_key=... or set MISTRAL_API_KEY env var.'
        )

Happy to open a PR.

— Youssef Ibrahim | github.com/YousefZahran1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions