OpenAIEmbeddings class provides integration with OpenAI’s text embedding models.
Installation
Setup
Set your OpenAI API key:Usage
Basic usage
Embed single text
Embed multiple texts
Async usage
Configuration
Supported models
text-embedding-3-large- Latest and most capable model (3,072 dimensions)text-embedding-3-small- Smaller, faster model (1,536 dimensions)text-embedding-ada-002- Legacy model (1,536 dimensions)
Custom dimensions
Withtext-embedding-3 models, you can specify custom output dimensions:
Advanced options
OpenAI-compatible APIs
When using non-OpenAI providers (OpenRouter, Ollama, vLLM), setcheck_embedding_ctx_length=False:
Parameters
Name of OpenAI model to use.
Number of dimensions for output embeddings. Only supported in
text-embedding-3 models.OpenAI API key. Automatically inferred from
OPENAI_API_KEY environment variable if not provided.Base URL for API requests. Useful for proxy or service emulators.
Maximum number of texts to embed in each batch.
Maximum number of retries for API calls.
Timeout for requests to OpenAI API.
Whether to check token length and automatically split inputs. Set to
false for non-OpenAI providers.