Variable substitution in sessions
Overview
When using sessions with the Chat API, understanding how variable substitution works is essential for building dynamic, personalized conversations.
Key concept: Variables are substituted at session creation time and “baked into” the stored assistant configuration. Template placeholders like {{name}} are replaced with actual values and no longer exist in the session.
Vapi uses LiquidJS for variable substitution. The {{ }} syntax follows Liquid template language conventions, giving you access to filters, conditionals, and other Liquid features beyond simple variable replacement.
How variable substitution works
At session creation
When you create a session with assistantOverrides.variableValues, the system:
- Takes your assistant’s template variables (e.g.,
"Hello {{name}} from {{company}}") - Substitutes all
{{ }}placeholders using LiquidJS - Stores the pre-substituted assistant in the session
- Saves the original variable values in
session.metadata.variableValuesfor reference
If your assistant’s system prompt was "You are a helpful assistant for {{name}} at {{company}}", the session stores: "You are a helpful assistant for John at Acme Corp".
At chat creation
When you send a chat request with a sessionId:
- The system loads the session’s pre-substituted assistant
- Any
variableValuesin the chat request are processed, but there are no{{ }}placeholders left to substitute - New variable values have no effect on already-substituted text
Behavior examples
Variables persist across chats
Once you set variables at session creation, they persist for all chats in that session:
The assistant will respond with the values set at session creation (John, Acme Corp).
New variableValues don’t override session values
Passing new variableValues in a chat request will not change the session’s pre-substituted assistant. The template placeholders no longer exist.
The assistant still responds with “John” and “Acme Corp” because the original templates were already replaced.
Provide fresh templates to use new values
To use different variable values mid-session, provide a new template with {{ }} placeholders along with the new values:
Now the assistant responds with “Jane” and “Wayne Enterprises” because fresh template placeholders were provided.
Quick reference
Best practices
For consistent variables across a session
Pass assistantOverrides.variableValues once when creating the session. Subsequent chat requests only need the sessionId and input.
For different variables per conversation
Choose one of these approaches:
Option A: Don't use sessions
Pass the full assistant configuration in each chat request. This gives you complete control over variables per request.
Option B: Override with fresh templates
Include a new system prompt (or other text field) with {{ }} placeholders plus new variableValues in your chat request.
Option C: Create separate sessions
Create a new session for each unique variable context. This keeps conversations cleanly separated.
Next steps
- Session management - Learn about
previousChatIdvssessionIdapproaches - Variables - Configure dynamic variables in your assistant
- Streaming responses - Add real-time responses to your chats