Skip to content

Commit b45f987

Browse files
authored
fix(langchain): extract model name for ChatHuggingFace (#1728)
1 parent b7c5886 commit b45f987

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

langfuse/langchain/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def _extract_model_name(
9797
("ChatCohere", "model", None),
9898
("Cohere", "model", None),
9999
("HuggingFaceHub", "model", None),
100+
("ChatHuggingFace", "model_id", None),
100101
("ChatAnyscale", "model_name", None),
101102
("TextGen", "model", "text-gen"),
102103
("Ollama", "model", None),

tests/unit/test_langchain_utils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Unit tests for langfuse.langchain.utils model-name extraction."""
2+
3+
import pytest
4+
5+
from langfuse.langchain.utils import _extract_model_name
6+
7+
_MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
8+
9+
10+
def _chat_huggingface_serialized(inner_repr: str) -> dict:
11+
"""Mirror ``langchain_core.load.dumpd(ChatHuggingFace(...))``.
12+
13+
ChatHuggingFace is not LangChain-serializable, so it serializes to a
14+
``not_implemented`` stub with no ``kwargs``; the model id is only available
15+
inside the ``repr`` string as ``model_id='...'``.
16+
"""
17+
return {
18+
"lc": 1,
19+
"type": "not_implemented",
20+
"id": [
21+
"langchain_huggingface",
22+
"chat_models",
23+
"huggingface",
24+
"ChatHuggingFace",
25+
],
26+
"repr": f"ChatHuggingFace(llm={inner_repr}, model_id='{_MODEL_ID}')",
27+
"name": "ChatHuggingFace",
28+
}
29+
30+
31+
@pytest.mark.parametrize(
32+
"inner_repr",
33+
[
34+
f"HuggingFaceEndpoint(repo_id='{_MODEL_ID}', model='{_MODEL_ID}', task='text-generation')",
35+
f"HuggingFaceHub(repo_id='{_MODEL_ID}')",
36+
# HuggingFacePipeline exposes its own model_id, so the repr carries two
37+
# model_id='...' occurrences; re.search picks the first (inner) one.
38+
f"HuggingFacePipeline(model_id='{_MODEL_ID}')",
39+
],
40+
)
41+
def test_extract_model_name_chat_huggingface(inner_repr: str):
42+
serialized = _chat_huggingface_serialized(inner_repr)
43+
44+
assert _extract_model_name(serialized) == _MODEL_ID

0 commit comments

Comments
 (0)