feat: add LiteLLM as unified LLM provider#339
Open
RheagalFire wants to merge 1 commit into
Open
Conversation
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
litellm.completion()as an SDK dependency.LiteLLMAPI) and async (AsyncLiteLLMAPI) classes, following the same pattern asGPTAPI/AsyncGPTAPI.Motivation
lagent currently has dedicated providers for OpenAI (
GPTAPI) and Anthropic (ClaudeAPI). Users who want to use Google Gemini, Azure OpenAI, AWS Bedrock, Ollama, or any other provider need to write a new provider class from scratch. LiteLLM translatescompletion()-style calls to any of 100+ providers, so a singleLiteLLMAPIclass covers all of them with the same interface lagent already uses.Changes
lagent/llms/litellm_llm.py-LiteLLMAPI(sync) andAsyncLiteLLMAPI(async) extendingBaseAPILLM/AsyncBaseAPILLM. Implementschat(),_chat(),stream_chat(),_stream_chat(). Useslitellm.completion()/litellm.acompletion()withdrop_params=Truefor cross-provider compatibility. Lazy-imports litellm so the base install is unaffected.lagent/llms/__init__.py- registeredLiteLLMAPIandAsyncLiteLLMAPIin imports and__all__requirements/optional.txt- addedlitellm>=1.80,<1.87tests/test_litellm.py- 17 tests covering dispatch, credentials, gen_params translation, null responses, exception propagation, import guard, registration, async init, and live E2EIntegration details
drop_params=Truesilently drops provider-unsupported kwargs so the same config works across OpenAI, Anthropic, Gemini, etc.top_p,top_k,repetition_penaltyfrom gen_params before forwarding since these cause conflicts on certain providers (e.g. Anthropic rejectstemperature+top_ptogether). Users can override via**gen_params.RateLimitError,APIConnectionError,Timeout,InternalServerError,ServiceUnavailableError) using qualname-based matching so the module imports cleanly even without litellm installed.key='ENV'(default), noapi_keyis passed and litellm reads provider-specific env vars (OPENAI_API_KEY,ANTHROPIC_API_KEY, etc.) directly.Tests
Unit tests (17/17 pass):
Live E2E (Anthropic claude-sonnet-4-6 via Azure Foundry):
Risk / Compatibility
GPTAPI,ClaudeAPI, and all other providers are untouched.litellmis inrequirements/optional.txt- base install unaffected.''instead of crashing.Example usage