A chat pipeline can keep text generation on the same machine as its application. Haystack's OpenAIChatGenerator connects to LM Studio through its OpenAI-compatible API to return replies from a local language model.
The generator accepts Haystack chat messages and returns chat-message objects containing the model's reply. Its base URL selects the server, while the model identifier selects the model exposed by that server.
Python 3.11 or newer and a running LM Studio server with a loaded chat model are required. A loopback server without token authentication needs no hosted API account, although the client library still requires a placeholder key.
Steps to connect Haystack to LM Studio:
- Create a Python virtual environment in your project directory.
$ python3 -m venv .venv
On Ubuntu or Debian, the matching python3-venv package is required if Python reports that ensurepip is unavailable.
Related: How to create a virtual environment in Python - Activate the virtual environment in your POSIX shell.
$ . .venv/bin/activate
- Install Haystack into the active environment.
$ python -m pip install haystack-ai
The distribution is named haystack-ai, and its import package is haystack. OpenAIChatGenerator is included; a separate LM Studio integration package is unnecessary.
- List the model identifiers exposed by your local LM Studio server.
$ curl -fsS http://127.0.0.1:1234/v1/models { "data": [ { "id": "local-chat", "object": "model", "owned_by": "organization_owner" }, { "id": "qwen2.5-0.5b-instruct", "object": "model", "owned_by": "organization_owner" }, { "id": "text-embedding-nomic-embed-text-v1.5", "object": "model", "owned_by": "organization_owner" } ], "object": "list" }The server and Python application run on the same machine. The address 127.0.0.1 keeps requests on that machine; the server should also bind to loopback. The identifier local-chat comes from loading a model with that name. A model list can include unloaded downloads when just-in-time loading is enabled, so a list response alone does not prove that generation works.
Related: How to run LM Studio headlessly with llmster
Related: How to download a model with lms in LM Studio
Related: How to load a model with lms in LM Studio - Create haystack_chat.py in your text editor with the imports and input message below.
- haystack_chat.py
from haystack.components.generators.chat import OpenAIChatGenerator from haystack.dataclasses import ChatMessage from haystack.utils import Secret messages = [ ChatMessage.from_user("What is 2 + 2? Answer in one short sentence.") ]
- Connect Haystack to your local server by appending this configuration after the messages list in haystack_chat.py with your model identifier in place of local-chat.
generator = OpenAIChatGenerator( model="local-chat", api_base_url="http://127.0.0.1:1234/v1", api_key=Secret.from_token("lm-studio"), generation_kwargs={"max_tokens": 64}, )
api_base_url includes /v1; the client adds /chat/completions. The explicit local URL replaces the hosted default. lm-studio is only a placeholder for this unauthenticated loopback server, not a hosted credential. If your server requires authentication, Secret.from_env_var(“LM_API_TOKEN”) can read its token from an environment variable instead. Authentication should stay enabled on an existing protected server. max_tokens limits the reply length.
Related: Haystack OpenAIChatGenerator documentation
Related: LM Studio OpenAI-compatible API - Append the request and reply handling below the generator configuration in haystack_chat.py.
result = generator.run(messages=messages) reply = result["replies"][0] print(f"Model: {reply.meta['model']}") print(reply.text)
ChatMessage.from_user supplies the user message. The response's replies list contains ChatMessage objects, with generated text in .text and response metadata in .meta.
- Save haystack_chat.py in the project directory.
- Confirm that Haystack returns a reply from your selected local model by running haystack_chat.py. Example output:
$ python haystack_chat.py Model: local-chat 2 + 2 equals 4.
The model name should match the identifier selected above, and the answer's wording varies. A connection error means the local server is unreachable; a model error means the identifier or model availability needs attention. A printed model name together with generated text confirms that the Haystack call received a chat completion.