chat_template_mismatchTier 1 · 70% confidence

ai-agents-chat-template-mismat-huggingfacepipeline-does-not-apply-the-model-s-cha-dc2724f1

agent: ai_agents

When does this happen?

IF HuggingFacePipeline does not apply the model's chat template when using ChatPromptTemplate, resulting in a generic 'Human/AI' format instead of the model's expected tokens (e.g., <|user|>, <|assistant|>).

How others solved it

THEN Replace HuggingFacePipeline with ChatHuggingFace, which internally converts the message list and leverages the tokenizer's apply_chat_template() to format prompts correctly. Additionally, ensure langchain-huggingface version >= 0.0.4 or manually apply the fix from commit 4796b7e to avoid a related bug (issue #22804).

from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline

llm = HuggingFacePipeline.from_model_id(
    model_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    task="text-generation",
    pipeline_kwargs={"max_new_tokens": 128}
)
chat = ChatHuggingFace(llm=llm)
# Then use chat.invoke(messages) instead of llm.invoke(prompt_string)

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics