AI Engineer Problems › Miscellaneous

Prompt Template Variable Hydrator

Easy · Coding · Miscellaneous · Asked at Scale AI, LangChain

Prompt Template Variable Hydrator

When formatting prompts dynamically, you substitute variables into a template. To keep templates clean, they contain placeholders formatted like {variable_name}.

Tasks

Implement the function hydrate_prompt(template, variables, defaults) to:

  1. Substitute all placeholders matching {variable_name} with their values from the variables and defaults dictionaries.
  2. If a placeholder required by the template is missing from both dictionaries, raise a KeyError with the missing key name.
  3. Ensure that if defaults is not provided, it defaults to an empty dictionary.

Solve it in the browser on GenAIDojo