A JSON schema defines the names and types your application expects. In LM Studio, pass the schema through response_format and parse the JSON string in choices[0].message.content.
Extract two fields with response_format
Start the LM Studio local server, load a model that supports structured output, and use the identifier returned by GET /v1/models. The example assumes the default loopback address. Set the port and authorization headers to match your local configuration.
import json
from urllib.request import Request, urlopen
payload = {
"model": "REPLACE_WITH_YOUR_MODEL_ID",
"messages": [{"role": "user", "content":
"Extract the ID and total in cents. Invoice INV-0427 totals USD 269.15."}],
"response_format": {"type": "json_schema", "json_schema": {
"name": "invoice_fields",
"schema": {
"type": "object",
"properties": {"invoice_id": {"type": "string"}, "amount_cents": {"type": "integer"}},
"required": ["invoice_id", "amount_cents"],
"additionalProperties": False,
},
}},
}
request = Request("http://127.0.0.1:1234/v1/chat/completions",
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json"})
with urlopen(request, timeout=60) as response:
completion = json.load(response)
item = json.loads(completion["choices"][0]["message"]["content"])
assert item == {"invoice_id": "INV-0427", "amount_cents": 26915}, item
Verify the schema and the extracted values
The sample should return the invoice ID INV-0427 and the integer 26915. Try a second amount such as USD 18.05 and change the expected value to 1805. A schema checks shape. Your application still needs a source comparison when a correct value matters.
Check this example against a model and runtime that support the requested schema before using it with a larger set of documents.
Try it: four checks
| Check | What you should see | If you do not see it |
|---|---|---|
| Server running | GET /v1/models returns your model | Start the server in the Developer tab or with lms |
| Schema accepted | The chat call returns without a format error | Check the response_format shape |
| Reply parses | The message content parses as JSON | Ask for JSON in the prompt too |
| Fields match | The parsed object has the required fields | Test with your model first |
Expected result. We checked the LM Studio OpenAI Compatibility Endpoints page, which lists the chat completions endpoint and the model identifier rule, and the docs note that structured-output constraint support depends on the model and runtime. We did not call the API on this machine.
Limits
This guide covers the response_format call only. Whether a given model honours the schema is a property of that model. We did not test any call ourselves.
