Skip to content

Commit fa2f6ec

Browse files
committed
fix(langchain): read AzureOpenAI API version from openai_api_version
`_extract_model_name` for `AzureOpenAI` checks `serialized_kwargs["openai_api_version"]` to decide whether a version is available, then reads the value from `serialized_kwargs["deployment_version"]` (`langfuse/langchain/utils.py:73-74`). LangChain's `AzureOpenAI` doesn't expose a `deployment_version` field — only `openai_api_version` and `deployment_name` — so `deployment_version` is always `None`, the `isinstance(deployment_version, str)` check fails, and the function returns the bare deployment name with no version suffix. Read the value from the same key that gates the branch (`openai_api_version`). Combined with the deduplication logic added in PR #1203, deployments now produce `<deployment_name>-<api_version>` — matching the documented intent of this code path — while still collapsing if the deployment_name already embeds the version. Add `tests/unit/test_extract_model_name.py` with three regression cases: api_version present, api_version absent, and the dedup path from #1203.
1 parent 8bcc8fa commit fa2f6ec

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

langfuse/langchain/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ def _extract_model_name(
7070

7171
serialized_kwargs = serialized.get("kwargs")
7272
if serialized_kwargs and isinstance(serialized_kwargs, dict):
73+
# LangChain's AzureOpenAI exposes the API version under
74+
# `openai_api_version`, not `deployment_version` — the latter
75+
# is never populated, so previously the version suffix was
76+
# dropped from the resulting model name.
7377
if serialized_kwargs.get("openai_api_version"):
74-
deployment_version = serialized_kwargs.get("deployment_version")
78+
deployment_version = serialized_kwargs.get("openai_api_version")
7579

7680
if serialized_kwargs.get("deployment_name"):
7781
deployment_name = serialized_kwargs.get("deployment_name")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Unit tests for ``langfuse.langchain.utils._extract_model_name``."""
2+
3+
from langfuse.langchain.utils import _extract_model_name
4+
5+
6+
def _azure_openai_serialized(
7+
*, deployment_name: str, openai_api_version: str | None
8+
) -> dict:
9+
"""Build a minimal ``serialized`` dict mirroring what LangChain emits for AzureOpenAI."""
10+
kwargs: dict = {"deployment_name": deployment_name}
11+
if openai_api_version is not None:
12+
kwargs["openai_api_version"] = openai_api_version
13+
return {"id": ["langchain", "llms", "openai", "AzureOpenAI"], "kwargs": kwargs}
14+
15+
16+
def test_azure_openai_returns_deployment_name_with_api_version_suffix():
17+
"""Regression: ``openai_api_version`` should be appended to the deployment name.
18+
19+
Previously the code read the value from a non-existent ``deployment_version``
20+
key after checking that ``openai_api_version`` was set, so the suffix was
21+
silently dropped.
22+
"""
23+
serialized = _azure_openai_serialized(
24+
deployment_name="my-deployment",
25+
openai_api_version="2024-02-15-preview",
26+
)
27+
assert _extract_model_name(serialized) == "my-deployment-2024-02-15-preview"
28+
29+
30+
def test_azure_openai_returns_bare_deployment_name_when_no_api_version():
31+
serialized = _azure_openai_serialized(
32+
deployment_name="my-deployment",
33+
openai_api_version=None,
34+
)
35+
assert _extract_model_name(serialized) == "my-deployment"
36+
37+
38+
def test_azure_openai_no_duplicate_version_suffix():
39+
"""If the deployment_name already contains the version, don't append it again.
40+
41+
This guards the existing behavior introduced in PR #1203.
42+
"""
43+
serialized = _azure_openai_serialized(
44+
deployment_name="my-deployment-2024-02-15-preview",
45+
openai_api_version="2024-02-15-preview",
46+
)
47+
assert _extract_model_name(serialized) == "my-deployment-2024-02-15-preview"

0 commit comments

Comments
 (0)