Skip to content

feat: Extend ChatPromptBuilder with insert tag - #11486

Merged
sjrl merged 9 commits into
v3from
add-messages-tag-jinja2
Jun 3, 2026
Merged

feat: Extend ChatPromptBuilder with insert tag#11486
sjrl merged 9 commits into
v3from
add-messages-tag-jinja2

Conversation

@sjrl

@sjrl sjrl commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Add a new messages tag in our ChatPromptBuilder ({% messages %}) to support building prompts like

{% message role="system" %}You are a helpful assistant.{% endmessage %}
{% messages %}
{% message role="user" %}{{ query }}{% endmessage %}

so users can interleave a list of chat messages into their prompt.

I also added support for subscripting (e.g. {% messages[0] %}, {% messages[-1:] %}) to allow users to select which of the incoming messages they would like to include or not.

How did you test it?

Added new unit tests.

Notes for the reviewer

The immediate use case for this would be to use this new format for Agent. So get rid of system_prompt and user_prompt from Agent's init method and replace it with a singular messages_template (or prompt_template) variable. The default prompt_template would be {% messages %} which translates to the Agent expecting the messages variable to be provided at runtime. Then if users want to customize and add new additional messages like a system message they can easily update the prompt_template to

{% message role="system" %}You are a helpful assistant.{% endmessage %}
{% messages %}

and any new jinja2 params added would then be expected runtime params.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@vercel

vercel Bot commented Jun 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Jun 3, 2026 9:47am

Request Review

@sjrl sjrl self-assigned this Jun 2, 2026
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/utils
  jinja2_chat_extension.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl
sjrl marked this pull request as ready for review June 2, 2026 13:11
@sjrl
sjrl requested a review from a team as a code owner June 2, 2026 13:11
@sjrl
sjrl requested review from anakin87 and davidsbatista and removed request for a team and davidsbatista June 2, 2026 13:11
@sjrl

sjrl commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

@anakin87 I'd appreciate your review on this when you're back since it modifies the Jinja2 extension you built.

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some minor comments. Looks good in general

I am just a little afraid of using {% messages %} (very similar to {% message %}). WDYT?
We might also decide to use something like {% insert messages %}

Comment thread haystack/utils/jinja2_chat_extension.py
Comment thread releasenotes/notes/add-messages-jinja-tag-483cca5bda6884f2.yaml
Comment thread test/utils/test_jinja2_chat_extension.py Outdated
@sjrl

sjrl commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

I left some minor comments. Looks good in general

I am just a little afraid of using {% messages %} (very similar to {% message %}). WDYT? We might also decide to use something like {% insert messages %}

Done in e98c8cf

I agree that it's better it also allows for more customization by not requiring the param name to be messages

@sjrl
sjrl requested a review from anakin87 June 3, 2026 09:49

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sjrl
sjrl merged commit 3b1558f into v3 Jun 3, 2026
26 of 27 checks passed
@sjrl
sjrl deleted the add-messages-tag-jinja2 branch June 3, 2026 10:02
@sjrl sjrl changed the title feat: Extend ChatPromptBuilder with messages tag feat: Extend ChatPromptBuilder with insert tag Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants