Local language models can supply the text-generation part of a retrieval application. LlamaIndex can connect to LM Studio through its OpenAI-compatible API, so an LLM call runs against a model on the same machine.
LlamaIndex's OpenAILike adapter accepts a custom server address and model identifier. Its chat-model setting routes a completion call through the chat endpoint, while the context-window setting tells LlamaIndex how much text the loaded instance can accept. Embedding models remain a separate choice for applications that index documents.
The Python client and LM Studio server run on the same machine, with a chat model already loaded and the HTTP server listening on loopback. The Python commands use a POSIX shell in your project directory and require Python 3.10 or newer. The local server is assumed to have its default unauthenticated configuration; a placeholder key does not grant access to a server that requires an API token.
Steps to connect LlamaIndex to LM Studio:
- Check your loaded chat model's API identifier.
$ lms ps
The IDENTIFIER column names the loaded instance. The model-load guide assigns local-chat to a CPU-loaded Qwen2.5 0.5B Instruct instance with a 2048-token context. A missing instance needs loading before the Python call. An embedding model cannot answer a chat prompt.
- Create a Python virtual environment for the client dependencies.
$ python3 -m venv .venv
If your project already has a virtual environment, that environment can be used instead. On Ubuntu or Debian, a missing ensurepip message means the matching python3-venv package is needed.
Related: Create a Python virtual environment - Activate the project's virtual environment.
$ source .venv/bin/activate
- Install LlamaIndex's OpenAI-compatible LLM integration.
$ python -m pip install llama-index-llms-openai-like
This package installs llama-index-core and the adapter's dependencies. The full llama-index starter package is unnecessary for a standalone LLM call.
- Create a file named lmstudio_llamaindex.py in your project using your text editor.
from llama_index.llms.openai_like import OpenAILike
- Append the local model configuration below the import in lmstudio_llamaindex.py.
llm = OpenAILike( model="local-chat", api_base="http://127.0.0.1:1234/v1", api_key="lm-studio", is_chat_model=True, context_window=2048, max_tokens=64, )
model must match your loaded identifier from lms ps; context_window must match the context configured for that instance. api_base includes /v1, without appending /chat/completions. is_chat_model=True selects that chat endpoint even when using complete(). max_tokens limits the generated reply. The lm-studio key is only a nonempty placeholder required by the client library for this unauthenticated loopback server. It is not an OpenAI credential. A loopback listener keeps requests on this machine; exposing an unauthenticated listener to the network would let other machines use the API.
Related: LlamaIndex OpenAILike reference
Related: LM Studio OpenAI-compatible endpoints - Append a completion call and its text output at the end of lmstudio_llamaindex.py.
response = llm.complete("What is 2 + 2? Answer briefly.") print(response.text)
complete() returns a LlamaIndex completion response. Its text field contains the generated reply from the selected local model.
- Save lmstudio_llamaindex.py in your text editor.
- Connect LlamaIndex to your LM Studio model by running the script. Example output:
$ python lmstudio_llamaindex.py 2 + 2 equals 4.
The wording varies by model and generation settings. A text reply confirms that the LlamaIndex call reached the local API and received a completion. A connection error means the server address or running state needs checking; a model error needs the identifier checked against lms ps. Creating the client object alone does not contact LM Studio.