LM Studio Python SDK
Project description
LM Studio Python SDK
Using the SDK
Installation
The SDK can be installed from PyPI as follows:
$ pip install lmstudio
Installation from the repository URL or a local clone is also supported for development and pre-release testing purposes.
Examples
The base component of the LM Studio SDK is the (synchronous) Client.
This should be created once and used to manage the underlying
websocket connections to the LM Studio instance.
However, a top level convenience API is provided for convenience in
interactive use (this API implicitly creates a default Client instance
which will remain active until the Python interpreter is terminated).
Using this convenience API, requesting text completion from an already loaded LLM is as straightforward as:
import lmstudio as lms
model = lms.llm()
model.complete("Once upon a time,")
Requesting a chat response instead only requires the extra step of
setting up a Chat helper to manage the chat history and include
it in response prediction requests:
import lmstudio as lms
EXAMPLE_MESSAGES = (
"My hovercraft is full of eels!",
"I will not buy this record, it is scratched."
)
model = lms.llm()
chat = lms.Chat("You are a helpful shopkeeper assisting a foreign traveller")
for message in EXAMPLE_MESSAGES:
chat.add_user_message(message)
print(f"Customer: {message}")
response = model.respond(chat)
chat.add_assistant_response(response)
print(f"Shopkeeper: {response}")
Additional SDK examples and usage recommendations may be found in the main LM Studio Python SDK documentation.
SDK versioning
The LM Studio Python SDK uses a 3-part X.Y.Z numeric version identifier:
X: incremented when the minimum version of significant dependencies is updated (for example, dropping support for older versions of Python or LM Studio). Previously deprecated features may be dropped when this part of the version number increases.Y: incremented when new features are added, or some other notable change is introduced (such as support for additional versions of Python). New deprecation warnings may be introduced when this part of the version number increases.Z: incremented for bug fix releases which don't contain any other changes. Adding exceptions and warnings for previously undetected situations is considered a bug fix.
This versioning policy is intentionally similar to semantic versioning, but differs in the specifics of when the different parts of the version number will be updated.
Release candidates may be published prior to full releases, but this will typically only occur when seeking broader feedback on particular features prior to finalizing the release.
Outside the preparation of a new release, the SDK repository will include a .devN suffix
on the nominal Python package version.
Contributing to SDK development
Fetching the source code
$ git clone https://github.com/lmstudio-ai/lmstudio-python
$ cd lmstudio-python
To be able to run tox -e sync-sdk-schema, it is also
necessary to ensure the lmstudio-js submodule is updated:
$ git submodule update --init --recursive
Development Environment
In order to work on the Python SDK, you need to install
:pypi:pdm, :pypi:tox, and :pypi:tox-pdm
(everything else can be executed via tox environments).
Given these tools, the default development environment can be set up and other commands executed as described below.
The simplest option for handling that is to install uv, and then use
its uv tool command to set up pdm and a second environment
with tox + tox-pdm. pipx is another reasonable option for this task.
In order to use the Python SDK, you just need some form of
Python environment manager (since lmstudio-python publishes
the package lmstudio to PyPI).
Recommended local checks
The set of checks recommended for local execution are accessible via
the check marker in tox:
$ tox -m check
This runs the same checks as the static and test markers (described below).
Code consistency checks
The project source code is autoformatted and linted using :pypi:ruff.
It also uses :pypi:mypy in strict mode to statically check that Python APIs
are being accessed as expected.
All of these commands can be invoked via tox:
$ tox -e format
$ tox -e lint
$ tox -e typecheck
Linting and type checking can be executed together using the static marker:
$ tox -m static
Avoid using # noqa comments to suppress these warnings - wherever
possible, warnings should be fixed instead. # noqa comments are
reserved for rare cases where the recommended style causes severe
readability problems, and there isn't a more explicit mechanism
(such as typing.cast) to indicate which check is being skipped.
# fmt: off/on and # fmt: skip comments may be used as needed
when the autoformatter makes readability worse instead of better
(for example, collapsing lists to a single line when they intentionally
cover multiple lines, or breaking alignment of end-of-line comments).
Automated testing
The project's tests are written using the :pypi:pytest test framework.
:pypi:tox is used to automate the setup and execution of these tests
across multiple Python versions. One of these is nominated as the
default test target, and is accessible via the test marker:
$ tox -m test
You can also use other defined versions by specifying the target environment directly:
$ tox -e py3.11
There are additional labels defined for running the oldest test environment, the latest test environment, and all test environments:
$ tox -m test_oldest
$ tox -m test_latest
$ tox -m test_all
To ensure all the required models are loaded before running the tests, run the following command:
$ tox -e load-test-models
tox has been configured to forward any additional arguments it is given to
pytest. This enables the use of pytest's
rich CLI.
In particular, you can select tests using all the options that pytest provides:
$ # Using file name
$ tox -m test -- tests/test_basics.py
$ # Using markers
$ tox -m test -- -m "slow"
$ # Using keyword text search
$ tox -m test -- -k "catalog"
Additional notes on running and updating the tests can be found in the
tests/README.md file.
Expanding the API
- the content of
src/lmstudio/_sdk_modelsis automatically generated by thesync-sdk-schema.pyscript insdk-schemaand should not be modified directly. Runtox -e sync-sdk-schemato regenerate the Python submodule from the existing export of thelmstudio-jsschema (for example, after modifying the data model template). Runtox -e sync-sdk-schema -- --regen-schemaafter updating thesdk-schema/lmstudio-jssubmodule itself to a newer iteration of thelmstudio-jsJSON API. - as support for new API namespaces is added to the SDK, each should get a dedicated session type (similar to those for the already supported namespaces), even if it is only used privately by the client implementation.
- as support for new API channel endppoints is added to the SDK, each should get a dedicated base endpoint type (similar to those for the already supported channels). This avoids duplicating the receive message processing between the sync and async APIs.
- the
json_api.SessionDatabase class is useful for defining rich result objects which offer additional methods that call back into the SDK (for example, this is how downloaded model listings offer their interfaces to load a new instance of a model).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lmstudio-1.5.0.tar.gz.
File metadata
- Download URL: lmstudio-1.5.0.tar.gz
- Upload date:
- Size: 200.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.25.9 CPython/3.12.11 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
458c34fe1f94a7dcc521d4226b4cee82b8af7ea3da8c40b31bbdac558d9a74d4
|
|
| MD5 |
17fb1336de9264026f9e98be7d750b5b
|
|
| BLAKE2b-256 |
db6d40876f14c759fa2072ccbc844cc3ef7c74c1824e17e7d19b2cf4ff2cea2c
|
File details
Details for the file lmstudio-1.5.0-py3-none-any.whl.
File metadata
- Download URL: lmstudio-1.5.0-py3-none-any.whl
- Upload date:
- Size: 139.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.25.9 CPython/3.12.11 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b1e5a1cf013744a6ce0a80960204af5bf468dbbb2a505acb4dfb67185fffbe9
|
|
| MD5 |
a179ef00a8641ed3aac68c7e290a7cf3
|
|
| BLAKE2b-256 |
b851dcf7a86872a9de6529d67c68db0319b6f2afa093294d1fb8f1affcc86cc8
|