feat(models): add @extensible decorator to get_api_key()#1258
feat(models): add @extensible decorator to get_api_key()#1258frdel merged 1 commit intoagent0ai:developmentfrom
Conversation
ea88e18 to
f17682d
Compare
Why
|
f17682d to
88dcc88
Compare
|
This PR has been rebased onto current The Both PRs are independent and can be merged in any order. |
|
This will be useful to implement different credential helpers/providers - like vault for example - or keystone / other cloud provider secrets management solution. |
Add @extensible decorator to get_api_key() in models.py, enabling plugins to intercept or replace the API key resolution logic for any LLM provider. Currently, get_api_key() resolves API keys exclusively from OS environment variables via os.getenv(). This means external secrets backends (OpenBao/Vault, AWS Secrets Manager, Azure Key Vault, etc.) cannot provide API keys without injecting them into the process environment. With @extensible, a plugin can intercept the start hook to resolve API keys from any backend, falling through to the default dotenv behaviour when no plugin is active. No behavioural change for existing users — the decorator only adds extension hooks around the existing function. Note: the companion fix for the circular import this previously required (deferred import in settings.py) is submitted separately in PR agent0ai#1295.
88dcc88 to
7d9ec2c
Compare
Problem
get_api_key()inmodels.pyresolves API keys exclusively from OS environment variables viaos.getenv(). This means external secrets backends (OpenBao/Vault, AWS Secrets Manager, Azure Key Vault, etc.) cannot provide API keys to LiteLLM without injecting them into the process environment — a workaround that is fragile, doesn't support cache TTLs, and bypasses the existing SecretsManager architecture.While
helpers/secrets.pyfactory functions were recently made extensible (#1246), the actual API key resolution path for chat/embedding/utility models goes throughmodels.get_api_key()→dotenv.get_dotenv_value()→os.getenv(), completely bypassing SecretsManager.Solution
Add the
@extensibledecorator toget_api_key(), enabling plugins to intercept API key resolution at themodels_get_api_key_startextension point.This is a 2-line change:
extensiblefromhelpers.extension@extensibledecorator toget_api_key()How it works
With
@extensible, a plugin extension can:data["result"]in a_starthook to provide an API key from any backend, short-circuiting the default dotenv lookupos.getenv()behaviour when no plugin is activedata["args"]ordata["kwargs"]to alter the service name lookupBackward Compatibility
No behavioural change for existing users. The
@extensibledecorator only adds extension hooks around the existing function. Without any registered extensions,get_api_key()behaves identically to before.Motivating Use Case
OpenBao secrets backend plugin that needs to provide LiteLLM API keys from a KV v2 secrets engine, with TTL-based caching and automatic fallback to
.envfiles.Related
models_get_api_key_start/models_get_api_key_end