PromptTemplateParseException
semantic_kernel.exceptions.PromptTemplateParseException
Stack trace
semantic_kernel.exceptions.PromptTemplateParseException: Failed to parse prompt template: Unexpected token at line 3, column 15
File "app.py", line 42, in create_prompt
prompt = kernel.prompt_template("my_template")
File "semantic_kernel/prompt_template.py", line 88, in __init__
self._parse_template(template_str)
File "semantic_kernel/prompt_template.py", line 120, in _parse_template
raise PromptTemplateParseException(f"Unexpected token at line {line}, column {col}") Why it happens
Semantic Kernel uses a strict syntax for prompt templates that supports variables and expressions. If the template contains invalid syntax, unmatched braces, or unsupported expressions, the parser throws this exception. This often happens when manually editing templates or copying from incompatible sources.
Detection
Validate prompt templates immediately after loading by calling the parse method or constructing the PromptTemplate object inside a try/except block catching PromptTemplateParseException to log detailed syntax errors.
Causes & fixes
Unmatched or missing curly braces in the prompt template string
Ensure all opening braces '{' have corresponding closing braces '}' and that nested braces follow Semantic Kernel syntax rules.
Using unsupported expressions or functions inside the template placeholders
Limit template placeholders to supported variable names and simple expressions as defined in Semantic Kernel documentation.
Including invalid characters or line breaks inside placeholders
Remove line breaks and special characters from inside placeholders; keep expressions on a single line.
Passing a non-string or malformed template object to the prompt constructor
Always pass a properly formatted string as the prompt template; validate input before constructing the PromptTemplate.
Code: broken vs fixed
from semantic_kernel import Kernel
kernel = Kernel()
# This template has unmatched braces causing parse error
prompt_template = "Hello, {user_name! How can I help you today?"
prompt = kernel.prompt_template(prompt_template) # Raises PromptTemplateParseException here import os
from semantic_kernel import Kernel
from semantic_kernel.exceptions import PromptTemplateParseException
kernel = Kernel()
prompt_template = "Hello, {user_name}! How can I help you today?" # Fixed unmatched brace
try:
prompt = kernel.prompt_template(prompt_template) # Now parses correctly
print("Prompt template parsed successfully")
except PromptTemplateParseException as e:
print(f"Prompt template parse error: {e}")
# API keys and environment variables should be set externally via os.environ Workaround
Catch PromptTemplateParseException and sanitize or fallback to a default simple prompt template string to keep the app running while logging the error for later fix.
Prevention
Use Semantic Kernel's prompt template validation utilities during development and CI to catch syntax errors early, and avoid manual edits without validation.