# TypeError: text\_generation() got an unexpected keyword argument 'token'

**URL:** <https://discuss.huggingface.co/t/typeerror-text-generation-got-an-unexpected-keyword-argument-token/165778>\
**Category:** Spaces\
**Created:** [August 5, 2025, 9:37pm UTC](https://discuss.huggingface.co/t/typeerror-text-generation-got-an-unexpected-keyword-argument-token/165778 "2025-08-05T21:37:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OctopusMode](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/octopusmode/32/52155_2.png) [@OctopusMode](https://discuss.huggingface.co/u/OctopusMode)\
**Post date:** [August 5, 2025, 9:37pm UTC](https://discuss.huggingface.co/t/typeerror-text-generation-got-an-unexpected-keyword-argument-token/165778/1 "2025-08-05T21:37:42Z")

</div>

I’m trying to create a Streamlit chatbot using Mistral-7B-Instruct-v0.3, and after entering my input in the chat, I received such error.  
Here’s the code:  
#########################################################################  
import os  
from langchain\_huggingface import HuggingFaceEndpoint  
import streamlit as st  
from langchain\_core.prompts import PromptTemplate  
from langchain\_core.output\_parsers import StrOutputParser

model\_id=“mistralai/Mistral-7B-Instruct-v0.3”

def get\_llm\_hf\_inference(model\_id=model\_id, max\_new\_tokens=128, temperature=0.1):  
“”"  
Returns a language model for HuggingFace inference.  
Parameters:  
- model\_id (str): The ID of the HuggingFace model repository.  
- max\_new\_tokens (int): The maximum number of new tokens to generate.  
- temperature (float): The temperature for sampling from the model.  
Returns:  
- llm (HuggingFaceEndpoint): The language model for HuggingFace inference.  
“”"  
llm = HuggingFaceEndpoint(  
repo\_id=model\_id,  
max\_new\_tokens=max\_new\_tokens,  
temperature=temperature,  
token = os.getenv(“HF\_TOKEN”)  
)  
return llm

# Configure the Streamlit app

st.set\_page\_config(page\_title=“HuggingFace ChatBot”, page\_icon=“🤗”)  
st.title(“Personal HuggingFace ChatBot”)  
st.markdown(f"_This is a simple chatbot that uses the HuggingFace transformers library to generate responses to your text input. It uses the {model\_id}._")

# Initialize session state for avatars

if “avatars” not in st.session\_state:  
st.session\_state.avatars = {‘user’: None, ‘assistant’: None}

# Initialize session state for user text input

if ‘user\_text’ not in st.session\_state:  
st.session\_state.user\_text = None

# Initialize session state for model parameters

if “max\_response\_length” not in st.session\_state:  
st.session\_state.max\_response\_length = 256

if “system\_message” not in st.session\_state:  
st.session\_state.system\_message = “friendly AI conversing with a human user”

if “starter\_message” not in st.session\_state:  
st.session\_state.starter\_message = “Hello, there! How can I help you today?”

# Sidebar for settings

with st.sidebar:  
st.header(“System Settings”)

```
# AI Settings
st.session_state.system_message = st.text_area(
    "System Message", value="You are a friendly AI conversing with a human user."
)
st.session_state.starter_message = st.text_area(
    'First AI Message', value="Hello, there! How can I help you today?"
)

# Model Settings
st.session_state.max_response_length = st.number_input(
    "Max Response Length", value=128
)

# Avatar Selection
st.markdown("*Select Avatars:*")
col1, col2 = st.columns(2)
with col1:
    st.session_state.avatars['assistant'] = st.selectbox(
        "AI Avatar", options=["🤗", "💬", "🤖"], index=0
    )
with col2:
    st.session_state.avatars['user'] = st.selectbox(
        "User Avatar", options=["👤", "👱‍♂️", "👨🏾", "👩", "👧🏾"], index=0
    )
# Reset Chat History
reset_history = st.button("Reset Chat History")

```

# Initialize or reset chat history

if “chat\_history” not in st.session\_state or reset\_history:  
st.session\_state.chat\_history = [{“role”: “assistant”, “content”: st.session\_state.starter\_message}]

def get\_response(system\_message, chat\_history, user\_text,  
eos\_token\_id=[‘User’], max\_new\_tokens=256, get\_llm\_hf\_kws={}):  
“”"  
Generates a response from the chatbot model.  
Args:  
system\_message (str): The system message for the conversation.  
chat\_history (list): The list of previous chat messages.  
user\_text (str): The user’s input text.  
model\_id (str, optional): The ID of the HuggingFace model to use.  
eos\_token\_id (list, optional): The list of end-of-sentence token IDs.  
max\_new\_tokens (int, optional): The maximum number of new tokens to generate.  
get\_llm\_hf\_kws (dict, optional): Additional keyword arguments for the get\_llm\_hf function.  
Returns:  
tuple: A tuple containing the generated response and the updated chat history.  
“”"  
# Set up the model  
hf = get\_llm\_hf\_inference(max\_new\_tokens=max\_new\_tokens, temperature=0.1)

```
# Create the prompt template
prompt = PromptTemplate.from_template(
    (
        "[INST] {system_message}"
        "\nCurrent Conversation:\n{chat_history}\n\n"
        "\nUser: {user_text}.\n [/INST]"
        "\nAI:"
    )
)
# Make the chain and bind the prompt
chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')

# Generate the response
response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
response = response.split("AI:")[-1]

# Update the chat history
chat_history.append({'role': 'user', 'content': user_text})
chat_history.append({'role': 'assistant', 'content': response})
return response, chat_history

```

# Chat interface

chat\_interface = st.container(border=True)  
with chat\_interface:  
output\_container = st.container()  
st.session\_state.user\_text = st.chat\_input(placeholder=“Enter your text here.”)

# Display chat messages

with output\_container:  
# For every message in the history  
for message in st.session\_state.chat\_history:  
# Skip the system message  
if message[‘role’] == ‘system’:  
continue

```
    # Display the chat message using the correct avatar
    with st.chat_message(message['role'], 
                         avatar=st.session_state['avatars'][message['role']]):
        st.markdown(message['content'])

```

# When the user enter new text:

```
if st.session_state.user_text:
    
    # Display the user's new message immediately
    with st.chat_message("user", 
                         avatar=st.session_state.avatars['user']):
        st.markdown(st.session_state.user_text)
        
    # Display a spinner status bar while waiting for the response
    with st.chat_message("assistant", 
                         avatar=st.session_state.avatars['assistant']):

        with st.spinner("Thinking..."):
            # Call the Inference API with the system_prompt, user text, and history
            response, st.session_state.chat_history = get_response(
                system_message=st.session_state.system_message, 
                user_text=st.session_state.user_text,
                chat_history=st.session_state.chat_history,
                max_new_tokens=st.session_state.max_response_length,
            )
            st.markdown(response)

```

##############################################  
Here is the error:  
TypeError: text\_generation() got an unexpected keyword argument ‘token’

Traceback:

```auto
File "/app/src/streamlit_app.py", line 162, in <module>
    response, st.session_state.chat_history = get_response(File "/app/src/streamlit_app.py", line 121, in get_response
    response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))File "/usr/local/lib/python3.9/site-packages/langchain_core/runnables/base.py", line 3046, in invoke
    input_ = context.run(step.invoke, input_, config)File "/usr/local/lib/python3.9/site-packages/langchain_core/runnables/base.py", line 5434, in invoke
    return self.bound.invoke(File "/usr/local/lib/python3.9/site-packages/langchain_core/language_models/llms.py", line 389, in invoke
    self.generate_prompt(File "/usr/local/lib/python3.9/site-packages/langchain_core/language_models/llms.py", line 766, in generate_prompt
    return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)File "/usr/local/lib/python3.9/site-packages/langchain_core/language_models/llms.py", line 971, in generate
    return self._generate_helper(File "/usr/local/lib/python3.9/site-packages/langchain_core/language_models/llms.py", line 792, in _generate_helper
    self._generate(File "/usr/local/lib/python3.9/site-packages/langchain_core/language_models/llms.py", line 1544, in _generate
    self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)File "/usr/local/lib/python3.9/site-packages/langchain_huggingface/llms/huggingface_endpoint.py", line 318, in _call
    response_text = self.client.text_generation(

```

######################  
Please help me!

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [August 5, 2025, 10:38pm UTC](https://discuss.huggingface.co/t/typeerror-text-generation-got-an-unexpected-keyword-argument-token/165778/2 "2025-08-05T22:38:48Z")

</div>

I recommend updating the `huggingface_hub` library to the latest version and [using `max_new_tokens` instead of `max_length`](https://discuss.huggingface.co/t/typeerror-inferenceclient-text-generation-got-an-unexpected-keyword-argument/160999) and [`huggingfacehub_api_token` instead of `token`](https://python.langchain.com/docs/integrations/llms/huggingface_endpoint/).

```bash
pip install -U huggingface_hub[hf_xet]

```

If you really need to use the old version of the code, I think there is a way to downgrade the `huggingface_hub` library.

```bash
pip install huggingface_hub<0.25

```

---

<div class="post-metadata">

**Author:** ![OctopusMode](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/octopusmode/32/52155_2.png) [@OctopusMode](https://discuss.huggingface.co/u/OctopusMode)\
**Post date:** [August 6, 2025, 5:20pm UTC](https://discuss.huggingface.co/t/typeerror-text-generation-got-an-unexpected-keyword-argument-token/165778/3 "2025-08-06T17:20:36Z")

</div>

It still doesn’t work, I get additional error, and it took me so long to build and rebuild app.py on Hugging Face so I’m trying something else now. Thank you!
