Download a model via REST API
Arguments
- model
Character. The model to download. Accepts model catalog identifiers (e.g., "openai/gpt-oss-20b") and exact Hugging Face links. Must be one name, given as a single string.
- quantization
Character. Optional. Quantization level of the model to download (e.g., "Q4_K_M"). Only supported for Hugging Face links.
- host
Character. The host address of the local server. Defaults to "http://localhost:1234".
- ...
Additional arguments passed to the request.
- token
Character or
NULL. An API token for a server that requires authentication.NULLreads therlmstudio.tokenoption and then theRLMSTUDIO_API_TOKENenvironment variable. See rlmstudio_token.
Value
A character string containing the download job_id, or
"already_downloaded", invisibly, if the model is already
downloaded. The call aborts with rlmstudio_bad_response when the
reply holds neither a job_id string nor the status
"already_downloaded". It also aborts when the reply is not a JSON
object whose status is a string.
Server not running
Functions that call the LM Studio REST API open a TCP connection to the
hostname and port named in host before they send the request. A function
that checks its own arguments does that first, so a bad model, job_id,
input, inputs, or schema aborts with an argument message and no
condition class
even when the server is down. A condition of class
rlmstudio_no_server is raised when that connection cannot be opened. A
refused connection raises it. So do an address the package cannot parse and
a hostname that does not resolve. An address that neither accepts nor
refuses the connection also raises it. That case waits for the operating
system to give up, which can take a minute. Start the server with
lms_server_start(), or give host the address that your server listens
on.
The check reads the port and nothing else. Any process holding that port
accepts the connection, so the condition is not raised even though no LM
Studio server is there. The call then does not raise
rlmstudio_no_server, and what it does depends on what answers. On a
status-200 body that does not parse as JSON, the chat functions,
lms_embed(), list_models(), lms_load(), lms_download(),
lms_download_status(), and lms_unload_all() raise
rlmstudio_bad_response. lms_unload() does not read the body, so it can
report success. A body that parses as JSON but has another shape can
come back unchanged with simplify = FALSE. With simplify = TRUE, the
chat functions and lms_embed() raise rlmstudio_bad_response for it.
list_models() raises it for a model list with another shape, and so do
lms_unload_all() and lms_load() without force = TRUE, which read that
list. lms_load(), lms_download(), and lms_download_status() raise it
for a reply of their own with another shape, such as {}. A process that
does not answer in HTTP gives an httr2_failure error. Use lms_server_ready() for
the stronger test: it asks the host for a model list and reports TRUE
only for a model list that list_models() can read.
lms_chat_batch() checks the server once before its first input, and
lms_chat() checks it again for each input. If that check finds the server
gone during the batch, the batch aborts with rlmstudio_no_server, and no
request goes out after that. The condition then carries a results field, a list as
long as inputs. Its elements before the lost input hold the values that
format = "list" returns for those inputs. The element of the lost input
and every element after it are NULL. The check before the first input
adds no results field. A connection that fails after the check passes,
such as a server that stops during a request, raises an httr2_failure
error instead. That error aborts the batch and carries no results field.
API failure
A condition of class rlmstudio_api_error is raised when a REST call
returns a response that the wrapper treats as a failure. The condition
carries a status field, which holds the HTTP response status as an
integer.
lms_chat_batch() does not abort on it. The element of the failed input
holds the condition, or NA where the result is text, and the batch warns
once and goes on. See the details of lms_chat_batch().
Malformed response
A condition of class rlmstudio_bad_response is raised when the server
answers with a status the wrapper accepts and a body the wrapper cannot
read. It is raised where a wrapper checks the body before it reshapes it,
rather than indexing straight into whatever arrived. Ten functions raise
it: lms_embed(), lms_chat(), lms_chat_native(),
lms_chat_openresponses(), lms_chat_openai(), list_models(),
lms_load(), lms_download(), lms_download_status(), and
lms_unload_all(). lms_chat() raises it through the chat function it
calls. lms_unload_all() raises it through list_models(), and so does
lms_load() unless force = TRUE.
All ten raise it for a status-200 body that does not parse as JSON, such
as an HTML page from a proxy, JSON text that stops part way, or an empty
body. In the functions that take simplify, the body is parsed before
simplify is read, so the condition is raised whatever simplify is. The
body is parsed by its content and not by its Content-Type header, so
valid JSON under text/plain is read as JSON. The body is read as JSON
text and nothing else. A body whose text is a URL or the path of a file
does not parse, and the package does not fetch the URL or read the file.
The message says that the body did not parse as JSON and that something
other than LM Studio may be answering on the host. It does not hold the
body text.
list_models() also raises it for a status-200 model list with the wrong
shape. lms_unload_all() and lms_load() without force = TRUE raise it
through list_models(). A model list must follow four rules. Each field is
read by its exact name, so a field named keyX does not stand in for
key.
The body is a JSON object whose
modelsfield is an array. The array can be empty.Each entry of
modelsis a JSON object. Itstypeandkeyare strings, and itsloaded_instancesis an array.The
size_bytesof an entry is a number, or absent, ornull.Each entry of
loaded_instancesis a JSON object whoseidis a string with a character that is not whitespace.
The rules are checked before the type and loaded filters, so an entry
that the filters drop can still raise the condition. The message names the
field or entry that broke a rule. lms_server_ready() applies the same
rules and returns FALSE for a body that breaks one.
lms_load(), lms_download(), and lms_download_status() also raise it
for a status-200 reply of their own with the wrong shape. Each reply must
follow the rule of its function. Each field is read by its exact name. The
rules check the type of a field and not its value, with two exceptions. The
status of a load reply must be "loaded", and a download reply whose
status is "already_downloaded" needs no job_id.
A reply of
lms_load()is a JSON object whosestatusis the string"loaded". Withecho_load_config = TRUE, itsload_configis also a JSON object.A reply of
lms_download()is a JSON object whosestatusis a string. If the status is not"already_downloaded", itsjob_idis a string.A reply of
lms_download_status()is a JSON object whosejob_idandstatusare strings. Itstotal_size_bytes,downloaded_bytes, andbytes_per_secondare each a number, or absent, ornull.
The message names the field that broke the rule, or it says that the body is not a JSON object. It also says that something other than LM Studio may be answering on the host.
lms_embed() raises it on an embeddings block it cannot trust. The vectors
it returns are placed by the index that the response reports, so a block
with a missing, repeated, or out-of-range index would otherwise pair a
vector with the wrong text and give back a matrix that is silently wrong.
lms_chat_native() and lms_chat_openresponses() raise it with
simplify = TRUE when the reply holds no readable answer text. Both read
the answer from the items of type "message" in the output array. They
raise it when output is missing, empty, or not an array, or when an item
in it is not a JSON object. They also raise it when no item has the type
"message", as in a reply that holds only reasoning or a tool call. The
text of a message must be one string. For lms_chat_native() that is the
content of the item. For lms_chat_openresponses() it is the text of
each part of type "output_text". For lms_chat_openresponses() only, the
content of each message must be an array of JSON objects, and the
messages together must hold at least one "output_text" part.
With simplify = TRUE and logprobs = TRUE, lms_chat_openresponses()
also raises it for a logprobs value that breaks one of these rules. The
logprobs value of each "output_text" part is checked. A logprobs
value, token, logprob, or top_logprobs that is null or absent
passes its rule. A null step or candidate breaks rule 2 or rule 5.
The value is an array.
Each step in the array is a JSON object.
The
tokenof a step is a string.The
logprobof a step is a number.The
top_logprobsof a step is an array of JSON objects.The
tokenandlogprobof each of those objects follow rules 3 and 4.
The parts are checked in order, then the steps of a part, then the
candidates of a step, one at a time. Within a step, rules 3 and 4 and the
array test of rule 5 come before the candidates. The message names the
first broken rule that this order reaches. These checks run only after the
text of every "output_text" part is read, so a reply that also has a bad
text in any part gets the text message.
Parts of other types, such as a refusal, are not checked, and with
logprobs = FALSE no part is checked. Fields are read by their exact
names, so a field whose name only starts with the one asked for, such as
tokenX, reads as absent and gives NA in the data frame.
Apart from a body that does not parse as JSON, lms_chat_openai() raises
it in three cases, all only with simplify = TRUE. The first case is a response whose choices field is
missing, empty, or not an array, or whose first element is not a JSON
object with a message object in it, so there is no reply to read. This
case is raised with or without a schema, and with logprobs = TRUE as
well. The second case is a reply that does not parse. A schema was
given, logprobs = FALSE, and the reply content is not one string of valid
JSON. The third case is reply content that is not one string, such as
null, a missing content field, a number, or an array. A reply that
holds only a tool call has null content. This case is raised without a
schema, and with logprobs = TRUE with or without one. In the second and
third cases, if the server reports the finish reason "length", the token
limit cut the reply off. The message then says so and names max_tokens.
With simplify = TRUE, lms_chat_native(), lms_chat_openresponses(),
and lms_chat_openai() also raise it for a body that is a bare JSON value,
such as 5, "s", or true. The message says that the response body is
not a JSON object. A body of null gets the message about its missing
output or choices field instead.
lms_chat() can raise the condition through all three chat functions.
lms_chat_batch() does not abort on it. The element of the failed input
holds the condition, or NA where the result is text, and the batch warns
once and goes on. See the details of lms_chat_batch().
The condition carries a status field, which holds the HTTP response
status as an integer. Today the status is always 200: each of these
functions reads the body only after a 200, and reports every other status
as an rlmstudio_api_error instead. A condition from lms_chat_openai()
also carries two more fields. The content field holds the reply content,
and the finish_reason field holds the finish reason of the first choice.
Both are NULL for a response with no choices. In the third case,
content holds the value that was read, which is NULL for null or
missing content. For the second and third cases, the message names the
content field, so you can read what the model wrote without a second
request. The other messages of the chat functions and lms_embed() name
simplify = FALSE, which returns the body unchanged, with one exception. A body that did not parse as JSON is
checked before that argument is read, so its message points at the host
instead. For such a body, the content and finish_reason fields of a
condition from lms_chat_openai() are NULL.
Examples
if (FALSE) { # \dontrun{
lms_server_start()
# Download a model by its HuggingFace identifier
job_id <- lms_download("google/gemma-3-1b")
# Download with a specific quantization level
lms_download("google/gemma-3-1b", quantization = "4bit")
} # }