-
Notifications
You must be signed in to change notification settings - Fork 303
fix(langchain): extract model name for ChatHuggingFace #1728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vismaytiwari
wants to merge
2
commits into
langfuse:main
Choose a base branch
from
vismaytiwari:fix-langchain-chathuggingface-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """Unit tests for langfuse.langchain.utils model-name extraction.""" | ||
|
|
||
| from langfuse.langchain.utils import _extract_model_name | ||
|
|
||
|
|
||
| def _chat_huggingface_serialized(model_id: str) -> dict: | ||
| """Mirror ``langchain_core.load.dumpd(ChatHuggingFace(...))``. | ||
|
|
||
| ChatHuggingFace is not LangChain-serializable, so it serializes to a | ||
| ``not_implemented`` stub with no ``kwargs``; the model id is only available | ||
| inside the ``repr`` string as ``model_id='...'``. | ||
| """ | ||
| return { | ||
| "lc": 1, | ||
| "type": "not_implemented", | ||
| "id": [ | ||
| "langchain_huggingface", | ||
| "chat_models", | ||
| "huggingface", | ||
| "ChatHuggingFace", | ||
| ], | ||
| "repr": ( | ||
| f"ChatHuggingFace(llm=HuggingFaceEndpoint(repo_id='{model_id}', " | ||
| f"model='{model_id}', task='text-generation'), model_id='{model_id}')" | ||
| ), | ||
| "name": "ChatHuggingFace", | ||
| } | ||
|
|
||
|
|
||
| def test_extract_model_name_chat_huggingface(): | ||
| serialized = _chat_huggingface_serialized("Qwen/Qwen2.5-Coder-32B-Instruct") | ||
|
|
||
| assert _extract_model_name(serialized) == "Qwen/Qwen2.5-Coder-32B-Instruct" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description correctly notes that
ChatHuggingFaceworks with three backends —HuggingFaceEndpoint,HuggingFaceHub, andHuggingFacePipeline.HuggingFacePipelineitself has amodel_idattribute in its own repr, so a wrapped repr could look likeChatHuggingFace(llm=HuggingFacePipeline(model_id='...', ...), model_id='...'). The regex usesre.search(first match), meaning it would pick up the innerHuggingFacePipeline'smodel_idrather thanChatHuggingFace's. In practice both should be the same value, but adding a parameterised test for each backend shape would guard against any future repr change where the two values might differ.Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I parameterised the test to cover all three backends (HuggingFaceEndpoint, HuggingFaceHub, HuggingFacePipeline). The HuggingFacePipeline case specifically exercises the double model_id repr you flagged, where re.search matches the inner one; since both ids resolve to the same model the extracted value is correct, and the test now guards against future drift between them.