Skip to content

init_chat_model doesn't work with huggingface models #28226

@SauravMaheshkar

Description

@SauravMaheshkar

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

Snippet

from langchain.chat_models import init_chat_model


llm = init_chat_model(
    model="microsoft/Phi-3-mini-4k-instruct",
    model_provider="huggingface",
    temperature=0,
    max_tokens=1024,
    timeout=None,
    max_retries=2,
)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/Users/sauravmaheshkar/dev/papersai/mre.py", line 4, in <module>
    llm = init_chat_model(
  File "/Users/sauravmaheshkar/dev/papersai/.venv/lib/python3.10/site-packages/langchain/chat_models/base.py", line 304, in init_chat_model
    return _init_chat_model_helper(
  File "/Users/sauravmaheshkar/dev/papersai/.venv/lib/python3.10/site-packages/langchain/chat_models/base.py", line 393, in _init_chat_model_helper
    return ChatHuggingFace(model_id=model, **kwargs)
  File "/Users/sauravmaheshkar/dev/papersai/.venv/lib/python3.10/site-packages/langchain_huggingface/chat_models/huggingface.py", line 317, in __init__
    super().__init__(**kwargs)
  File "/Users/sauravmaheshkar/dev/papersai/.venv/lib/python3.10/site-packages/langchain_core/load/serializable.py", line 125, in __init__
    super().__init__(*args, **kwargs)
  File "/Users/sauravmaheshkar/dev/papersai/.venv/lib/python3.10/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for ChatHuggingFace
llm
  Field required [type=missing, input_value={'model_id': 'microsoft/P... None, 'max_retries': 2}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/missing

Description

  • I'm trying to use the init_chat_model function to instantiate a model from the huggingface hub.

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 24.0.0: Tue Sep 24 23:36:26 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T8103
Python Version: 3.10.15 (main, Sep 9 2024, 22:43:48) [Clang 18.1.8 ]

Package Information

langchain_core: 0.3.19
langchain: 0.3.7
langsmith: 0.1.143
langchain_anthropic: 0.2.3
langchain_huggingface: 0.1.2
langchain_text_splitters: 0.3.2

Optional packages not installed

langgraph
langserve

Other Dependencies

aiohttp: 3.11.6
anthropic: 0.36.2
async-timeout: 4.0.3
defusedxml: 0.7.1
httpx: 0.27.2
huggingface-hub: 0.26.2
jsonpatch: 1.33
numpy: 1.26.4
orjson: 3.10.11
packaging: 24.2
pydantic: 2.9.2
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
sentence-transformers: 3.3.1
SQLAlchemy: 2.0.36
tenacity: 9.0.0
tokenizers: 0.20.3
transformers: 4.46.3
typing-extensions: 4.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing featuregood first issueGood for newcomersintegrationRelated to a provider partner package integration

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions