Variables and rendering
Select prompt content and validate variable substitutions before inference.
Use a configured client and an existing prompt with the expected variable definitions. Active retrieval requires a previously activated version.
const active = await nirmos.managedPrompts.retrieveActive("support-reply");
const latest = await nirmos.managedPrompts.retrieveLatest("support-reply");
const pinned = await nirmos.managedPrompts.retrieve("support-reply", { version: 3 });
console.log(active.version.number, latest.version.number, pinned.version.number);Active follows the backend active-version pointer. Latest may include a draft. Numeric selection is a content version number, not the string version ID used by management methods.
Render an already retrieved prompt
const prompt = await nirmos.managedPrompts.retrieve("support-reply");
const rendered = await nirmos.managedPrompts.render(prompt, { customerName: "Asha", "order.id": "order_123" });
console.log(rendered.promptId, rendered.promptVersion, rendered.messages);Supplying a Prompt renders locally. Supplying { id, version?, cache? } first retrieves it. Required defaults resolve before interpolation. allowUnresolved: true leaves unmatched placeholders in content but does not suppress missing required variable errors.
const prompt = await nirmos.managedPrompts.retrieveLatest("support-reply", { cache: "reload" });
const rendered = await nirmos.managedPrompts.render(prompt, { customerName: "Asha" }, { allowUnresolved: true });
console.log(rendered.messages);Retrieval options include RequestOptions; render options do not include timeout/signal. Default cached concurrent retrieval shares one request by identifier/version, so callers should not assume independent cancellation on shared lookups.
Validate before sending
This example is entirely local; no stored prompt or provider is needed.
import { PromptRenderError, type Prompt, type PromptVariable } from "@nirmos/sdk";
const client = nirmos; // Reuse your configured client; this example makes no HTTP calls.
const variables: PromptVariable[] = [
{ name: "customer.name", type: "string", required: true, minLength: 1 },
{ name: "tone", type: "enum", values: ["formal", "friendly"], default: "friendly" },
{ name: "count", type: "number", min: 1, max: 3, required: true },
];
const content = { type: "text" as const, text: "{{customer.name}}: {{tone}}, {{count}}" };
const prompt: Prompt = {
id: "local", name: "Local example", slug: "local", status: "draft",
version: { id: "local-v1", number: 1, status: "draft", active: false, content, variables, metadata: {} },
content, variables, metadata: {}, tags: [], requestMetadata: {},
};
try {
await client.managedPrompts.render(prompt, { customer: { name: "Asha" }, count: "2" });
throw new Error("Expected validation failure");
} catch (error) {
if (!(error instanceof PromptRenderError)) throw error;
console.log(error.message, error.missingVariables); // count: Enter a valid number.; ["count"]
}
const rendered = await client.managedPrompts.render(prompt, { customer: { name: "Asha" }, count: 2 });
console.log(rendered.messages); // Asha: friendly, 2missingVariables also identifies invalid variable names, not only absent values. Use the rendering contract for empty/null values and constraint limits.
Rendering contract
Placeholders accept {{name}}, {{ spaced }} and {{user.name}}; names start with a letter and may contain letters, digits, underscores, dots and hyphens. Literal flat keys win over nested dotted paths. Nested lookup uses own properties and rejects prototype traversal.
For declared variables, the renderer applies defaults only to undefined, then validates values before substitution:
| Definition | Local behavior |
|---|---|
required | Rejects undefined and ""; a default can satisfy an omitted value. Whitespace-only strings are not empty. |
string, number, boolean, array | Checks JavaScript type; numbers must be finite. No string-to-number/boolean coercion. |
enum | Compares String(value) with values; use string values for predictable results. |
min, max | Inclusive bounds on numeric values. |
minLength, maxLength | Inclusive JavaScript string-length bounds. |
itemType | Checks array item types; per-item range/length/finiteness and enum membership are not validated. |
Optional undefined and empty strings bypass value constraints. An optional empty string renders empty; an unresolved undefined token fails unless allowUnresolved: true. That option does not bypass required/value validation. null does not trigger a default: declared string/number/boolean/array types reject it; untyped substitutions render "null". Scalars become strings and objects/arrays become JSON. Extra undeclared values can be substituted but have no definition-based validation.
Loops, conditions, filters and unsupported expressions raise PromptRenderError. Advanced compile preview is a separate contract. See typed failure example.