Skip to content

Conversation

@ujjwaltwri
Copy link

Summary

init_chat_model(model="...", model_provider="huggingface") currently fails because ChatHuggingFace is instantiated with a model_id instead of a Hugging Face LLM instance. This leads to a validation error on the llm field.

This PR updates the huggingface branch of _init_chat_model_helper to construct a HuggingFaceEndpoint and pass it as llm into ChatHuggingFace, matching the expectations of langchain-huggingface.

Changes

  • For model_provider="huggingface", create a HuggingFaceEndpoint(repo_id=model, task="text-generation", **kwargs) and wrap it in ChatHuggingFace(llm=llm).
  • Add a unit test for init_chat_model(..., model_provider="huggingface") in test_chat_models.py that verifies the correct parameters are forwarded to the Hugging Face endpoint.

Testing

  • pytest libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py -k "huggingface"

Note: running the full test suite locally with pytest libs/langchain_v1 -k "init_chat_model" in my environment raised a no option named 'only_extended' error from pytest configuration. The targeted unit test above passes.

@github-actions github-actions bot added langchain Related to the package `langchain` fix labels Dec 4, 2025
@github-actions github-actions bot added the dependencies Pull requests that update a dependency file label Dec 4, 2025
@ujjwaltwri
Copy link
Author

This PR fixes the HuggingFace integration in init_chat_model for langchain_v1. When model_provider="huggingface" is used, the previous implementation passed the model string directly into ChatHuggingFace, which is invalid because the HF integration expects an instantiated HuggingFaceEndpoint as the llm argument. This PR corrects that behavior by constructing the endpoint with the appropriate parameters and forwarding them properly. A targeted unit test was added to verify that repo_id, task, and temperature are forwarded to the endpoint, and the test was updated to skip cleanly when the optional dependency is not installed. Minor mypy adjustments were made to satisfy the valid-type rule for the dynamic subclass used in testing. All formatting, linting, and tests now pass in CI.

@ccurme ccurme self-assigned this Dec 5, 2025
@mdrxy mdrxy changed the title fix: make init_chat_model work with huggingface provider fix(langchain): make init_chat_model work with huggingface provider Dec 8, 2025
@github-actions github-actions bot added fix and removed fix labels Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file fix langchain Related to the package `langchain`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants