Skip to main content
Embeddings are handled by the LiteLLMEmbeddings class, which provides unified access to 100+ embedding providers through the LiteLLM library. This includes OpenAI, VoyageAI, Cohere, AWS Bedrock, Azure, and many more.

Why LiteLLM?

LiteLLM provides several advantages over using provider-specific implementations:
  • Unified API: Single interface for all providers
  • Robust Error Handling: Built-in retry logic and rate limiting
  • No Provider Bugs: Avoids provider-specific issues like VoyageAI’s circular import bug
  • Easy Switching: Change providers without code changes
  • Future-Proof: LiteLLM team maintains provider integrations

Installation

Embeddings require the litellm and tiktoken libraries. The tokenizers library is also recommended for better tokenizer support across providers.

Supported Providers

LiteLLM supports 100+ providers. Here are some popular ones: For a complete list, see the LiteLLM documentation.

Usage

Basic Usage

Using VoyageAI

Using Cohere

Using AWS Bedrock

Configuration Options

Using with AutoEmbeddings

Using with Pipeline

API Key Configuration

LiteLLM automatically looks for API keys in environment variables. The variable name depends on the provider:
  • OpenAI: OPENAI_API_KEY
  • VoyageAI: VOYAGE_API_KEY
  • Cohere: COHERE_API_KEY or CO_API_KEY
  • AWS Bedrock: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  • Azure: AZURE_API_KEY and AZURE_API_BASE
You can also pass the API key directly:

Error Handling

LiteLLM provides robust error handling with automatic retries:

Tokenizer Support

LiteLLMEmbeddings automatically selects the appropriate tokenizer based on the provider:
  • OpenAI models: Uses tiktoken
  • VoyageAI models: Attempts to load HuggingFace tokenizer, falls back to tiktoken
  • Other models: Uses cl100k_base from tiktoken as fallback

Performance Tips

  1. Batch Processing: Use embed_batch() for multiple texts to reduce API calls
  2. Adjust Batch Size: Tune batch_size based on your text lengths and provider limits
  3. Connection Pooling: LiteLLM handles connection pooling automatically
  4. Async Support: LiteLLM supports async operations (may be added in future versions)

Comparison with Provider-Specific Classes

Common Use Cases

Migrating from VoyageAI to avoid circular import

Multi-provider experimentation

Cost optimization

Troubleshooting

Import Error

Solution: Install the litellm extra:

API Key Not Found

Solution: Set the appropriate environment variable or pass api_key parameter.

Dimension Detection Failed

Solution: Provide dimension explicitly:

Example: Complete RAG Pipeline