# Buggy code
def interpolate(
    template: Union[Dict[Text, Any], Text], values: Dict[Text, Text]
) -> Union[Dict[Text, Any], Text]:
    if isinstance(template, str):
        return interpolate_text(template, values)
    elif isinstance(template, dict):
        for k, v in template.items():
            if isinstance(v, dict):
                interpolate(v, values)
            elif isinstance(v, list):
                template[k] = [interpolate(i, values) for i in v]
            else:
                template[k] = interpolate_text(v, values)
        return template
    return template


# Traceback (TypeError)
Encountered an exception while running action 'utter_sifre'. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.
2020-03-20 07:37:14 DEBUG    rasa.core.processor  - expected string or bytes-like object
Traceback (most recent call last):
File "/build/lib/python3.6/site-packages/rasa/core/processor.py", line 564, in _run_action
events = await action.run(output_channel, nlg, tracker, self.domain)
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/actions/action.py", line 253, in run
message = await nlg.generate(self.template_name, tracker, output_channel.name())
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/template.py", line 77, in generate
template_name, filled_slots, output_channel, **kwargs
File "/build/lib/python3.6/site-packages/rasa/core/nlg/template.py", line 93, in generate_from_slots
return self._fill_template(r, filled_slots, **kwargs)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/template.py", line 119, in _fill_template
template[key] = interpolate(template[key], template_vars)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 51, in interpolate
template[k] = [interpolate(i, values) for i in v]
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 51, in <listcomp>
template[k] = [interpolate(i, values) for i in v]
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 53, in interpolate
template[k] = interpolate_text(v, values)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 17, in interpolate_text
text = re.sub(r"{([^\n{}]+?)}", r"{0[\1]}", template)
File "/usr/local/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
2020-03-20 07:37:14 DEBUG    rasa.core.processor  - Action 'utter_sifre' ended with events '[]'.

# Fixed code
def interpolate(
    template: Union[Dict[Text, Any], Text], values: Dict[Text, Text]
) -> Union[Dict[Text, Any], Text]:
    """Recursively process template and interpolate any text keys.

    Args:
        template: The template that should be interpolated.
        values: A dictionary of keys and the values that those
            keys should be replaced with.

    Returns:
        The template with any replacements made.
    """
    if isinstance(template, str):
        return interpolate_text(template, values)
    elif isinstance(template, dict):
        for k, v in template.items():
            if isinstance(v, dict):
                interpolate(v, values)
            elif isinstance(v, list):
                template[k] = [interpolate(i, values) for i in v]
            elif isinstance(v, str):
                template[k] = interpolate_text(v, values)
        return template
    return template