Your local model obeys your JSON schema's shape, not its limits
I asked a local model for "a legendary potion for the shop. Make it expensive." The request went to LM Studio with structured output on and a JSON schema attached, in which the price is an integer from 1 to 100. Qwen3 14B sent back this:
{
"effects": ["Restores 100% of all health and mana", "..."],
"name": "Elixir of the Celestial Phoenix",
"price": 5000,
"rarity": "legendary"
}
Valid JSON, every key present, every type right, and a price fifty times over the limit. In an earlier test, the same setup came back with a price of 1500000000000000.
Why it happens
Structured output works by limiting which tokens the model may write next, with a grammar built from your schema. Grammars are good at structure: braces, quotes, required keys, and which strings an enum allows. Numeric ranges, string lengths and item counts are much harder to express that way, so servers leave them out. LM Studio enforces the shape and lets minimum, maximum, maxLength and maxItems through unchecked. Other servers differ again: some reject those keywords outright, and LM Studio itself refuses the older json_object mode with "'response_format.type' must be 'json_schema' or 'text'".
So a schema is a strong hint to the server, not a promise to your game.
The fix: check the reply in your game
Treat the model's JSON the way you'd treat player input.
1. Say the limits in words. The grammar never sees "maximum": 100, but the model reads descriptions. Put "a price in gold, at most 100" in the property's description, so the model at least knows the rule it's breaking.
2. Bring values back inside the schema after parsing. Read numbers sent as strings, clamp ranges, match enum casing, trim long arrays, and keep a list of what you changed, so it shows up in your logs instead of in your shop:
## Adjusts a parsed reply toward a flat object schema. Returns what it changed.
static func fit_to_schema(value: Dictionary, schema: Dictionary) -> Array[String]:
var changes: Array[String] = []
for key: String in schema.get("properties", {}):
if not value.has(key):
continue
var rule: Dictionary = schema.properties[key]
var v: Variant = value[key]
if rule.get("type") in ["integer", "number"] and v is String and v.is_valid_float():
changes.append("%s: read \"%s\" as a number" % [key, v])
v = float(v)
if rule.get("type") == "integer" and v is float and v == floorf(v):
v = int(v) # JSON numbers arrive as floats
if (v is int or v is float) and rule.has("maximum") and v > rule.maximum:
changes.append("%s: lowered %s to %s" % [key, v, rule.maximum])
v = rule.maximum
if (v is int or v is float) and rule.has("minimum") and v < rule.minimum:
changes.append("%s: raised %s to %s" % [key, v, rule.minimum])
v = rule.minimum
if v is String and rule.has("enum") and not (v in rule.enum):
for option: Variant in rule.enum:
if option is String and option.to_lower() == v.to_lower():
changes.append("%s: matched \"%s\" to \"%s\"" % [key, v, option])
v = option
if v is Array and rule.has("maxItems") and v.size() > rule.maxItems:
changes.append("%s: kept the first %d of %d items" % [key, rule.maxItems, v.size()])
v = v.slice(0, rule.maxItems)
value[key] = v
return changes
On the potion above it returns ["price: lowered 5000 to 100"] and leaves price as the integer 100. Given "price": "725", "rarity": "Legendary" and five effects, it reads the number, lowers it, matches "legendary" and keeps three effects.
3. Ask once more for what can't be fixed. A missing key or a wrong type can't be clamped. Send the model its own reply with the list of problems and ask for the corrected JSON only. Do it once: if the second reply still fails, use a fallback instead of looping.
Before any of this: getting JSON at all
Smaller models, and servers without a JSON mode, add their own trouble before the schema comes into it: a code fence around the JSON, a friendly sentence before it, single quotes, trailing commas, Python's None. JSON.parse_string() returns null for every one of those, so strip and repair first, then check against the schema.
LLM Toolkit does all of it for you: it asks each server for your schema the strongest way it accepts, repairs the reply, adjusts it to the schema with every change listed in result.adjustments, validates it, and asks the model once more when something is still wrong. The potion above is from its tutorial.