Stateful applications need a way to pass data between model calls and other Python functions. LangGraph provides that state and execution order, while LM Studio generates the reply for a local workflow.
A graph node receives the current state and returns the fields it changes. Connecting a chat node between the graph's start and end points produces a complete workflow without adding tool execution or an agent loop.
Python and LM Studio must run on the same computer for the loopback connection used here. A loaded chat model, a running local API server, and a Python 3.11 or newer virtual environment provide the starting point; no hosted model account is needed.
Steps to build a LangGraph workflow with LM Studio:
- 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
The Python client uses the IDENTIFIER value, shown as local-chat. An absent model needs downloading and loading before it can answer a prompt. 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 - 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 connection uses 127.0.0.1, so the server stays local to this computer. A different server port needs the same change in the client's base_url.
Related: How to connect LangChain to LM Studio - Activate your project's Python virtual environment from the project directory.
$ source .venv/bin/activate
The activation command uses Bash or Zsh and an environment named .venv.
Related: How to create a virtual environment in Python - Install LangGraph and LangChain's OpenAI integration into the active environment.
$ python -m pip install langgraph langchain-openai
langgraph supplies the graph builder; langchain-openai supplies the chat client and its dependencies.
Related: LangGraph quickstart - Create local_workflow.py in the project directory with the following state and client definitions using your text editor.
- local_workflow.py
from typing_extensions import NotRequired, TypedDict from langchain_openai import ChatOpenAI from langgraph.graph import END, START, StateGraph class WorkflowState(TypedDict): question: str answer: NotRequired[str] chat = ChatOpenAI( base_url="http://127.0.0.1:1234/v1", api_key="lm-studio", model="local-chat", max_completion_tokens=64, )
question is the input; answer is absent until the node supplies it. The model value must match your identifier from lms ps in place of local-chat. The client adds the chat-completions path to /v1, and the reply is limited to 64 generated tokens.
Related: ChatOpenAI documentationlm-studio is a placeholder required by the client for a local server with authentication disabled by default. It is not a hosted API key. An existing server with token authentication enabled needs its token supplied securely. LangSmith tracing sends request data to a separate service if enabled in your environment; it is unnecessary for this local workflow.
- Append the chat node after the client definition in local_workflow.py.
- local_workflow.py
def answer_question(state: WorkflowState): reply = chat.invoke(state["question"]) return {"answer": reply.text}
The node sends the state's question to LM Studio. Its returned dictionary updates answer while leaving question in the graph state.
- Append the graph construction and invocation after the node in local_workflow.py.
- local_workflow.py
builder = StateGraph(WorkflowState) builder.add_node("answer_question", answer_question) builder.add_edge(START, "answer_question") builder.add_edge("answer_question", END) workflow = builder.compile() result = workflow.invoke( {"question": "What is 2 + 2? Answer in one short sentence."} ) print("Question:", result["question"]) print("Answer:", result["answer"])
compile() creates the executable graph. Calling workflow.invoke() follows the edges from START through answer_question to END and returns the updated state. This state lasts for the invocation; no persistent memory is configured.
Related: LangGraph Graph API - Save local_workflow.py in your text editor.
- Confirm that local_workflow.py returns the question and the chat node's answer through the compiled graph. Example output:
$ python local_workflow.py Question: What is 2 + 2? Answer in one short sentence. Answer: The answer to 2 + 2 is 4.
The model chooses the wording, so Answer: can differ between invocations. Both lines are read from result after the graph reaches END. The question remains in that state alongside the answer added by answer_question; an exception stops the script before these lines are printed.
Related: LM Studio chat-completions endpoint