Principle:AI Northstar Tech Vector io Multi Backend Embedding Fallback
| Knowledge Sources | |
|---|---|
| Domains | Embedding, Multi_Backend, Fallback, Re_embedding |
| Last Updated | 2026-10-15 00:00 GMT |
Overview
A resilience pattern that chains multiple embedding generation backends with automatic fallback, selecting the appropriate backend based on model name, hardware availability, and error signals.
Description
Multi-Backend Embedding Fallback addresses the problem that embedding models are served by different backends:
- API-based models (OpenAI text-embedding-3-small, Cohere, Azure) are accessed via LiteLLM, which provides a unified API for 100+ providers.
- Local HuggingFace models (e.g. TaylorAI/bge-micro-v2) are loaded via Sentence Transformers and run locally on CPU or GPU.
- Apple Silicon models can use MLX, a framework optimized for Apple's Neural Engine.
The pattern chains these backends in priority order and falls back on errors:
- LiteLLM (default): Attempts the litellm.embedding() call. If the model name starts with huggingface/ or LiteLLM raises a BadRequestError indicating the model provider is unknown, falls back to Sentence Transformers.
- Sentence Transformers: Loads the model via SentenceTransformer with trust_remote_code=True. Uses CUDA if available, otherwise CPU.
- MLX (Apple Silicon only): On arm64 machines without CUDA and when --disable_mlx is not set, uses mlx_embedding_models.EmbeddingModel.from_registry() for Apple-optimized inference.
Once the system falls back to Sentence Transformers, a global flag (use_sentence_transformers) is set to avoid repeated LiteLLM failures on subsequent batches.
The LiteLLM path is wrapped with tenacity retry logic: random exponential backoff (max 10 seconds), up to 5 attempts, retrying on any exception. This handles transient API errors (rate limits, network issues).
Quantization is supported via sentence_transformers.quantize_embeddings when the --quantize option is set to a value other than float32 (e.g. int8, binary, uint8).
Usage
Use this principle when designing systems that need to generate embeddings from multiple model providers with different runtime requirements. It is suited for:
- Tools that support both API-based and local embedding models
- Systems that need to work across different hardware (CPU, GPU, Apple Silicon)
- Applications where embedding generation should be resilient to backend failures
Theoretical Basis
The fallback chain:
# Abstract algorithm (NOT real implementation)
@retry(max_attempts=5, backoff=random_exponential(max=10))
def generate_embeddings(args, batch_text):
if model_name.startswith("huggingface/"):
return sentence_transformers_backend(args, batch_text)
if already_switched_to_st:
return sentence_transformers_backend(args, batch_text)
try:
return litellm.embedding(model=model_name, input=batch_text)
except BadRequestError as e:
if "LLM Provider" in str(e):
switch_to_sentence_transformers() # Set global flag
return sentence_transformers_backend(args, batch_text)
raise
def sentence_transformers_backend(args, batch_text):
if is_apple_silicon() and not has_cuda() and not args.disable_mlx:
return mlx_backend(args, batch_text)
else:
model = SentenceTransformer(model_name, device="cuda" if has_cuda() else "cpu")
return model.encode(batch_text)