LlamaIndex with an OpenAI-Compatible API: OpenAILike Setup
How do you use LlamaIndex with an OpenAI-compatible API?
Use the OpenAILike class, not OpenAI. Install llama-index-llms-openai-like, pass your endpoint as api_base, your model ID as model, and set three fields LlamaIndex cannot discover on its own: is_chat_model=True, the model's real context_window, and is_function_calling_model if the model supports tools. For embeddings, use OpenAILikeEmbedding the same way.
# pip install llama-index-llms-openai-like
import os
from llama_index.core import Settings
from llama_index.llms.openai_like import OpenAILike
Settings.llm = OpenAILike(
model="your-model-id", # exact ID from GET /v1/models
api_base="https://your-endpoint/v1", # include /v1
api_key=os.environ["LLM_API_KEY"],
is_chat_model=True, # default False sends /v1/completions
context_window=128000, # the model's real window
is_function_calling_model=True, # only if the model supports tools
timeout=120.0,
)
print(Settings.llm.complete("Reply with OK"))This guide explains why the plain OpenAI class fails, what each OpenAILike field does, how to set up embeddings for RAG, and how to fix the usual errors. It was checked against the LlamaIndex API reference and the openai-like package source on 30 November 2026.
Why the plain OpenAI class fails
llama_index.llms.openai.OpenAI also accepts api_base, so it looks like the obvious choice. But it looks up the model name in a built-in table of official OpenAI models to get the context window and capabilities. Any other ID raises:
ValueError: Unknown model 'your-model-id'. Please provide a valid OpenAI model name in: ...The error is raised in your own process, not by the server, and it often appears only when you call complete() or chat(), because that is when LlamaIndex reads the model metadata. OpenAILike skips the lookup and uses the values you pass. LlamaIndex's own documentation describes it as a thin wrapper that makes the OpenAI integration work with third-party OpenAI-compatible APIs.
The OpenAILike fields that matter
|
Field |
Default |
What to set |
|---|---|---|
|
|
OpenAI default model |
The exact ID your server lists at |
|
|
|
Your endpoint, including |
|
|
from |
Your key; any placeholder for local servers without auth |
|
|
|
|
|
|
3,900 |
The model's real context length |
|
|
|
|
|
|
None |
Output cap, sent as |
|
|
60 seconds |
Raise for long generations or slow local hardware |
|
|
None |
Extra request-body fields, such as provider-specific options |
Three defaults cause most problems:
is_chat_model=Falsemakes LlamaIndex call the legacyPOST /v1/completionsroute and flatten your chat into one prompt. Many servers do not serve that route at all, so you get a 404.context_window=3900makes query engines pack only about 3,900 tokens of retrieved text and history, even if the model handles far more. Answers look shallow, and synthesis splits into extra refine calls.is_function_calling_model=Falsestops function-calling agents.FunctionAgentraises an error that the LLM must be a function-calling LLM, and some workflows fall back to text-based ReAct.
Watch your environment variables. LlamaIndex falls back to OPENAI_API_BASE and OPENAI_API_KEY, not OPENAI_BASE_URL. A variable left over from another tool can send calls to the wrong place, so pass api_base and api_key explicitly.
Embeddings for RAG with OpenAILikeEmbedding
Setting Settings.llm does not change embeddings. Unless you set Settings.embed_model, VectorStoreIndex still embeds with OpenAI's default embedding model, which needs an OpenAI key and sends your documents to OpenAI.
# pip install llama-index-embeddings-openai-like
from llama_index.embeddings.openai_like import OpenAILikeEmbedding
Settings.embed_model = OpenAILikeEmbedding(
model_name="your-embedding-model-id", # note: model_name, not model
api_base="https://your-endpoint/v1",
api_key=os.environ["LLM_API_KEY"],
embed_batch_size=10,
)Points to check:
- The class takes
model_name. Passingmodelraises aValueError. /v1/embeddingsworks only with embedding models. A chat model ID fails.- Do not mix vectors from different embedding models in one index. If you change the model, re-embed.
- If vectors look wrong or the call fails on encoding, the server may not support the base64 format the OpenAI client requests by default; check the provider's embeddings notes.
A minimal end-to-end RAG example
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
docs = SimpleDirectoryReader("./data").load_data()
index = VectorStoreIndex.from_documents(docs) # uses Settings.embed_model
engine = index.as_query_engine(similarity_top_k=4) # uses Settings.llm
print(engine.query("Summarise the refund policy."))With both Settings.llm and Settings.embed_model pointed at compatible endpoints, nothing in this pipeline calls OpenAI. You can also pass llm= or embed_model= to a single component instead of setting them globally.
Local servers: Ollama, LM Studio, vLLM
The same pattern works for local OpenAI-compatible servers:
|
Server |
|
Notes |
|---|---|---|
|
LM Studio |
|
Model ID from |
|
Ollama |
|
Any key; LlamaIndex also has a native Ollama integration |
|
vLLM |
|
Key only if you started vLLM with one |
Set context_window to what the server actually loaded, not the model's theoretical maximum; local servers often load a model with a smaller context to save memory. For more on running these engines, see self-hosted OpenAI-compatible APIs.
Troubleshooting
|
Error or symptom |
Cause |
Fix |
|---|---|---|
|
|
Using |
Switch to |
|
404 on every call |
|
Set |
|
Calls go to OpenAI unexpectedly |
|
Set the embed model; pass |
|
"LLM must be a FunctionCallingLLM" |
|
Set |
|
Short, shallow RAG answers |
|
Set the real window |
|
|
Passed |
Use |
|
Timeouts on long answers |
60-second default |
Raise |
If a request works with curl but not through LlamaIndex, compare the body it sends; our guide to OpenAI-compatible base URLs covers the path mistakes that cause most 404s, and OpenAI compatibility issues lists fields servers ignore.
FAQ
Which LlamaIndex class should I use for an OpenAI-compatible API?
OpenAILike from llama-index-llms-openai-like for the LLM, and OpenAILikeEmbedding from llama-index-embeddings-openai-like for embeddings.
Why does LlamaIndex say my model is unknown?
You are using the OpenAI class, which only accepts official OpenAI model names. OpenAILike accepts any ID.
Do I need is_chat_model=True?
For almost all current models, yes. Without it LlamaIndex calls the legacy completions route, which many servers do not provide.
Can LlamaIndex use a local model through LM Studio or Ollama?
Yes. Point api_base at the local server's /v1 URL, use the server's model ID, and pass a placeholder key if the server has no authentication.
How do I stop LlamaIndex from sending data to OpenAI?
Set both Settings.llm and Settings.embed_model to compatible endpoints. If only the LLM is set, embeddings still default to OpenAI.
Summary
To use LlamaIndex with any OpenAI-compatible API, swap OpenAI for OpenAILike, include /v1 in api_base, and set is_chat_model, context_window and is_function_calling_model yourself. Add OpenAILikeEmbedding so RAG embeddings use the same provider. Those few settings fix nearly every error people hit when moving LlamaIndex off OpenAI.