feat: add LiteLLM plugin for 100+ LLM providers#6527
Conversation
|
|
| # If api_key provided, set it as env var for the provider litellm will resolve | ||
| if is_given(api_key) and api_key: | ||
| os.environ.setdefault("LITELLM_API_KEY", api_key) |
There was a problem hiding this comment.
π‘ Explicitly provided API key is silently ignored
An API key passed to the LiteLLM model is stored (os.environ.setdefault("LITELLM_API_KEY", ...) at livekit-plugins/livekit-plugins-litellm/livekit/plugins/litellm/llm.py:105) under a name the LiteLLM SDK never reads for provider authentication, so the key is never actually used and requests can fail to authenticate.
Impact: A user who passes an API key directly instead of using environment variables gets authentication failures at request time even though the key was accepted without error.
Prompt for agents
The constructor accepts an api_key argument but only writes it to os.environ under LITELLM_API_KEY via os.environ.setdefault (llm.py:103-105). This has two problems: (1) the LiteLLM SDK's acompletion() does not read a generic LITELLM_API_KEY env var to authenticate to the underlying provider β it reads provider-specific vars (OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.) or accepts an explicit api_key= keyword argument on the completion call; and (2) setdefault silently does nothing if LITELLM_API_KEY already exists in the environment. As a result, an explicitly supplied api_key is effectively dropped and provider auth may fail. Consider storing the api_key on the instance and threading it through the shim so it is passed as api_key= into litellm.acompletion() (the shim's create() could inject a default api_key into kwargs), rather than setting an unused env var. Ensure the value flows to the actual completion call for the chosen provider.
Was this helpful? React with π or π to provide feedback.
Description
This pull request adds
livekit-plugins-litellmas a new plugin package. It integrates the LiteLLM SDK directly into LiveKit Agents, enabling developers to connect to 100+ LLM providers (Anthropic, Bedrock, Vertex AI, Cohere, etc.) through a single, unified interface.Key Changes
livekit-plugins-litellm):llm.py: Implements theLLMclass inheriting fromOpenAILLMto reuse existing streaming, chunking, and tool-calling infrastructure._LiteLLMClientShim: AnAsyncOpenAI-compatible shim client that routes all completion requests tolitellm.acompletionwithout requiring an external proxy server.drop_params(defaulting toTrue) to prevent provider-specific parameters from causing API errors.pyproject.toml):livekit-plugins-litellmin the workspace under[tool.uv.sources].tests/test_litellm_llm.py):drop_paramsoverrides, defaults, and model instantiations.Verification
Run the unit test suite:
python -m unittest livekit-plugins/livekit-plugins-litellm/tests/test_litellm_llm.pyResults:
Ran 8 tests in 4.133s - OK