[Python] Best practice for storing multiline prompt templates with placeholders in python?...

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I need to store multiline prompt templates in my python project, and want to:

    • replace placeholders at runtime, e.g. using pythons .format()
    • preserve multiline formatting for human readability regardless of editor

    I currently solve this in 2 ways:

    1. Python-approach: For projects with few (<5) prompt templates


    prompts.py:

    _prompt_template = r"""
    This could be long and include placeholders like: {placeholder}

    (...)

    Such placeholders are replaced at runtime *using pythons .format()*.
    """

    def get_prompt_template():
    return _prompt_template[:]


    code.py:

    from prompts import get_date_quote_prompt_template

    prompt_template = get_prompt_template()

    prompt = prompt_template.format(
    placeholder="some dynamic content")

    2. YAML-approach: For projects with many prompt templates


    prompts.yaml:

    prompt_template: |
    This could be long and include placeholders like: {placeholder}

    (...)

    Such placeholders are replaced at runtime *using pythons .format()*.


    code.py:

    import yaml

    with open('prompts.yaml', 'r') as file:
    config = yaml.safe_load(file)
    prompt_template = config.get('prompt_template')

    prompt = prompt_template.format(
    placeholder="some dynamic content")

    Is there some agreed upon best practice?

    Continue reading...

Compartilhe esta Página