Skip to content

[HANDS-ON BUG] Custom template does not process system message correctly in chat template #278

Description

@ArnoldIOI

Describe the bug
In the custom chat template example provided in smol-course, unit1/2.md, advanced-template-customization, the system_message variable is set inside a for loop:

custom_template = """
{%- for message in messages %}
    {%- if message['role'] == 'system' %}
        {%- set system_message = message['content'] %}
    {%- endif %}
{%- endfor %}
{%- if system_message is defined %}
<|system|>{{ system_message }}<|end|>
{%- endif %}
... (rest omitted)
"""

However, due to Jinja2 scoping rules, variables set inside a loop are not accessible outside that loop's scope. As a result, system_message will not be defined in the template body outside the loop, and the system message will not be rendered properly.

To Reproduce
add a system message to the debug_messages, and watch the formatted text in the output, no system message will be shown

# Example usage
debug_messages = [
    {"role": "system", "content": "You are a helpful assistant."}, # add a system message
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hi there!"}
]

Screenshots

Image

Additional context
Add any other context about the problem here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions