A chat application's model backend can run on the same computer as its Python code. LangChain can send prompts to a model in LM Studio through the server's OpenAI-compatible API.

The ChatOpenAI integration uses a configurable server address and model identifier. Its name does not require an OpenAI-hosted model, but the response must follow the OpenAI chat format; provider-specific extensions are outside this basic connection.

The connection assumes a running LM Studio HTTP server, a loaded chat model, and Python 3.11 or newer with a project virtual environment. Python runs on the same machine as LM Studio, so the loopback address keeps requests local without opening the server to other computers.

Steps to connect LangChain to LM Studio:

  1. Check the identifier of your loaded LM Studio chat model. Example output:
    $ lms ps
    IDENTIFIER    MODEL                    STATUS    SIZE         CONTEXT    PARALLEL    LOAD CONFIG    DEVICE    TTL
    local-chat    qwen2.5-0.5b-instruct    IDLE      397.81 MB    2048       4           LM Studio      Local     60m / 1h

    local-chat is the loaded instance identifier used in the Python code. An absent model needs downloading and loading before it can answer prompts. The lms command must be on your shell's PATH.
    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

  2. Confirm that LM Studio's HTTP server is running on port 1234. Example output:
    $ lms server status
    The server is running on port 1234.

    The local server setup uses 127.0.0.1 as its bind address. A stopped server cannot accept the client request.
    Related: How to run LM Studio headlessly with llmster

  3. Activate your project's Python virtual environment from the project directory.
    $ source .venv/bin/activate

    The activation command uses Bash or Zsh. The environment directory is .venv; a different environment path needs its own activation script.
    Related: How to create a virtual environment in Python

  4. Install LangChain's OpenAI integration into the active environment.
    $ python -m pip install langchain-openai

    The package includes the ChatOpenAI client and installs its compatible langchain-core and openai dependencies. A hosted OpenAI account is not required for this local connection.
    Related: LangChain ChatOpenAI documentation

  5. Create local_chat.py in the project directory with the following client definition using your text editor.
    local_chat.py
    from langchain_openai import ChatOpenAI
     
    chat = ChatOpenAI(
        base_url="http://127.0.0.1:1234/v1",
        api_key="lm-studio",
        model="local-chat",
        max_completion_tokens=64,
    )

    base_url ends in /v1 because the client adds the chat-completions path. The model value must match your loaded identifier from lms ps in place of local-chat. The reply limit keeps this short request to at most 64 generated tokens.
    Related: LM Studio OpenAI-compatible endpoints

    lm-studio is a placeholder required by the client for a local server with authentication disabled by default. It is not a hosted API key or an LM Studio token. An existing server with token authentication enabled needs its real token supplied securely; disabling authentication is unnecessary.

  6. Append the prompt invocation and text output after the client definition in local_chat.py.
    local_chat.py
    reply = chat.invoke(
        "Say hello in one short sentence."
    )
    print(reply.text)

    invoke sends the prompt to LM Studio and returns a LangChain message. Its text property contains the generated answer.

  7. Save local_chat.py in your text editor.
  8. Connect LangChain to LM Studio by running local_chat.py. Example output:
    $ python local_chat.py
    Hello! How can I help you today?

    The generated wording varies. A text reply confirms that the client reached the configured server and received model output. A connection error points to the address, port, or server state; a model error points to the identifier or loaded model.

Share this guide