Prompt Templates with Validation

easy
Unit 1 · LLMs0/3 to milestone

Production apps don't paste strings together. They use prompt templates with named slots, and fail fast when a variable is missing.

Implement render(template, variables):

  • Replace each {name} with str(variables[name]). Names are letters, digits and underscores.
  • {{ and }} produce literal { and }, so you can include JSON examples in prompts.
  • If any placeholder has no value, raise KeyError listing all the missing names, sorted, in the message.
  • Extra variables that aren't used are fine.

Also implement placeholders(template) returning the sorted list of distinct names used.

Write it yourself; don't call str.format.

#llm#prompting#strings3 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Prompt Templates with Validation · TechGradify