Registry / llm-agents / llama-index-llms-openai

llama-index-llms-openai

JSON →
library0.7.10pypypi✓ verified 26d ago

This package provides the OpenAI Large Language Model (LLM) integration for LlamaIndex (version 0.7.5). LlamaIndex is a data framework designed to connect LLMs with your private or domain-specific data, enabling applications like RAG (Retrieval Augmented Generation). This integration allows LlamaIndex users to leverage various OpenAI models for text completion, chat generation, streaming responses, and structured outputs within their LlamaIndex applications. The library is actively maintained and releases are tied to the broader LlamaIndex ecosystem updates.

pip install llama-index-llms-openai
INSTALL
IMPORT
SIG · LLAMA-INDEX-LLMS-O
L
llama-index-llms-openai
llm-agentspythonv0.7.10
Install
20.4s avg
Import
8110ms
Disk
270MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.10 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 6.726s · 258.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 20.4s · import 6.250s · 255MB
270MB installed
● package 270MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

OpenAI
from llama_index.llms.openai import OpenAI
from llamaindex import OpenAI
Beginning with LlamaIndex 0.9.x, direct imports from specific integration packages are required due to modularization. The older pattern (`from llamaindex import OpenAI`) is no longer supported for new versions.
ChatMessage
from llama_index.core.llms import ChatMessage
ChatMessage is part of the core LlamaIndex LLM abstractions.

This quickstart demonstrates how to initialize the OpenAI LLM and perform basic text completion and chat interactions. It assumes your OpenAI API key is set as an environment variable (`OPENAI_API_KEY`). You can explicitly pass the `api_key` argument during initialization if preferred. It also shows how to use ChatMessage from `llama_index.core.llms` for chat interactions.

