Applications that consume model replies need predictable fields instead of free-form prose. LM Studio can return structured output constrained by a JSON schema, so a local response can be parsed and checked before an application uses it.
The OpenAI-compatible Chat Completions endpoint accepts the schema in response_format. The generated object still arrives as a string inside choices[0].message.content, separate from the API response envelope.
Schema validation checks field names, types, and constraints rather than factual accuracy. A response can match the schema while extracting the wrong value, so applications still need any domain checks their data requires.
Steps to request structured output from LM Studio:
- Download Qwen2.5 3B Instruct in Q4_K_M quantization under the account running LM Studio.
$ lms get \ https://huggingface.co/lmstudio-community/Qwen2.5-3B-Instruct-GGUF@q4_k_m \ --gguf --yes
The remaining commands use a POSIX shell with curl and Python 3. LM Studio must already be running with lms available; on a headless Linux machine, the published setup provides llmster and starts its daemon. The full repository URL selects a public model without sign-in.
Related: How to run LM Studio headlessly with llmster
Related: How to download a model with lms in LM Studio - Load the downloaded model with the identifier local-json.
$ lms load qwen2.5-3b-instruct --identifier local-json \ --context-length 4096 --gpu off
The request addresses this loaded identifier. 4096 tokens leave room for the prompt and response, while –gpu off uses the CPU. Ubuntu and Debian require libgomp1 for CPU loading. Structured-output support varies by model; small-model chat success alone does not establish schema support.
Related: How to load a model with lms in LM Studio - Start the HTTP server on loopback port 1234.
$ lms server start --bind 127.0.0.1 --port 1234
The request uses this local address with API-token authentication disabled. Loopback keeps the listener on this machine; remote access needs separate authentication and network controls. If a server already uses another port, the request URL must match it.
- Create a Python virtual environment named .venv in your working directory.
$ python3 -m venv .venv
This directory holds the schema validator without changing system Python packages. Ubuntu and Debian may need the matching python3-venv package first.
Related: How to create a virtual environment in Python - Install jsonschema in the virtual environment.
$ .venv/bin/python -m pip install jsonschema
- Create request.json in the same directory using your text editor.
- request.json
{ "model": "local-json", "messages": [ { "role": "user", "content": "Extract the inventory item: notebook, quantity 3, in stock." } ], "response_format": { "type": "json_schema", "json_schema": { "name": "inventory_item", "strict": true, "schema": { "type": "object", "properties": { "item": {"type": "string", "minLength": 1}, "quantity": {"type": "integer", "minimum": 1, "maximum": 10}, "in_stock": {"type": "boolean"} }, "required": ["item", "quantity", "in_stock"], "additionalProperties": false } } }, "temperature": 0, "max_tokens": 256, "stream": false }
The schema requires a nonempty item string, an integer quantity from 1 through 10, and a boolean in_stock, with no extra properties. strict, stream, and additionalProperties use JSON booleans. max_tokens allows up to 256 generated tokens; a longer object may need more.
Related: LM Studio structured-output reference - Save request.json in your text editor.
- Send the schema-constrained request to the local Chat Completions endpoint.
$ curl --fail-with-body --silent --show-error \ http://127.0.0.1:1234/v1/chat/completions \ -H 'Content-Type: application/json' \ --data-binary @request.json \ -o response.json
response.json contains the complete API response, including the generated content and completion status. Curl reports connection and HTTP errors; an existing response.json is overwritten.
- Validate the generated JSON against the schema in request.json. Example output:
$ .venv/bin/python -c ' import json from jsonschema import validate request = json.load(open("request.json")) schema = request["response_format"]["json_schema"]["schema"] response = json.load(open("response.json")) choice = response["choices"][0] if choice["finish_reason"] != "stop": raise SystemExit("Incomplete response") result = json.loads(choice["message"]["content"]) validate(result, schema) print(json.dumps(result, indent=2)) '
{ "item": "notebook", "quantity": 3, "in_stock": true }A completed, schema-valid response prints the parsed object. json.loads rejects surrounding prose or Markdown fences without stripping them, and validate raises an error for missing fields, wrong types, or constraint violations. A completion status other than stop is rejected before parsing; a length status can require a larger token budget within the model's context limit.