Imagine a product note containing the word FRIEND. Your application receives only FRI, followed by an apparently normal completion. The HTTP request succeeded. No obvious infrastructure error explains the missing text. Somewhere in the request, however, a developer chose END as the stop sequence.
This is the kind of failure that makes GLM API stop sequences deceptively difficult. The parameter looks like a convenient way to prevent an answer from wandering into commentary. Yet a boundary chosen for the application's convenience can also occur inside the data the application needs. An answer can end at the intended technical boundary while failing the business task completely.
The useful question is therefore more specific than “How do I stop GLM?” It is “Which exact text may end this response, and what evidence will prove that the data before that boundary is complete?” That question matters whether you call the provider directly or reach GLM through a GLM proxy or API relay. This article develops a small delimiter lab around that distinction, with a bounded request template and a locally tested parser. No paid model request was executed for this article.
A delimiter is a text boundary, not an understanding of your task
The Z.AI Chat Completion reference documents string-array stop, ending generation at a specified string. Its prose says one entry; its schema displays four. Use one conservatively until your exact route confirms otherwise.
Now consider what your application means by “finished.” A human editor might mean a complete sentence. A data importer might mean all requested records. A report generator might mean that every section has supporting evidence. A stop string expresses none of those conditions. It gives the generation system a textual boundary, while your application still owns the meaning of completion.
That distinction explains why a short marker can appear to work in a demo. The demo's sample text never contains it. The failure arrives later, when a customer name, document quotation, code sample, or product description introduces the same characters. The difference is the input distribution, not necessarily a new model defect.
A prompt saying “write the marker only when finished” helps describe the output format. It does not make the marker impossible inside quoted evidence. If the task requires exact transcription, instructing the model to remove the marker from source text would silently change the task. The better response is to reconsider the boundary, then validate the content that survives it. For a broader discussion of unclear task contracts, see TreeRouter's article on semantic gaps in prompts.
The collision lab: common delimiters meet ordinary data
Before choosing a marker, write down the kinds of text that your application must preserve. A support workflow can include Markdown headings, copied logs, braces, multilingual names, and literal examples of API syntax. Treat these as normal payload, not edge cases to erase.
| Candidate stop text | Plausible collision | What the application can lose |
|---|---|---|
END | The product name FRIEND | Part of a field and everything following it |
### | A Markdown heading copied from a document | Later sections or quoted evidence |
} | A nested object or a brace inside a string | The remainder of an otherwise valid data structure |
| A blank line | Paragraph spacing in an explanation | Every paragraph after the first |
| A per-request marker | The model reproduces the marker too early | Potentially the entire payload |
The final row is deliberate. A longer, unusual marker reduces a practical collision surface; it does not prove correctness. The model sees the marker because it needs instructions about where to emit it. It may still explain the requested format, quote the instructions, or place the marker before the answer. A successful design therefore combines a less ambiguous boundary with a consumer that rejects incomplete work.
For an application-generated marker, a construction such as <END_ plus secrets.token_hex(16) plus > is easy to inspect. Python's official secrets documentation describes token_hex as producing hexadecimal random text. Here, randomness is being used to reduce accidental reuse, not to authenticate the model or protect a secret. Keep the generated marker with the request record so the exact response can be interpreted later.
Scan the source material for the proposed marker before submitting a request. If it appears, generate another. That check only covers material you already possess; it cannot guarantee what the model will generate. Also scan any literal output examples you place in the prompt. An example that includes the terminal marker inside a sample field can teach precisely the collision you hoped to avoid.
Why braces and JSON escaping do not solve the boundary problem
Using } as a stop sequence is especially tempting when the desired answer is an object. But the first closing brace might close an inner object rather than the outer envelope. It may also be an ordinary character in a string. A text boundary has no reason to inherit the nesting rules of your application's parser.
The JSON specification, RFC 8259, defines string quoting and escaping. That syntax answers whether text is valid JSON. It does not establish an API stop rule. A value containing } is still legitimate data when represented correctly, and braces should not become forbidden simply because they are convenient to recognize visually.
Escaping introduces another distinction: decoded values and their serialized text are different representations. If a boundary experiment uses a newline, record whether the generated content contains an actual newline or two visible characters, backslash and n. Inspect a representation such as Python's repr(content) rather than trusting a rendered chat bubble. An application may display two different representations identically enough to hide the relevant difference.
For machine-readable output, start by asking whether you need a custom stop string at all. A complete parsed object, a known batch identifier, and an expected set of record identifiers can establish a stronger application boundary. If your chosen route supports an appropriate structured output mode, evaluate that separately. Adding a textual delimiter to structured output is not automatically an improvement, and this article does not assume that a given relay passes every optional parameter through.
Observe the route with one bounded request
The following is a request template, not a record of a successful GLM response. Configure three environment variables from the route you intend to use: GLM_CHAT_URL must be the exact chat-completions URL, GLM_MODEL must be the exact enabled model identifier, and GLM_API_KEY must be its credential. Do not infer an API URL from the address of a marketing website.
For a direct Z.AI account, the reference documents https://api.z.ai/api/paas/v4/chat/completions. For a TreeRouter route, obtain the current endpoint and model identifier from your account's integration information. The payload below targets a GLM-5.3-compatible route and uses reasoning_effort: low. The documented GLM-5.3 effort levels are low, high, and max; its thinking cannot be disabled, as the Thinking Mode guide explains. A short visible answer should not be treated as evidence that no reasoning work occurred.
# Set GLM_CHAT_URL, GLM_MODEL, and GLM_API_KEY securely before running.
: "${GLM_CHAT_URL:?Set the exact chat-completions URL}"
: "${GLM_MODEL:?Set the exact enabled GLM-5.3 model ID}"
: "${GLM_API_KEY:?Set the route API key}"
export GLM_MODEL
python - <<'PY'
import json
import os
from pathlib import Path
marker = "<END_7c90a41d>"
body = {
"model": os.environ["GLM_MODEL"],
"messages": [{"role": "user", "content":
"Reply with exactly this text, without quotes or explanation: "
"ALPHA" + marker + "OMEGA"}],
"stop": [marker],
"stream": False,
"max_tokens": 2048,
"reasoning_effort": "low"
}
Path("glm-stop-request.json").write_text(
json.dumps(body), encoding="utf-8")
PY
curl --disable --silent --show-error --fail-with-body \
--connect-timeout 10 --max-time 60 --retry 0 \
--request POST "$GLM_CHAT_URL" \
--header "Authorization: Bearer $GLM_API_KEY" \
--header "Content-Type: application/json" \
--data-binary @glm-stop-request.json \
--output glm-stop-response.json \
--write-out 'HTTP %{http_code}\n'The curl manual documents the transfer timeout and retry controls used here. This template makes one request, limits connection and total transfer time, and requests a bounded output. Those limits constrain the experiment; they are not a quote for its price. Keep credentials out of shared logs and do not enable shell tracing around the command.
Inspect the saved response only after checking curl's exit status and the HTTP result. Preserve the returned model identifier, response identifier, finish_reason, and the exact assistant content. Do you see ALPHA? Does OMEGA remain? Is the marker present, absent, or changed? Record the observation without assuming in advance that the stop marker is omitted. The cited stop description alone is not enough to establish literal marker behavior for every relay implementation.
If the model fails to reproduce the instructed text, that result is inconclusive about exact matching. A later comparison without stop can help distinguish instruction following from boundary handling, but it is another explicitly budgeted request, not an automatic retry loop. Likewise, a timeout or an output limit does not prove that the route ignored stop. Change one variable at a time and retain the original evidence.
finish_reason: stop does not certify complete data
The completion reference lists stop, tool_calls, length, sensitive, model_context_window_exceeded, and network_error as termination reasons. Preserve the actual value. For the small text-only importer in this article, anything other than stop is a reason to withhold import and investigate. That is an application policy, not a universal interpretation of every GLM workflow.
Even stop only makes the response eligible for further checks. It cannot prove that every requested item exists, that a copied value is correct, or that a delimiter occurred in the intended position. The same separation between status and finished work appears in TreeRouter's discussion of agent completion claims. A completion label and a usable result are different observations.
Imagine requesting notes for records r1 and r2. The model returns a syntactically valid object containing only r1, then terminates. No amount of checking for balanced braces will discover the absent record unless the consumer knows what it expected. A record count is also insufficient if r1 appears twice. Compare identities, reject duplicates, and validate the fields that matter to the task.
When investigating an incomplete response, keep the delimiter hypothesis separate from a token budget hypothesis. A more general example of distinguishing termination causes is TreeRouter's Claude truncated-response guide. Its protocol details belong to Claude; the useful habit here is preserving evidence before changing settings.
A local consumer that refuses plausible fragments
The following Python example runs without an API key or network access. It represents a tiny batch of notes, not a general JSON classification service. Its deliberately narrow contract requires the expected batch and both record IDs. The Python JSON documentation provides the decoding hooks used to reject duplicate object keys and nonstandard numeric constants.
The marker_visible switch must come from an observed route contract. When enabled, the consumer removes exactly one terminal marker. It never splits arbitrary content on the first marker and never repairs missing braces. When disabled, the entire assistant content must parse as the object. These are two explicit modes, not a heuristic that silently guesses how the provider behaved.
import json
MARKER = "<END_7c90a41d>"
EXPECTED = {"r1", "r2"}
def unique_object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError("duplicate key")
result[key] = value
return result
def reject_constant(value):
raise ValueError("non-JSON numeric constant")
def accept(content, finish_reason, marker_visible=False):
if finish_reason != "stop":
raise ValueError("completion requires investigation")
if not isinstance(content, str) or len(content.encode("utf-8")) > 65536:
raise ValueError("invalid content or size")
if marker_visible:
if not content.endswith(MARKER):
raise ValueError("missing exact terminal marker")
content = content[:-len(MARKER)]
obj = json.loads(content, object_pairs_hook=unique_object,
parse_constant=reject_constant)
if not isinstance(obj, dict) or set(obj) != {"batch_id", "rows"}:
raise ValueError("wrong envelope")
if obj["batch_id"] != "batch-07" or not isinstance(obj["rows"], list):
raise ValueError("wrong batch")
seen = set()
for row in obj["rows"]:
if not isinstance(row, dict) or set(row) != {"id", "note"}:
raise ValueError("wrong row shape")
if not isinstance(row["id"], str) or not isinstance(row["note"], str):
raise ValueError("wrong row types")
if row["id"] in seen:
raise ValueError("duplicate row")
seen.add(row["id"])
if seen != EXPECTED:
raise ValueError("missing or unexpected rows")
return obj
def rejected(label, content, reason="stop", visible=False):
try:
accept(content, reason, visible)
except (ValueError, TypeError):
print(label + ": rejected")
else:
raise AssertionError(label + " should fail")
payload = {"batch_id": "batch-07", "rows": [
{"id": "r1", "note": "FRIEND membership"},
{"id": "r2", "note": "Keep the literal } and ### in this note"}
]}
valid = json.dumps(payload, ensure_ascii=False)
assert accept(valid, "stop") == payload
assert accept(valid + MARKER, "stop", True) == payload
# Local collision simulation; this does not claim API implementation behavior.
rejected("ordinary-word collision", valid.split("END", 1)[0])
rejected("brace collision", valid.split("}", 1)[0])
rejected("trailing commentary", valid + "\nFinished.")
missing = dict(payload, rows=payload["rows"][:1])
rejected("valid JSON with missing row", json.dumps(missing))
rejected("length termination", valid, "length")
rejected("missing visible marker", valid, visible=True)
duplicate = valid.replace('"batch_id": "batch-07"',
'"batch_id": "wrong", "batch_id": "batch-07"')
rejected("duplicate JSON key", duplicate)
print("2 accepted fixtures; 7 rejected fixtures; no network calls")Running the example produces two accepted fixtures and seven rejected fixtures. These are local test outcomes, not provider benchmarks. The ordinary-word and brace cases simulate truncation by splitting a string; they do not claim to reproduce the provider's matching implementation. Their purpose is to show that the consumer rejects the kinds of fragments a poor delimiter can create.
Notice the more interesting failure: valid JSON with a missing row is rejected too. This is where a machine-readable boundary becomes useful. Parsing proves that the text obeys a grammar; record reconciliation proves that a specific structural obligation was met. Neither proves that the note is factually faithful. If fidelity matters, compare protected fields to the source or route changed values for review. Do not mistake the sample's acceptance result for permission to skip that domain-specific check.
The size limit is an application choice for this small payload. Adapt it to your actual contract rather than copying it as a model limit. Also keep the outer API response distinct from the inner assistant text: an API envelope can be valid JSON while its message.content contains an unusable fragment. Parse and inspect both layers deliberately.
Make the boundary visible in operations
A useful incident record names the chosen marker, exact model route, prompt version, returned termination reason, and the validator's decision. It also preserves a suitably protected copy of the relevant response when your data-handling rules allow one. Logging only “success” erases the difference between a completed transfer, a parsed object, and a reconciled batch.
Prefer a small set of failure labels that tell the next engineer what to inspect: invalid JSON, unexpected trailing text, missing record, duplicate record, marker-contract mismatch, and noneligible termination. Keep these labels separate from provider error codes. A collision inside a customer note is an application boundary failure even if the transport worked correctly. TreeRouter's observability framework article offers related background on making silent failures visible.
If a GLM proxy changes models or routes, treat the observed delimiter contract as something to recheck. A shared request shape is useful, but the same JSON field names do not establish identical behavior across every upstream model. The architectural context is covered in TreeRouter's guide to API gateway routing. For this specific feature, keep the pilot narrow: one intended route, one stop entry, ordinary data that could collide, and a consumer with explicit rejection reasons.
Questions that matter before shipping
Can I use a newline to make every answer one line?
Only if a newline is genuinely outside the allowed payload, and the exact route behavior has been observed. Notes, quotations, and copied logs often need line breaks. A one-line presentation requirement can usually be handled after a complete answer is validated; it should not silently delete content during generation.
Is a random marker enough to prevent truncation?
No. It reduces accidental overlap with known source text, especially when you scan that text first. It does not force the model to emit the marker at the correct time. The consumer must still check the payload's shape, required identities, and task-specific content.
Should I add a missing brace and accept the result?
No. A repaired fragment may look valid while missing an entire record or a decisive qualification. Preserve the failure, decide whether a new request is justified, and make any retry bounded and attributable. Repairing syntax is not evidence that missing information has been recovered.
Can I assume TreeRouter supports this stop behavior?
Confirm support for your desired model route and run a small pilot before depending on it. Begin at TreeRouter, obtain the exact integration details for your account, and use the delimiter experiment to establish the behavior you actually receive. The decision to adopt the route should rest on observed results and your application's validation contract, not on the presence of an OpenAI-style parameter alone.
References
- Z.AI Chat Completion API reference — stop parameter and termination metadata.
- Z.AI Thinking Mode — current GLM thinking behavior.
- Python `secrets` — random hexadecimal marker generation.
- RFC 8259: JSON — strings, escaping, and JSON syntax.
- Python `json` — decoding and validation hooks.
- curl manual — bounded request controls.






