Local models managed by LM Studio are accessible to Python scripts through its official lmstudio-python SDK. The SDK installs from PyPI under the package name lmstudio.

The package requires Python 3.10 or newer. A virtual environment keeps its dependencies separate from the system interpreter and other projects. Shell activation with source works in Bash or zsh on Linux or macOS.

A running LM Studio application or standalone llmster daemon on the same computer is needed for the connection check. SDK version 1.5 or newer discovers the local daemon without enabling the optional HTTP server, and listing downloaded models does not require loading a model into memory.

Steps to install lmstudio-python for LM Studio:

  1. Create a virtual environment named .venv in your project directory.
    $ python3 -m venv .venv

    On Ubuntu or Debian, the matching python3-venv package is required if the interpreter reports that ensurepip is unavailable.
    Related: How to create a virtual environment in Python

  2. Activate the project's virtual environment in the current shell.
    $ source .venv/bin/activate

    The environment's python command now uses its own interpreter and packages.
    Related: How to activate a Python virtual environment

  3. Install the official lmstudio package from PyPI.
    $ python -m pip install lmstudio

    python -m pip installs into the interpreter selected by the active environment; sudo is not needed. The package name is lmstudio, although the SDK is called lmstudio-python.
    Related: Official Python SDK setup

  4. Confirm that pip reports lmstudio inside the virtual environment. Example output:
    $ python -m pip show lmstudio
    Name: lmstudio
    Version: 1.5.0
    ##### snipped #####
    Location: /home/sgreader/lmstudio-demo/.venv/lib/python3.14/site-packages
    ##### snipped #####

    The version and path vary. Location should point inside your project's .venv directory.

  5. Query the local LM Studio model inventory through the installed SDK. Example output:
    $ python -c 'import lmstudio as lms
    print([m.model_key for m in lms.list_downloaded_models()])'
    ['text-embedding-nomic-embed-text-v1.5']

    A list of model keys confirms that the import succeeded and the SDK reached LM Studio. An empty [] is also a successful response when no models are downloaded; a connection exception means the local application or daemon is unavailable. If API authentication is enabled, the SDK reads your token from LM_API_TOKEN as described in the authentication documentation. The local API does not enforce authentication by default.
    Related: How to download a model with lms in LM Studio

Share this guide