# Facing issue using a model hosted on HuggingFace Server and talking to it using API\_KEY

**URL:** <https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529>\
**Category:** Beginners\
**Created:** [May 11, 2025, 9:15am UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529 "2025-05-11T09:15:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shaleensr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/shaleensr/32/47299_2.png) [@Shaleensr](https://discuss.huggingface.co/u/Shaleensr)\
**Post date:** [May 11, 2025, 9:15am UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/1 "2025-05-11T09:15:16Z")

</div>

I am trying to create a simple langchain app on text-generation using API to communicate with models on HuggingFace servers.

I created a “.env” file and stored by KEY in the variable: “HUGGINGFACEHUB\_API\_TOKEN”  
I also checked it, API token is valid.

Post that, I tried running this code snippet:

```auto
    from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
    from dotenv import load_dotenv

    load_dotenv()

    llm = HuggingFaceEndpoint(
               repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
               task="text-generation"
    )

    model = ChatHuggingFace(llm=llm)
    result = model.invoke("What is the capital of India")
    print(result.content)

```

This is giving an error. I tried multiple things around it, but nothing worked.

Here is the error log:  
Traceback (most recent call last):  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\2.ChatModels\2\_chatmodel\_hf\_api.py”, line 13, in   
result = model.invoke(“What is the capital of India”)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\langchain\_core\language\_models\chat\_models.py”, line 370, in invoke  
self.generate\_prompt(  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\langchain\_core\language\_models\chat\_models.py”, line 947, in generate\_prompt  
return self.generate(prompt\_messages, stop=stop, callbacks=callbacks, \*\*kwargs)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\langchain\_core\language\_models\chat\_models.py”, line 766, in generate  
self.\_generate\_with\_cache(  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\langchain\_core\language\_models\chat\_models.py”, line 1012, in \_generate\_with\_cache  
result = self.\_generate(  
^^^^^^^^^^^^^^^  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\langchain\_huggingface\chat\_models\huggingface.py”, line 574, in _generate  
answer = self.llm.client.chat\_completion(messages=message\_dicts, \*\*params)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\huggingface\_hub\inference\_client.py”, line 886, in chat\_completion  
provider\_helper = get\_provider\_helper(  
^^^^^^^^^^^^^^^^^^^^  
File "C:\Users\SS\Desktop\Camp\_langchain\_models\venv\Lib\site-packages\huggingface\_hub\inference\_providers\_init_.py", line 165, in get\_provider\_helper  
provider = next(iter(provider\_mapping))  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
StopIteration

I am new to it. Any guidance around this is much appreciated. Thank you.

---

<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:** [May 11, 2025, 10:04am UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/2 "2025-05-11T10:04:01Z")

</div>

I think LangChain has not yet caught up with the changes in Hugging Face’s specifications.

> <https://github.com/huggingface/huggingface_hub/issues/2966>
>
> C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\redi…s\\connection.py:77: UserWarning: redis-py works best with hiredis. Please consider installing
> warnings.warn(msg)
> Write Query Here: describe
> C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\huggingface\_hub\\utils\\\_deprecation.py:131: FutureWarning: 'post' (from 'huggingface\_hub.inference.\_client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. \`InferenceClient.chat\_completion\`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface\_hub.
> warnings.warn(warning\_message, FutureWarning)
> Traceback (most recent call last):
> File "C:\\Users\\Public\\CHATBOT\\llm\_memory\_with\_Model.py", line 59, in \<module\>
> response=qa\_chain.invoke({'query': user\_query})
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 170, in invoke
> raise e
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 160, in invoke
> self.\_call(inputs, run\_manager=run\_manager)
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\retrieval\_qa\\base.py", line 154, in \_call
> answer = self.combine\_documents\_chain.run(
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\\_api\\deprecation.py", line 181, in warning\_emitting\_wrapper
> return wrapped(\*args, \*\*kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 611, in run  
> return self(kwargs, callbacks=callbacks, tags=tags, metadata=metadata)\[
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\\_api\\deprecation.py", line 181, in warning\_emitting\_wrapper
> return wrapped(\*args, \*\*kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 389, in \_\_call\_\_
> return self.invoke(
> ^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 170, in invoke
> raise e
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 160, in invoke
> self.\_call(inputs, run\_manager=run\_manager)
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\combine\_documents\\base.py", line 138, in \_call
> output, extra\_return\_dict = self.combine\_docs(
> ^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\combine\_documents\\stuff.py", line 259, in combine\_docs
> return self.llm\_chain.predict(callbacks=callbacks, \*\*inputs), {}
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\llm.py", line 318, in predict
> return self(kwargs, callbacks=callbacks)\[self.output\_key\]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\\_api\\deprecation.py", line 181, in warning\_emitting\_wrapper
> return wrapped(\*args, \*\*kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 389, in \_\_call\_\_
> return self.invoke(
> ^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 170, in invoke
> raise e
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\base.py", line 160, in invoke
> self.\_call(inputs, run\_manager=run\_manager)
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\llm.py", line 126, in \_call 
> response = self.generate(\[inputs\], run\_manager=run\_manager)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\\chains\\llm.py", line 138, in generate
> return self.llm.generate\_prompt(
> ^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\language\_models\\llms.py", line 763, in generate\_prompt
> return self.generate(prompt\_strings, stop=stop, callbacks=callbacks, \*\*kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\language\_models\\llms.py", line 966, in generate
> output = self.\_generate\_helper(
> ^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\language\_models\\llms.py", line 787, in \_generate\_helper
> self.\_generate(
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_core\\language\_models\\llms.py", line 1526, in \_generate
> self.\_call(prompt, stop=stop, run\_manager=run\_manager, \*\*kwargs)
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain\_huggingface\\llms\\huggingface\_endpoint.py", line 312, in \_call
> response = self.client.post(
> ^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\huggingface\_hub\\utils\\\_deprecation.py", line 132, in inner\_f
> return f(\*args, \*\*kwargs)
> ^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\huggingface\_hub\\inference\\\_client.py", line 302, in post
> mapped\_model = provider\_helper.\_prepare\_mapped\_model(model or self.model)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\huggingface\_hub\\inference\\\_providers\\hf\_inference.py", line 35, in \_prepare\_mapped\_model
> \_check\_supported\_task(model\_id, self.task)
> File "C:\\Users\\suboyina\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\huggingface\_hub\\inference\\\_providers\\hf\_inference.py", line 156, in \_check\_supported\_task
> raise ValueError(
> ValueError: Model 'mistralai/Mistral-7B-Instruct-v0.3' doesn't support task 'unknown'. Supported tasks: 'text-generation', got: 'unknown'

> Meanwhile, one possible solution would be to downgrade your `huggingface-hub` version to 0.27.1 or below.

```auto
pip install huggingface_hub<=0.27.1

```

---

<div class="post-metadata">

**Author:** ![niteshburnwal](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/niteshburnwal/32/47260_2.png) [@niteshburnwal](https://discuss.huggingface.co/u/niteshburnwal)\
**Post date:** [May 11, 2025, 3:13pm UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/3 "2025-05-11T15:13:25Z")

</div>

I am also facing similar issue  
please let me know if you found any solution

---

<div class="post-metadata">

**Author:** ![mahmutc](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mahmutc/32/52583_2.png) [@mahmutc](https://discuss.huggingface.co/u/mahmutc)\
**Post date:** [May 11, 2025, 4:04pm UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/4 "2025-05-11T16:04:11Z")

</div>

`pip install langchain-huggingface langchain`

```auto
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
llm = HuggingFaceEndpoint(
  repo_id="deepseek-ai/DeepSeek-R1",
  provider="together"
)
model = ChatHuggingFace(llm=llm)
result = model.invoke("What is the capital of India")

```

This works for me with the following setup:

```auto
$ pip freeze | grep huggingface
huggingface-hub==0.31.1
langchain-huggingface==0.2.0
$ pip freeze | grep langchain
langchain==0.3.25
langchain-core==0.3.59
langchain-huggingface==0.2.0
langchain-text-splitters==0.3.8

```

---

<div class="post-metadata">

**Author:** ![mahmutc](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mahmutc/32/52583_2.png) [@mahmutc](https://discuss.huggingface.co/u/mahmutc)\
**Post date:** [May 11, 2025, 4:11pm UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/5 "2025-05-11T16:11:55Z")

</div>

Please note the following regarding [TinyLlama/TinyLlama-1.1B-Chat-v1.0](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0):

> This model isn’t deployed by any Inference Provider.

---

<div class="post-metadata">

**Author:** ![Shaleensr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/shaleensr/32/47299_2.png) [@Shaleensr](https://discuss.huggingface.co/u/Shaleensr)\
**Post date:** [May 11, 2025, 4:25pm UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/6 "2025-05-11T16:25:46Z")

</div>

Thank you @mahmutc. This code snippet worked for me.

---

<div class="post-metadata">

**Author:** ![Shaleensr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/shaleensr/32/47299_2.png) [@Shaleensr](https://discuss.huggingface.co/u/Shaleensr)\
**Post date:** [May 11, 2025, 4:28pm UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/7 "2025-05-11T16:28:01Z")

</div>

The below snippet by mahmutc worked for me:

> [@mahmutc](#):
>
> ````auto
> from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
> llm = HuggingFaceEndpoint(
> repo_id="deepseek-ai/DeepSeek-R1",
> provider="together"
> )
> model = ChatHuggingFace(llm=llm)
> result = model.invoke("What is the capital of India")
> ```from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
> llm = HuggingFaceEndpoint(
> repo_id="deepseek-ai/DeepSeek-R1",
> provider="together"
> )
> model = ChatHuggingFace(llm=llm)
> result = model.invoke("What is the capital of India")
> 
> ````

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [May 12, 2025, 4:28am UTC](https://discuss.huggingface.co/t/facing-issue-using-a-model-hosted-on-huggingface-server-and-talking-to-it-using-api-key/154529/8 "2025-05-12T04:28:01Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
