llama-index-llms-openai 0.7.5


pip install llama-index-llms-openai

  Latest version

Released: Mar 30, 2026

Project Links

Meta
Author: llama-index
Requires Python: <4.0,>=3.10

Classifiers

LlamaIndex Llms Integration: Openai

Installation

To install the required package, run:

%pip install llama-index-llms-openai

Setup

  1. Set your OpenAI API key as an environment variable. You can replace "sk-..." with your actual API key:
import os

os.environ["OPENAI_API_KEY"] = "sk-..."

Basic Usage

Generate Completions

To generate a completion for a prompt, use the complete method:

from llama_index.llms.openai import OpenAI

resp = OpenAI().complete("Paul Graham is ")
print(resp)

Chat Responses

To send a chat message and receive a response, create a list of ChatMessage instances and use the chat method:

from llama_index.core.llms import ChatMessage

messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality."
    ),
    ChatMessage(role="user", content="What is your name?"),
]
resp = OpenAI().chat(messages)
print(resp)

Streaming Responses

Stream Complete

To stream responses for a prompt, use the stream_complete method:

from llama_index.llms.openai import OpenAI

llm = OpenAI()
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
    print(r.delta, end="")

Stream Chat

To stream chat responses, use the stream_chat method:

from llama_index.llms.openai import OpenAI
from llama_index.core.llms import ChatMessage

llm = OpenAI()
messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality."
    ),
    ChatMessage(role="user", content="What is your name?"),
]
resp = llm.stream_chat(messages)
for r in resp:
    print(r.delta, end="")

Configure Model

You can specify a particular model when creating the OpenAI instance:

llm = OpenAI(model="gpt-3.5-turbo")
resp = llm.complete("Paul Graham is ")
print(resp)

messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality."
    ),
    ChatMessage(role="user", content="What is your name?"),
]
resp = llm.chat(messages)
print(resp)

Asynchronous Usage

You can also use asynchronous methods for completion:

from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-3.5-turbo")
resp = await llm.acomplete("Paul Graham is ")
print(resp)

Set API Key at a Per-Instance Level

If desired, you can have separate LLM instances use different API keys:

from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-3.5-turbo", api_key="BAD_KEY")
resp = OpenAI().complete("Paul Graham is ")
print(resp)

LLM Implementation example

https://docs.llamaindex.ai/en/stable/examples/llm/openai/

0.7.5 Mar 30, 2026
0.7.4 Mar 27, 2026
0.7.3 Mar 20, 2026
0.7.2 Mar 13, 2026
0.7.1 Mar 13, 2026
0.7.0 Mar 12, 2026
0.6.26 Mar 05, 2026
0.6.25 Mar 03, 2026
0.6.24 Mar 03, 2026
0.6.23 Mar 01, 2026
0.6.22 Mar 01, 2026
0.6.21 Feb 26, 2026
0.6.20 Feb 26, 2026
0.6.19 Feb 20, 2026
0.6.18 Feb 06, 2026
0.6.17 Feb 03, 2026
0.6.16 Feb 02, 2026
0.6.15 Jan 26, 2026
0.6.14 Jan 26, 2026
0.6.13 Jan 13, 2026
0.6.12 Dec 16, 2025
0.6.11 Dec 12, 2025
0.6.10 Nov 27, 2025
0.6.9 Nov 14, 2025
0.6.8 Nov 10, 2025
0.6.7 Nov 03, 2025
0.6.6 Oct 27, 2025
0.6.5 Oct 15, 2025
0.6.4 Oct 07, 2025
0.6.3 Oct 07, 2025
0.6.2 Oct 06, 2025
0.6.1 Sep 25, 2025
0.6.0 Sep 24, 2025
0.5.6 Sep 08, 2025
0.5.5 Sep 08, 2025
0.5.4 Aug 16, 2025
0.5.3 Aug 12, 2025
0.5.2 Aug 08, 2025
0.5.1 Aug 07, 2025
0.5.0 Jul 30, 2025
0.4.7 Jun 16, 2025
0.4.6 Jun 14, 2025
0.4.5 Jun 10, 2025
0.4.4 Jun 07, 2025
0.4.3 Jun 04, 2025
0.4.2 Jun 03, 2025
0.4.1 Jun 02, 2025
0.4.0 May 30, 2025
0.3.44 May 23, 2025
0.3.43 May 22, 2025
0.3.42 May 19, 2025
0.3.41 May 19, 2025
0.3.40 May 15, 2025
0.3.39 May 15, 2025
0.3.38 Apr 21, 2025
0.3.37 Apr 16, 2025
0.3.36 Apr 16, 2025
0.3.35 Apr 14, 2025
0.3.34 Apr 14, 2025
0.3.33 Apr 10, 2025
0.3.32 Apr 09, 2025
0.3.31 Apr 08, 2025
0.3.30 Apr 02, 2025
0.3.29 Mar 28, 2025
0.3.28 Mar 27, 2025
0.3.27 Mar 24, 2025
0.3.26 Mar 20, 2025
0.3.25 Feb 27, 2025
0.3.24 Feb 27, 2025
0.3.23 Feb 27, 2025
0.3.22 Feb 27, 2025
0.3.21 Feb 27, 2025
0.3.20 Feb 17, 2025
0.3.19 Feb 12, 2025
0.3.18 Feb 06, 2025
0.3.17 Feb 03, 2025
0.3.16 Feb 01, 2025
0.3.15 Jan 31, 2025
0.3.14 Jan 22, 2025
0.3.13 Jan 08, 2025
0.3.12 Dec 19, 2024
0.3.11 Dec 18, 2024
0.3.10 Dec 11, 2024
0.3.9 Dec 11, 2024
0.3.8 Dec 11, 2024
0.3.7 Dec 11, 2024
0.3.6 Dec 11, 2024
0.3.5 Dec 11, 2024
0.3.4 Dec 10, 2024
0.3.3 Dec 08, 2024
0.3.2 Nov 25, 2024
0.3.1 Nov 20, 2024
0.3.0 Nov 18, 2024
0.2.16 Oct 22, 2024
0.2.15 Oct 16, 2024
0.2.14 Oct 15, 2024
0.2.13 Oct 09, 2024
0.2.12 Oct 07, 2024
0.2.11 Oct 03, 2024
0.2.10 Oct 02, 2024
0.2.9 Sep 18, 2024
0.2.8 Sep 17, 2024
0.2.7 Sep 14, 2024
0.2.6 Sep 13, 2024
0.2.5 Sep 12, 2024
0.2.4 Sep 12, 2024
0.2.3 Sep 07, 2024
0.2.2 Sep 04, 2024
0.2.1 Sep 02, 2024
0.2.0 Aug 22, 2024
0.1.31 Aug 21, 2024
0.1.30 Aug 20, 2024
0.1.29 Aug 07, 2024
0.1.28 Aug 06, 2024
0.1.27 Jul 23, 2024
0.1.26 Jul 18, 2024
0.1.25 Jul 03, 2024
0.1.24 Jun 28, 2024
0.1.23 Jun 25, 2024
0.1.22 Jun 03, 2024
0.1.21 May 24, 2024
0.1.20 May 20, 2024
0.1.19 May 13, 2024
0.1.18 May 08, 2024
0.1.17 May 07, 2024
0.1.16 Apr 19, 2024
0.1.15 Apr 09, 2024
0.1.14 Mar 29, 2024
0.1.13 Mar 26, 2024
0.1.12 Mar 16, 2024
0.1.11 Mar 15, 2024
0.1.10 Mar 15, 2024
0.1.9 Mar 13, 2024
0.1.8 Mar 12, 2024
0.1.7 Feb 29, 2024
0.1.6 Feb 21, 2024
0.1.5 Feb 20, 2024
0.1.4 Feb 20, 2024
0.1.3 Feb 20, 2024
0.1.2 Feb 17, 2024
0.1.1 Feb 12, 2024
0.1.0 Feb 10, 2024
0.0.1 Feb 02, 2024
Extras: None
Dependencies:
llama-index-core (<0.15,>=0.14.5)
openai (<3,>=1.108.1)