Skip to content

teff.prompt

teff.prompt

Prompt template rendering from workflow state.

Functions:

Name Description
render_template

Render {key} placeholders in template from state.

render_template

render_template(template, state)

Render {key} placeholders in template from state.

Values are coerced to strings, so {summ} with an int value renders as-is. A placeholder referencing a missing state key raises KeyError so template mistakes surface early.

Usage::

render_template(
    "create a repair plan for {type} up to {summ}",
    {"type": "kitchen", "summ": 15000},
)
# "create a repair plan for kitchen up to 15000"
Source code in teff/prompt.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def render_template(template: str, state: dict) -> str:
    """Render ``{key}`` placeholders in *template* from *state*.

    Values are coerced to strings, so ``{summ}`` with an ``int`` value
    renders as-is.  A placeholder referencing a missing state key raises
    ``KeyError`` so template mistakes surface early.

    Usage::

        render_template(
            "create a repair plan for {type} up to {summ}",
            {"type": "kitchen", "summ": 15000},
        )
        # "create a repair plan for kitchen up to 15000"
    """
    if "{" not in template:
        return template
    values = {k: str(v) for k, v in state.items()}
    return template.format_map(_TemplateDict(values))