deepset-ai/haystack · error · ValueError

The templatize_part filter cannot be used with a template co

Error message

The templatize_part filter cannot be used with a template containing a list ofChatMessage objects. Use a string template or remove the templatize_part filter from the template.

What it means

ChatPromptBuilder rejects the custom 'templatize_part' filter when the template is a list of ChatMessage objects. The filter only works with plain string templates, so __init__ raises to prevent misuse that cannot be resolved per-message.

Source

Thrown at haystack/components/builders/chat_prompt_builder.py:180

        """
        self._variables = variables
        self._required_variables = required_variables
        self.template = template

        self._env = HaystackSandboxedEnvironment(extensions=[ChatMessageExtension])
        if arrow_import.is_successful():
            self._env.add_extension(Jinja2TimeExtension)

        extracted_variables = []
        if template and not variables:
            if isinstance(template, list):
                for message in template:
                    if message.is_from(ChatRole.USER) or message.is_from(ChatRole.SYSTEM):
                        # infer variables from template
                        if message.text is None:
                            raise ValueError(NO_TEXT_ERROR_MESSAGE.format(role=message.role.value, message=message))
                        if message.text and "templatize_part" in message.text:
                            raise ValueError(FILTER_NOT_ALLOWED_ERROR_MESSAGE)
                        assigned_variables, template_variables = _extract_template_variables_and_assignments(
                            env=self._env, template=message.text
                        )
                        extracted_variables += list(template_variables - assigned_variables)
            elif isinstance(template, str):
                assigned_variables, template_variables = _extract_template_variables_and_assignments(
                    env=self._env, template=template
                )
                extracted_variables = list(template_variables - assigned_variables)

        extracted_variables = extracted_variables or []
        self.variables = variables or extracted_variables
        self.required_variables = required_variables or []

        if len(self.variables) > 0 and required_variables is None:
            logger.warning(
                "ChatPromptBuilder has {length} prompt variables and `required_variables` is explicitly set to "
                "`None`. This treats all prompt variables as optional, which may lead to unintended behavior in "

View on GitHub (pinned to e318778c9b)

Solutions

  1. Remove the templatize_part filter from the message texts.
  2. Switch to ChatPromptBuilder(template='...string...', variables=[...]) so templatize_part is supported.
  3. Pre-render or restructure the template so the partial-templatizing logic is done in Python before building messages.

Example fix

// before
ChatPromptBuilder(template=[ChatMessage.from_user('{{ doc | templatize_part }}')])
// after
ChatPromptBuilder(template='{{ doc | templatize_part }}', variables=['doc'])
Defensive patterns

Strategy: validation

Validate before calling

bad = [m for m in template if m.text and 'templatize_part' in m.text]
assert not bad, 'templatize_part not allowed in ChatMessage list templates'

Type guard

def uses_allowed_filters(m) -> bool:
    return not (m.text and 'templatize_part' in m.text)

Try / catch

try:
    builder = ChatPromptBuilder(template=msgs)
except ValueError as e:
    if 'templatize_part' in str(e):
        builder = ChatPromptBuilder(template=''.join(m.text or '' for m in msgs), variables=[...])
    else:
        raise

Prevention

When it happens

Trigger: Calling ChatPromptBuilder(template=[ChatMessage.from_user('... {{ x | templatize_part }} ...')]) with a message text containing 'templatize_part' in __init__.

Common situations: Migrating a PromptBuilder string template that used templatize_part into ChatPromptBuilder message-list form; copy-pasting template code across the two builders.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/3981015802b28ebf. Report an issue: GitHub.