skip to content

How should a prompt template's render step handle typed inputs, and missing or empty values?

level: middleimportance: should knowfreq 34%

answer

  1. a contract checked before the fill
  2. missing is not the same as empty
  3. a label with nothing after it
  4. the section goes, label included
  5. branches multiply rendered variants

basics

~20 s

Check a prompt's values against a declared contract before filling anything. A missing required value fails loudly with no model call; a legitimately empty optional value drops its whole section, label included, so no dangling line reaches the model.

solid answer

~50 s

I'd give the template a declared **input contract**: each variable's type, whether it is required, and its limits, such as an allowed list for `plan_tier` and a length cap for `ticket_body`. The render step checks the contract first, so a bad value stops before any tokens are spent. A missing required value is a caller bug and should raise; a permissive engine may print an undefined variable as empty text, so I'd turn on strict behaviour if it offers one. An empty optional value is a normal case: remove the whole section, label included, because `Customer:` with nothing after it leaves the model a gap it may fill or comment on. I keep template logic to presence checks and leave business decisions in code, since every branch adds a rendered variant to test. Validation checks shape, not meaning: a valid string can still carry instructions.

code

pseudocode · 24 lines
pseudocode
CONTRACT = {
  plan_tier:     { type: enum[free, pro, enterprise], required: true },
  language:      { type: enum[en, es, de, fr],        required: true },
  ticket_body:   { type: string, max_len: 8000,       required: true },
  customer_name: { type: string, max_len: 80,         required: false }
}

function check_contract(inputs):
  for name in keys(inputs):
    if name not in CONTRACT: raise UnknownInput(name)
  for name, rule in CONTRACT:
    value = inputs.get(name)
    if value is missing or blank:
      if rule.required: raise MissingInput(name)
      continue                         # optional and absent: allowed
    if not matches(value, rule): raise InvalidInput(name)

function render_reply_prompt(inputs):
  check_contract(inputs)               # raises before any model call
  sections = [INSTRUCTIONS, PLAN_SECTION]
  if present(inputs.customer_name):
    sections.add(CUSTOMER_SECTION)     # label and value travel together
  sections.add(TICKET_SECTION)
  return fill_once(join(sections, blank_line), inputs)

go deeper

for a junior

Remember the two cases: a required value that is missing is an error, and an optional value that is empty removes its section. Never leave a label with nothing after it.

for a middle

Explain the contract: type, required flag and limits, checked before the fill. Name what each check catches, and know that an engine may print an undefined variable as empty text unless configured otherwise.

for a senior

Show you have thought about the failure path, not only the check: what the user sees, what gets counted, and how a contract change deploys safely alongside its callers.

for a principal

Set the team's rule for how much logic templates may hold, and tie it to evaluation cost: every independent branch doubles the rendered shapes someone must trust.

## Why a placeholder needs a contract A **placeholder** names a gap and says nothing about what may fill it. Without further checks, a render step accepts any value and returns text, so a bad value becomes a plausible-looking prompt and then a plausible-looking reply. An **input contract** closes that gap: for each variable, it declares a type, whether the variable is required, and its limits. The checks run before the fill, so a failure costs no model call. Here is a contract for a support-reply generator: | Variable | Type | Required | Check | |---|---|---|---| | `plan_tier` | One of an allowed list | Yes | Value is in the list | | `language` | One of four supported | Yes | Value is in the list | | `ticket_body` | String | Yes | Not blank, under a length cap | | `customer_name` | String | No | Under a length cap | Each kind of check catches a different fault: - **An unknown-key check** catches `plan_teir`, where the caller believes it supplied the plan while the real placeholder stays unfilled. - **A type check** catches a null or a nested object that would otherwise print as a stray word or a debug dump inside the prompt. - **An allowed list** stops a tier name the prompt's policy text has never heard of. - **A length cap** stops one enormous paste from crowding out everything else in the context window. ## Missing and empty are different cases The two look alike in the rendered text and mean different things. - **Missing** means the caller did not supply a required value. That is a bug upstream, so the render should raise and the request should take a defined failure path. - **Empty** means the value is legitimately absent, such as a customer with no name on file. That is a normal request, and the template needs an answer for it. Whitespace-only input usually belongs in this case too. Engines differ in what they do with a name they cannot find. One template engine's documentation says that by default an undefined variable evaluates to an empty string when printed, and it offers a strict mode that disallows almost any use of one. Under a permissive default, a caller bug renders quietly, so check which behaviour yours has rather than assuming an error will appear. The usual result of either case left unhandled is a **dangling label**: ```plaintext Customer: Ticket: My export has been stuck since Tuesday. ``` No fixed rule says what a model does next. It may greet the reader as `Customer`, invent a detail or remark on the gap, which is why a review of the template file never finds the problem. ## Handling optional sections Treat an optional value and its surrounding text as one unit: 1. Decide whether the value is present, in one place, using the same definition of blank everywhere. 2. Include or drop the whole section, label and all, rather than blanking only the value. 3. Join sections in a way that leaves no stray blank lines. One engine's documentation shows that, by default, removing a block tag on its own line still leaves a blank line behind. 4. Use a default only when the sentence stays true. A neutral greeting is fine; `Unknown` may be repeated to a customer as if it were a name. A value that changes what the model should do, such as a plan tier, has no safe default. ## How much logic belongs in the template General-purpose engines allow conditionals, loops and attribute access, so the limit has to be a team's own rule. A common one follows: - **Reasonable in a template:** a presence check for an optional section, and a simple loop over a short list that always prints one way. - **Better in code:** deciding which policy applies to which tier, calculating anything, or building the text of a section from conditions. The reason is testing. Code can be unit-tested without rendering, and a decision moved out of the template arrives as one more validated value. Rendered variants also multiply: each independent optional section doubles the number of shapes, so three such sections give eight, and each shape can behave differently on the model. ## What the contract does not cover Validation checks shape, not meaning. A `ticket_body` that passes every check can still contain text telling the model to ignore its rules, and no type or length rule detects that; prompt-injection mitigation is a separate subject, and so is how to delimit that text once it is in the prompt.

  • A required value is missing in production. Should the feature fail, or call the model anyway?
    Fail the render and take a defined path: retry upstream, queue the request for a person, or show the user a message. Calling anyway spends tokens on a prompt already known to be wrong, and the reply can look plausible, so nobody notices. Count these failures on a metric, since a spike tends to point at an upstream change.
  • Why reject unknown keys instead of ignoring them?
    A misspelled key such as `plan_teir` means the caller thinks it supplied the plan while the real placeholder is unfilled. Ignoring extras hides that, and rejecting them turns it into an immediate error in tests. Strictness adds one rollout rule: deploy the contract change before any caller starts sending the new field.
  • When is a default value better than dropping a section of a prompt template?
    When the text stays true and the sentence needs the slot, such as a greeting that works with a neutral word. Avoid a default that looks like data, because a model may repeat `Unknown` as though it were a name. If the value changes what the model should do, as a plan tier does, fail the render instead.

  • An empty string and a missing variable can be handled the same way.
  • Every template engine raises an error when a variable is undefined.
  • A label with no value is harmless because models ignore blank fields.
  • Validated inputs make a prompt safe from instructions hidden inside a value.
  • Tier rules and other business decisions belong in template conditionals.
  • Fill any missing value with a stand-in word so the model call always proceeds.