chat_template_mismatchTier 1 · 70% confidence
ai-agents-chat-template-mismat-conversation-with-langchain-s-conversationchain-us-982163c9
agent: ai_agents
When does this happen?
IF Conversation with LangChain's ConversationChain using HuggingFace models results in the model repeating or looping after initial responses due to incorrect chat template formatting.
How others solved it
THEN Ensure the prompt template matches the model's expected chat format. For HuggingFace models, use the tokenizer's apply_chat_template method to format the chat history and input. Alternatively, use LangChain's ChatPromptTemplate with MessagesPlaceholder and a system message that includes context, avoiding manual prompt templates that omit assistant labels or special tokens.
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.llms import HuggingFacePipeline
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained('CohereForAI/aya-23-8B', device_map='auto')
tokenizer = AutoTokenizer.from_pretrained('CohereForAI/aya-23-8B')
pipeline = pipeline('text-generation', model=model, tokenizer=tokenizer, do_sample=True, max_new_tokens=100)
llm = HuggingFacePipeline(pipeline=pipeline)
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
prompt = ChatPromptTemplate.from_messages([
('system', 'You are a helpful assistant.'),
MessagesPlaceholder(variable_name='chat_history'),
('human', '{input}')
])
conversation = ConversationChain(prompt=prompt, llm=llm, memory=memory)
response = conversation.predict(input='Hi there! I am Sam')
print(response)Related patterns
github
ai-agents-github-support-for-reasoning-in-openrouter-and-deepseek-p-48add6f0
Tier 1 · 40%
githubai-agents-github-server-capabilities-not-affecting-the-stream-of-ca-ca806d9e
Tier 1 · 40%
githubai-agents-github-patrick-von-platen-cd4d7ceb
Tier 1 · 40%
model_loadingai-agents-model-loading-loading-a-gemma-3-checkpoint-with-automodelforcaus-cc5b7a71
Tier 1 · 70%
githubai-agents-github-runtimeerror-cuda-error-cublas-status-not-initiali-9b601119
Tier 1 · 40%
githubai-agents-github-bug-frequent-ide-disconnections-disrupting-workflo-e9f35aca
Tier 1 · 40%
Have you seen this in your site?
Connect AgentMinds to match against your tech stack automatically.