Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions langfuse/langchain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def _extract_model_name(
("ChatCohere", "model", None),
("Cohere", "model", None),
("HuggingFaceHub", "model", None),
("ChatHuggingFace", "model_id", None),
("ChatAnyscale", "model_name", None),
("TextGen", "model", "text-gen"),
("Ollama", "model", None),
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test_langchain_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Unit tests for langfuse.langchain.utils model-name extraction."""

import pytest

from langfuse.langchain.utils import _extract_model_name

_MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"


def _chat_huggingface_serialized(inner_repr: 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={inner_repr}, model_id='{_MODEL_ID}')",
"name": "ChatHuggingFace",
}


@pytest.mark.parametrize(
"inner_repr",
[
f"HuggingFaceEndpoint(repo_id='{_MODEL_ID}', model='{_MODEL_ID}', task='text-generation')",
f"HuggingFaceHub(repo_id='{_MODEL_ID}')",
# HuggingFacePipeline exposes its own model_id, so the repr carries two
# model_id='...' occurrences; re.search picks the first (inner) one.
f"HuggingFacePipeline(model_id='{_MODEL_ID}')",
],
)
def test_extract_model_name_chat_huggingface(inner_repr: str):
serialized = _chat_huggingface_serialized(inner_repr)

Comment thread
hassiebp marked this conversation as resolved.
assert _extract_model_name(serialized) == _MODEL_ID