aix.credentials¶
Unified, discoverable API-key / secret resolution for AIX.
Every key-requiring function in AIX (chat, embeddings, generate_image,
text_to_speech, transcribe, generate_video, …) resolves its
credentials through a single, documented path – instead of relying on LiteLLM’s
implicit environment reading, scattered os.getenv calls, or an orphaned
config getter.
Resolution layers, highest precedence first:
Explicit argument –
chat(..., api_key=...).Provider environment variable – the canonical name for the inferred provider (e.g.
OPENAI_API_KEY,ANTHROPIC_API_KEY,GEMINI_API_KEY). A project.envis softly discovered whenpython-dotenvis installed (no hard dependency); explicit env vars always win over.envvalues.AIX config store – a config2py central store in the user app-config folder, keyed by the canonical env-var name (so the store and the env layer share one namespace). See
aix.util.(REPL only) interactive prompt – when
prompt_if_missing=Trueand running interactively, ask once and persist to the config store.
The provider for a model id is inferred via LiteLLM’s get_llm_provider; a
small curated PROVIDER_ENV_VARS table maps each documented provider to
its env-var name(s) and console URL.
When a required key is genuinely absent, check_requirements() raises
MissingCredentialError – naming which key is missing, how to set
it, and where to obtain one – rather than letting a cryptic, provider-specific
LiteLLM error surface late. This preflight is wired in via the
requires_credentials() decorator so error-raising stays separate from
business logic.
Examples
>>> from aix.credentials import resolve_api_key, PROVIDER_ENV_VARS
>>> resolve_api_key("gpt-4o", api_key="sk-explicit") # explicit wins
'sk-explicit'
>>> "openai" in PROVIDER_ENV_VARS
True
- Security:
Resolved key values are never logged or returned by diagnostics;
check_keys()reports availability only. Keys live in the user app-config folder, never in the repo.
Functions
|
Infer the provider name from a model id (or pass through a provider name). |
|
Return the env-var name(s) for |
|
Resolve an API key for |
|
Presence-only preflight: ensure a key for |
|
Decorator: preflight credentials before the wrapped function runs. |
|
Report, per provider, whether a usable API key is discoverable. |
Exceptions
|
Raised when a required API key cannot be resolved. |
- exception aix.credentials.MissingCredentialError(model_or_provider, *, provider=None, env_names=None)[source]¶
Bases:
ExceptionRaised when a required API key cannot be resolved.
The message names which key is missing, how to set it, and (when known) where to obtain one. Key values are never included.
- aix.credentials.check_keys(providers=None)[source]¶
Report, per provider, whether a usable API key is discoverable.
Never returns or logs key values – only availability and the env-var name(s) checked. Useful for quick setup debugging.
- Parameters:
providers (
Optional[Iterable[str]]) – Providers to check; defaults to every provider inPROVIDER_ENV_VARS.- Returns:
bool, “env_vars”: […], “source”: <where-found-or-None>}``.
sourceis"env","store", orNone(never the value itself).- Return type:
Examples
>>> report = check_keys(["openai"]) >>> report["openai"]["available"] True
- aix.credentials.check_requirements(model_or_provider, *, api_key=None)[source]¶
Presence-only preflight: ensure a key for
model_or_provideris resolvable.Does not validate the key over the network – only that one is discoverable via
resolve_api_key()(explicit arg, env/.env, or store). RaisesMissingCredentialErrorwith actionable guidance when absent.Returns
Truewhen a key is available.- Return type:
Examples
>>> check_requirements("gpt-4o", api_key="sk-explicit") True
- aix.credentials.infer_provider(model_or_provider)[source]¶
Infer the provider name from a model id (or pass through a provider name).
Uses LiteLLM’s
get_llm_providerwhen available. Ifmodel_or_provideris already a known provider name (a key inPROVIDER_ENV_VARS), it is returned as-is. ReturnsNoneif the provider cannot be determined.Examples
>>> infer_provider("gpt-4o") 'openai' >>> infer_provider("openrouter") # already a provider name 'openrouter' >>> infer_provider(None) is None True
- aix.credentials.provider_env_vars(provider)[source]¶
Return the env-var name(s) for
provideras a list (possibly empty).Examples
>>> provider_env_vars("openai") ['OPENAI_API_KEY'] >>> provider_env_vars("gemini") ['GEMINI_API_KEY', 'GOOGLE_API_KEY'] >>> provider_env_vars("totally-unknown-provider") []
- aix.credentials.requires_credentials(default_model)[source]¶
Decorator: preflight credentials before the wrapped function runs.
Separates error-raising from business logic (per the AIX coding principles). The wrapped function is expected to take keyword-only
modelandapi_keyparameters.default_modelis a zero-arg callable returning the model the function would default to (read from the active config at call time), so the preflight checks the same model the function will use.- Parameters:
default_model (
Callable[[],Optional[str]]) – Callable returning the default model id when the caller passesmodel=None.
Examples
>>> from aix import config >>> @requires_credentials(lambda: config.get_config().chat.model) ... def f(*, model=None, api_key=None): ... return "ran" >>> f(api_key="sk-explicit") 'ran'
- aix.credentials.resolve_api_key(model_or_provider, *, api_key=None, prompt_if_missing=False)[source]¶
Resolve an API key for
model_or_providerthrough the documented layers.Layers, highest precedence first: explicit
api_keyargument, provider environment variable (with soft.envdiscovery), the AIX config store, and – only whenprompt_if_missingis true and running interactively – an interactive prompt that persists the answer.- Parameters:
model_or_provider (
Optional[str]) – A model id (provider inferred via LiteLLM) or a provider name directly (e.g."openai").api_key (
Optional[str]) – An explicit key; when given (non-empty) it is returned verbatim.prompt_if_missing (
bool) – If true, fall back to a REPL prompt + persist when no key is found elsewhere. Off by default so the common path never blocks on input.
- Return type:
- Returns:
The resolved key, or
Noneif genuinely absent.
Examples
>>> resolve_api_key("gpt-4o", api_key="sk-explicit") 'sk-explicit'