{"id":2576,"library":"llama-index-embeddings-openai","title":"LlamaIndex OpenAI Embeddings","description":"This library provides an integration for LlamaIndex, a data framework for LLM applications, to utilize OpenAI's embedding models. It enables users to convert text into numerical vector representations (embeddings) using various OpenAI models. Currently at version 0.6.0, it follows the rapid development and release cadence of the broader LlamaIndex ecosystem.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/embeddings/llama-index-embeddings-openai","tags":["LlamaIndex","embeddings","OpenAI","LLM","integration","AI","NLP"],"install":[{"cmd":"pip install llama-index-embeddings-openai","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core LlamaIndex framework for setting up embedding models and indices.","package":"llama-index-core"},{"reason":"The underlying Python client for interacting with OpenAI's API.","package":"openai"}],"imports":[{"note":"As of LlamaIndex v0.9.x, provider integrations like OpenAIEmbeddings were moved to their own dedicated packages and are no longer re-exported from `llama_index.core`.","wrong":"from llama_index.core.embeddings.openai import OpenAIEmbedding","symbol":"OpenAIEmbedding","correct":"from llama_index.embeddings.openai import OpenAIEmbedding"},{"note":"Used for setting the global default embedding model.","symbol":"Settings","correct":"from llama_index.core import Settings"}],"quickstart":{"code":"import os\nfrom llama_index.core import Settings\nfrom llama_index.embeddings.openai import OpenAIEmbedding\n\n# Set your OpenAI API key as an environment variable\n# It's recommended to load this from a .env file in production\nos.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\", \"YOUR_OPENAI_API_KEY\")\n\n# Ensure the API key is set\nif not os.environ[\"OPENAI_API_KEY\"] or os.environ[\"OPENAI_API_KEY\"] == \"YOUR_OPENAI_API_KEY\":\n    raise ValueError(\"OPENAI_API_KEY environment variable not set. Please set it to your OpenAI API key.\")\n\n# Initialize the OpenAI Embedding model and set it as the global default\n# By default, uses 'text-embedding-ada-002'\nSettings.embed_model = OpenAIEmbedding(model=\"text-embedding-ada-002\")\n\n# Alternatively, create a local instance without setting it globally\nembed_model_local = OpenAIEmbedding(model=\"text-embedding-3-small\")\n\n# Get a single text embedding using the local instance\ntext = \"This is a test sentence for embedding with a local model.\"\nembedding = embed_model_local.get_text_embedding(text)\nprint(f\"Embedding length: {len(embedding)}\")\n# print(f\"First 5 elements of embedding: {embedding[:5]}...\")\n\n# Get embeddings for multiple texts using the global default\ntexts_list = [\"Hello world!\", \"LlamaIndex is great.\", \"OpenAI embeddings are powerful.\"]\nembeddings_for_list = Settings.embed_model.get_text_embeddings(texts_list)\nprint(f\"Number of embeddings for list: {len(embeddings_for_list)}\")\nfor i, emb in enumerate(embeddings_for_list):\n    print(f\"Embedding {i} length: {len(emb)}\")","lang":"python","description":"This quickstart demonstrates how to install the `llama-index-embeddings-openai` package, set your OpenAI API key, and initialize `OpenAIEmbedding` either globally via `Settings` or as a local instance to generate text embeddings. It shows how to get embeddings for both single and multiple text inputs."},"warnings":[{"fix":"Change import statements from `from llama_index.core.embeddings.openai import OpenAIEmbedding` to `from llama_index.embeddings.openai import OpenAIEmbedding`.","message":"Breaking Change (LlamaIndex v0.9.x): Embedding providers are no longer re-exported from `llama_index.core`. You must import `OpenAIEmbedding` directly from `llama_index.embeddings.openai`.","severity":"breaking","affected_versions":"llama-index-core>=0.9.0"},{"fix":"After importing `Settings` and `OpenAIEmbedding`, explicitly assign `Settings.embed_model = OpenAIEmbedding()`.","message":"Breaking Change (LlamaIndex v0.11.x): Default LLM and embedding models are no longer set automatically via `Settings`. You must explicitly set `Settings.embed_model`.","severity":"breaking","affected_versions":"llama-index-core>=0.11.0"},{"fix":"Set `OPENAI_API_KEY` as an environment variable (e.g., `export OPENAI_API_KEY='sk-...'`) or pass it explicitly during `OpenAIEmbedding` initialization if the underlying `openai` client is not configured globally. Environment variables take precedence.","message":"OpenAI API Key is mandatory and must be configured. Lack of a valid key will result in `APIConnectionError` or `AuthenticationError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade `llama-index-core` to a newer version or, if stuck on 0.10.6, try creating a new `OpenAIEmbedding` instance or investigate if a workaround involving `callback_manager` initialization is available for your specific `llama-index-core` patch version.","message":"Older `llama-index` core versions (e.g., 0.10.6) might encounter issues with `callback_manager` assignments leading to `ValueError` or crashes when using `OpenAIEmbedding`.","severity":"gotcha","affected_versions":"llama-index-core==0.10.6"},{"fix":"Review OpenAI's rate limits. The `OpenAIEmbedding` class has internal retry logic, but adjusting the `batch_size` parameter during initialization or implementing custom backoff strategies may be necessary for large-scale operations. Ensure stable internet connection.","message":"Rate limits and connection errors can occur due to frequent API calls or network issues, especially when processing many documents.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}