Skip to content

Conversation

aaron-seq
Copy link

Description:

This change fixes a ValidationError that occurred when using init_chat_model with model_provider="huggingface".

The previous implementation incorrectly passed all keyword arguments directly to the ChatHuggingFace constructor, which expects an llm object rather than a model_id. This caused a Pydantic error when arguments like task were provided.

The fix updates the logic to first correctly instantiate a HuggingFacePipeline object using the from_model_id class method, which properly handles arguments like task. This initialized pipeline object is then passed to the ChatHuggingFace wrapper, resolving the issue.

A unit test has been added to verify the fix and prevent regressions.

Closes #28226

Issue:

Fixes #28226

Dependencies:

None

Copy link

vercel bot commented Sep 30, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchain Ignored Ignored Preview Oct 1, 2025 3:13pm

@github-actions github-actions bot added the langchain Related to the package `langchain` label Sep 30, 2025
Copy link
Collaborator

@eyurtsev eyurtsev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronseq12 thanks for the contribution -- there seem to be a lot of typos in this PR, do you want to try and resolve?

@aaron-seq
Copy link
Author

@eyurtsev yup got some time free, will be working on this!
thanks

- Move exception messages to variables to satisfy EM101 rule
- Use shorter, more concise error messages to satisfy TRY003 rule
@aaron-seq
Copy link
Author

aaron-seq commented Oct 8, 2025

Linting Issues Fixed

I've resolved the Ruff linting errors that were preventing this PR from passing checks:

Issues Resolved:

EM101 Rule Violations:

  • Moved exception messages to variables instead of using string literals directly in raise statements
  • Applied this fix to both ImportError and ValueError exceptions in the HuggingFace initialization code

TRY003 Rule Violations:

  • Shortened exception messages to be more concise and avoid "long messages outside the exception class"
  • Made error messages more specific and actionable

Changes Made:

  1. ImportError handling:

    # Before: raise ImportError("Please install langchain-huggingface...")
    # After:
    import_error_msg = "Please install langchain-huggingface to use HuggingFace models."
    raise ImportError(import_error_msg) from e
  2. ValueError handling:

    # Before: raise ValueError("The 'task' keyword argument is required...")
    # After:
    task_error_msg = "The 'task' keyword argument is required for HuggingFace models."
    raise ValueError(task_error_msg)

The PR should now pass all linting checks while maintaining the same functional behavior for HuggingFace model initialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

langchain Related to the package `langchain`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

init_chat_model doesn't work with huggingface models

3 participants