Quick answer: GLM 5.4 the mode makes the model return a single JSON object instead of prose. Set response_format to {"type": "json_object"}, describe the exact keys and value types in the prompt, and always validate the result before you trust it. the mode guarantees parseable JSON, not your schema, so a validator and one retry are still part of a production pipeline.
Teams reach for the mode the moment they stop reading model output by eye and start routing it into code. A classifier that returns a label, an extractor that returns fields, an agent that returns a plan: all of them need structured output. GLM 5.4 the mode removes the "sometimes it wraps the JSON in a sentence" failure, but it does not remove the need for a schema. This guide covers how to get the most out of the mode on GLM 5.4 and where it stops.
What JSON mode guarantees
the mode guarantees one thing: the content is valid JSON that parses. It does not guarantee the keys you wanted, the value types you expected, or that every field is present. That distinction is the whole game with the mode.
A reliable the mode setup has three layers:
- The mode —
response_formatset tojson_object. - The instruction — a prompt that names every key and type.
- The guard — a validator that rejects bad output and retries once.
Skip the second layer and the mode returns syntactically valid junk. Skip the third and a bad field reaches your database.
Enable JSON mode on GLM 5.4
The request is ordinary Chat Completions with one extra field. Send the mode flag and a system message that spells out the shape you want.
resp = client.chat.completions.create(
model="glm-5.4",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": (
"Return a JSON object with keys: "
"label (string: spam|ham), "
"confidence (number 0-1), "
"reason (string, max 20 words). "
"No other keys."
)},
{"role": "user", "content": text},
],
)
Two details matter for the mode. First, the word "JSON" should appear in the prompt; many providers require it. Second, list the allowed values for enums so the mode does not invent a label.
Write prompts that JSON mode can follow
the mode follows a schema described in the prompt far better than one implied by context. Be literal.
- Name every key, including nested keys.
- State the type: string, number, boolean, array, object.
- Give allowed values for enums.
- Say what to do when a value is unknown, for example
null. - Ask for no extra keys.
Vague prompts produce valid but useless the mode output, like {"result": "yes"} when you needed a confidence and a reason. Specific prompts produce fields you can store.
Validate every JSON mode response
Never trust the mode output as-is. Parse it, then validate against a schema. A small validator catches the failures the mode does not:
- Missing required keys.
- Wrong types, like a number sent as a string.
- Enum values outside the allowed set.
- Extra keys you did not ask for.
- Empty strings where a value was required.
If validation fails, retry once with the validator's error added to the prompt. This "the mode plus repair" loop fixes most problems and is cheap compared with a bad write to your database.
Common JSON mode mistakes
- Assuming schema enforcement. the mode is not JSON Schema mode; it does not enforce your fields.
- No enum list. The model invents values, and JSON mode happily returns them.
- Nested structures without an example. Deep shapes need a sample, or JSON mode guesses the nesting.
- Parsing without a try block. Even valid JSON mode output can be cut off at a token limit, leaving invalid JSON.
- Ignoring finish_reason. A
lengthfinish means the object may be incomplete; treat it as a failure. - No retry. A single repair attempt turns most JSON mode errors into successes.
JSON mode vs tool calling
JSON mode is for data you want back. Tool calling is for actions you want the model to take. If the model needs to decide which function to run, use tools, not JSON mode. If it needs to return a record, use JSON mode.
A practical rule: JSON mode for extraction and classification, tool calling for workflows with side effects. Mixing them in one call is possible but harder to debug, and JSON mode loses its simplicity.
A JSON mode production checklist
response_formatset tojson_object.- The prompt names every key and type.
- Enums list allowed values.
- Unknown values map to
null. - The response is validated against a schema.
- A failed validation retries once with the error.
finish_reasonis checked for truncation.- JSON mode output is logged for debugging.
If all eight pass, JSON mode is production-ready.
FAQ
Does GLM 5.4 JSON mode enforce my schema? No. JSON mode guarantees valid JSON, not your schema. Validate separately.
Why does JSON mode still return invalid JSON sometimes? Usually a token limit cuts the object off. Check finish_reason and raise max_tokens for JSON mode.
Can JSON mode return an array? Many providers require an object at the top level. Wrap arrays in a key such as items when you use JSON mode.
Is JSON mode slower? It can be slightly slower than prose, but the difference is small next to the cost of parsing failures that JSON mode prevents.
Turn prose into reliable data
JSON mode is the bridge between a chat model and your code, as long as you pair it with a clear schema and a validator. Set response_format, list the keys and types, and retry once on failure. Try the shape in GLM 5.4 in the chat UI, then wire the same request into production through the API docs. Used with discipline, JSON mode makes structured output boring, which is exactly what production needs.
Sources
- Z.ai API documentation: https://docs.z.ai/
- OpenAI, JSON mode and structured outputs: https://platform.openai.com/docs/guides/structured-outputs
- JSON Schema specification: https://json-schema.org/




