[Python] TypeError: 'NoneType' object is not iterable when using ChatHuggingFace with...

Discussão em 'Python' iniciado por Stack, Julho 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the TinyLlama/TinyLlama-1.1B-Chat-v1.0 model from Hugging Face with LangChain using the langchain_huggingface integration. My goal is to get a simple response from the model using ChatHuggingFace. Here's the code:

    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(input="What is the capital of India")

    print(result)



    But when I run it, I get this error:

    File "...huggingface.py", line 527, in _create_chat_result
    for res in response["choices"]:
    TypeError: 'NoneType' object is not iterable



    It seems like response is None, which causes this error when trying to loop through response["choices"].


    • Verified my Hugging Face token is set via .env and works.


    • Model loads successfully on the Hugging Face web UI.


    • Used "text-generation" as the task based on the model card.


    • Expected model.invoke(...) to return a valid response, but got a NoneType error.


    • Not sure if the model is compatible with ChatHuggingFace or if a different task/prompt format is needed.

    Continue reading...

Compartilhe esta Página