import os from llama_index.llms.openai import OpenAI # Ensure your OpenAI API key is set as an environment variable (OPENAI_API_KEY) # If not, you can pass it directly to the OpenAI constructor: # llm = OpenAI(api_key="your_api_key_here", model="gpt-3.5-turbo") openai_api_key = os.environ.get('OPENAI_API_KEY', 'YOUR_OPENAI_API_KEY') if openai_api_key == 'YOUR_OPENAI_API_KEY': print("WARNING: Please set the OPENAI_API_KEY environment variable or replace 'YOUR_OPENAI_API_KEY' with your actual key.") llm = OpenAI( model="gpt-3.5-turbo", api_key=openai_api_key # Uses env var if set, otherwise the placeholder ) # Example: Generate a completion resp = llm.complete("Tell me a short story about a brave knight.") print(f"Completion: {resp}") # Example: Send a chat message from llama_index.core.llms import ChatMessage messages = [ ChatMessage(role="system", content="You are a helpful assistant."), ChatMessage(role="user", content="What is the capital of Canada?") ] chat_resp = llm.chat(messages) print(f"Chat Response: {chat_resp.content}")
Debug
Known issues
breakingWith LlamaIndex v0.9.x and newer, direct imports from the root `llamaindex` package for LLMs and other integrations have been removed due to modularization. You must now import `OpenAI` directly from `llama_index.llms.openai`.
fix
Update your import statements from `from llamaindex import OpenAI` to `from llama_index.llms.openai import OpenAI`.
affects: LlamaIndex >= 0.9.0
breakingThe `ServiceContext` object has been deprecated in LlamaIndex v0.10.x and completely removed in v0.11.x. Global LLM and embedding configurations are now managed through the `Settings` object, and explicit setting of LLMs (like OpenAI) is required.
fix
Migrate from `ServiceContext` to `Settings`. Instead of `service_context = ServiceContext.from_defaults(llm=OpenAI())`, use `from llama_index.core import Settings; Settings.llm = OpenAI()`. Explicitly set your LLM and embedding model.
affects: LlamaIndex >= 0.10.0
gotchaThe OpenAI LLM client, by default, expects your OpenAI API key to be set as an environment variable named `OPENAI_API_KEY`. If this variable is not set, you will encounter authentication errors.
fix
Set the `OPENAI_API_KEY` environment variable before running your application (e.g., `export OPENAI_API_KEY='sk-...'`). Alternatively, pass the API key directly to the `OpenAI` constructor: `llm = OpenAI(api_key='sk-...', model='gpt-3.5-turbo')`.
affects: All
gotchaWhile `pip install llama-index` includes a starter bundle with `llama-index-core` and `llama-index-llms-openai`, if you're installing only `llama-index-llms-openai` directly, ensure `llama-index-core` is also installed, as it provides fundamental LlamaIndex abstractions.
fix
If not installing the full `llama-index` package, explicitly install `pip install llama-index-core llama-index-llms-openai`.
affects: All
gotchaAlthough a default model (often `gpt-3.5-turbo`) might be used, it is best practice to explicitly specify the `model` parameter when initializing `OpenAI` to ensure consistency and control, especially when using newer models or specific capabilities.
fix
Initialize the LLM with the desired model, e.g., `llm = OpenAI(model="gpt-4o")` or `llm = OpenAI(model="gpt-3.5-turbo")`.
affects: All
breakingLlamaIndex uses Python's `|` operator for type hinting unions (PEP 604). This syntax requires Python 3.10 or newer. Running LlamaIndex on older Python versions (e.g., Python 3.9) will result in a `TypeError: unsupported operand type(s) for |`.
fix
Upgrade your Python environment to version 3.10 or newer. If you must use an older Python version (e.g., 3.9), you will need to downgrade your LlamaIndex installation to a version compatible with Python 3.9.
affects: LlamaIndex >= 0.10.0, Python < 3.10
Errors
Common errors & fixes
ValueError: No API key found for OpenAI. Please set either the OPENAI_API_KEY environment variable or openai.api_key prior to initialization.
The OpenAI API key is not configured in your environment or directly in the code, which is essential for authenticating requests to OpenAI models.
fix
Set the `OPENAI_API_KEY` environment variable (e.g., `os.environ["OPENAI_API_KEY"] = "sk-..."`) or pass the `api_key` directly to the `OpenAI` class constructor when initializing the LLM.
Retrying llama_index.llms.openai.base.OpenAI._chat in xxx seconds as it raised APIConnectionError: Connection error…
The application is unable to establish a connection with the OpenAI API endpoint, which can be due to network issues, an incorrect API base URL, firewall restrictions, or temporary OpenAI service outages.
fix
Verify your internet connection, check if the `api_base` URL (if customized for proxies or local models) is correct and accessible, ensure no firewall is blocking access, and confirm the OpenAI service status. If using Azure OpenAI, ensure `api_type`, `api_version`, and `api_base` are correctly set.
ModuleNotFoundError: No module named 'llama_index.llms.openai.base'
The `llama-index-llms-openai` package is either not installed, or there's a version mismatch with `llama-index-core` or `llama-index` itself, leading Python to fail in finding the specified module due to structural changes in the library.
fix
Ensure `llama-index-llms-openai` is installed (`pip install llama-index-llms-openai`). If it's already installed, try upgrading both `llama-index` and `llama-index-llms-openai` to compatible versions (`pip install --upgrade llama-index llama-index-llms-openai`). Restart your Python environment after installation.
ImportError: cannot import name 'LLM' from 'llama_index.llms.openai'
This error occurs because the `LLM` base abstraction was moved or renamed in LlamaIndex, or the expected import path has changed from `llama_index.llms.openai` to a more generalized location, typically `llama_index.core.llms`.
fix
Update your import statement to use `from llama_index.core.llms import LLM` if you need the base LLM class, or directly import the concrete `OpenAI` class using `from llama_index.llms.openai import OpenAI`.
Upgrade
Version history
0.7.10latest on PyPI · released Jul 21, 2026
Audit
Dependencies
llama-index-corerequiredThis package is an integration for the core LlamaIndex framework. 'llama-index-core' provides the fundamental abstractions.
openairequiredThe underlying Python client library for interacting with OpenAI's API.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
llama-index-llms-openai — pip install llama-index-llms-openai · libregistry