The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(deepseek): inject `model_provider` in `response_metadata` (#33544)
Mason Daughertyde791bc3ef25
diff --git a/libs/partners/deepseek/langchain_deepseek/chat_models.py b/libs/partners/deepseek/langchain_deepseek/chat_models.pyindex 4229f23e16..d7bfa3d6b2 100644--- a/libs/partners/deepseek/langchain_deepseek/chat_models.py+++ b/libs/partners/deepseek/langchain_deepseek/chat_models.py@@ -263,6 +263,11 @@ class ChatDeepSeek(BaseChatOpenAI):if not isinstance(response, openai.BaseModel):return rtn+ for generation in rtn.generations:+ if generation.message.response_metadata is None:+ generation.message.response_metadata = {}+ generation.message.response_metadata["model_provider"] = "deepseek"+choices = getattr(response, "choices", None)if choices and hasattr(choices[0].message, "reasoning_content"):rtn.generations[0].message.additional_kwargs["reasoning_content"] = choices[@@ -294,6 +299,10 @@ class ChatDeepSeek(BaseChatOpenAI):if (choices := chunk.get("choices")) and generation_chunk:top = choices[0]if isinstance(generation_chunk.message, AIMessageChunk):+ generation_chunk.message.response_metadata = {+ **generation_chunk.message.response_metadata,+ "model_provider": "deepseek",+ }if (reasoning_content := top.get("delta", {}).get("reasoning_content")) is not None:diff --git a/libs/partners/deepseek/tests/integration_tests/test_chat_models.py b/libs/partners/deepseek/tests/integration_tests/test_chat_models.pyindex 817162901f..55976552a6 100644--- a/libs/partners/deepseek/tests/integration_tests/test_chat_models.py+++ b/libs/partners/deepseek/tests/integration_tests/test_chat_models.py@@ -10,6 +10,8 @@ from langchain_tests.integration_tests import ChatModelIntegrationTestsfrom langchain_deepseek.chat_models import ChatDeepSeek+MODEL_NAME = "deepseek-chat"+class TestChatDeepSeek(ChatModelIntegrationTests):"""Test `ChatDeepSeek` chat model."""@@ -23,7 +25,7 @@ class TestChatDeepSeek(ChatModelIntegrationTests):def chat_model_params(self) -> dict:"""Parameters to create chat model instance for testing."""return {- "model": "deepseek-chat",+ "model": MODEL_NAME,"temperature": 0,}@@ -49,6 +51,14 @@ def test_reasoning_content() -> None:response = chat_model.invoke("What is 3^3?")assert response.contentassert response.additional_kwargs["reasoning_content"]++ content_blocks = response.content_blocks+ assert content_blocks is not None+ assert len(content_blocks) > 0+ reasoning_blocks = [+ block for block in content_blocks if block.get("type") == "reasoning"+ ]+ assert len(reasoning_blocks) > 0raise ValueError@@ -61,3 +71,11 @@ def test_reasoning_content_streaming() -> None:full = chunk if full is None else full + chunkassert isinstance(full, AIMessageChunk)assert full.additional_kwargs["reasoning_content"]++ content_blocks = full.content_blocks+ assert content_blocks is not None+ assert len(content_blocks) > 0+ reasoning_blocks = [+ block for block in content_blocks if block.get("type") == "reasoning"+ ]+ assert len(reasoning_blocks) > 0diff --git a/libs/partners/deepseek/tests/unit_tests/test_chat_models.py b/libs/partners/deepseek/tests/unit_tests/test_chat_models.pyindex d4bde14000..31be8ab98c 100644--- a/libs/partners/deepseek/tests/unit_tests/test_chat_models.py+++ b/libs/partners/deepseek/tests/unit_tests/test_chat_models.py@@ -13,6 +13,8 @@ from pydantic import SecretStrfrom langchain_deepseek.chat_models import ChatDeepSeek+MODEL_NAME = "deepseek-chat"+class MockOpenAIResponse(BaseModel):"""Mock OpenAI response model."""@@ -35,7 +37,7 @@ class MockOpenAIResponse(BaseModel):context: dict[str, Any] | None = None,serialize_as_any: bool = False,) -> dict[str, Any]:- """Convert to dictionary, ensuring reasoning_content is included."""+ """Convert to dictionary, ensuring `reasoning_content` is included."""choices_list = []for choice in self.choices:if isinstance(choice.message, ChatCompletionMessage):@@ -61,7 +63,7 @@ class MockOpenAIResponse(BaseModel):class TestChatDeepSeekUnit(ChatModelUnitTests):- """Unit tests for `ChatDeepSeek` chat model."""+ """Standard unit tests for `ChatDeepSeek` chat model."""@propertydef chat_model_class(self) -> type[ChatDeepSeek]:@@ -77,7 +79,7 @@ class TestChatDeepSeekUnit(ChatModelUnitTests):"DEEPSEEK_API_BASE": "api_base",},{- "model": "deepseek-chat",+ "model": MODEL_NAME,},{"api_key": "api_key",@@ -89,7 +91,7 @@ class TestChatDeepSeekUnit(ChatModelUnitTests):def chat_model_params(self) -> dict:"""Parameters to create chat model instance for testing."""return {- "model": "deepseek-chat",+ "model": MODEL_NAME,"api_key": "api_key",}@@ -103,7 +105,7 @@ class TestChatDeepSeekCustomUnit:def test_create_chat_result_with_reasoning_content(self) -> None:"""Test that reasoning_content is properly extracted from response."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))mock_message = MagicMock()mock_message.content = "Main content"mock_message.reasoning_content = "This is the reasoning content"@@ -120,8 +122,8 @@ class TestChatDeepSeekCustomUnit:)def test_create_chat_result_with_model_extra_reasoning(self) -> None:- """Test that reasoning is properly extracted from model_extra."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ """Test that reasoning is properly extracted from `model_extra`."""+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))mock_message = MagicMock(spec=ChatCompletionMessage)mock_message.content = "Main content"mock_message.role = "assistant"@@ -143,7 +145,7 @@ class TestChatDeepSeekCustomUnit:def test_convert_chunk_with_reasoning_content(self) -> None:"""Test that reasoning_content is properly extracted from streaming chunk."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))chunk: dict[str, Any] = {"choices": [{@@ -170,7 +172,7 @@ class TestChatDeepSeekCustomUnit:def test_convert_chunk_with_reasoning(self) -> None:"""Test that reasoning is properly extracted from streaming chunk."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))chunk: dict[str, Any] = {"choices": [{@@ -197,7 +199,7 @@ class TestChatDeepSeekCustomUnit:def test_convert_chunk_without_reasoning(self) -> None:"""Test that chunk without reasoning fields works correctly."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))chunk: dict[str, Any] = {"choices": [{"delta": {"content": "Main content"}}]}chunk_result = chat_model._convert_chunk_to_generation_chunk(@@ -212,7 +214,7 @@ class TestChatDeepSeekCustomUnit:def test_convert_chunk_with_empty_delta(self) -> None:"""Test that chunk with empty delta works correctly."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))chunk: dict[str, Any] = {"choices": [{"delta": {}}]}chunk_result = chat_model._convert_chunk_to_generation_chunk(@@ -227,7 +229,7 @@ class TestChatDeepSeekCustomUnit:def test_get_request_payload(self) -> None:"""Test that tool message content is converted from list to string."""- chat_model = ChatDeepSeek(model="deepseek-chat", api_key=SecretStr("api_key"))+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))tool_message = ToolMessage(content=[], tool_call_id="test_id")payload = chat_model._get_request_payload([tool_message])@@ -240,3 +242,72 @@ class TestChatDeepSeekCustomUnit:tool_message = ToolMessage(content="test string", tool_call_id="test_id")payload = chat_model._get_request_payload([tool_message])assert payload["messages"][0]["content"] == "test string"++ def test_create_chat_result_with_model_provider(self) -> None:+ """Test that `model_provider` is added to `response_metadata`."""+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))+ mock_message = MagicMock()+ mock_message.content = "Main content"+ mock_message.role = "assistant"+ mock_response = MockOpenAIResponse(+ choices=[MagicMock(message=mock_message)],+ error=None,+ )++ result = chat_model._create_chat_result(mock_response)+ assert (+ result.generations[0].message.response_metadata.get("model_provider")+ == "deepseek"+ )++ def test_convert_chunk_with_model_provider(self) -> None:+ """Test that `model_provider` is added to `response_metadata` for chunks."""+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))+ chunk: dict[str, Any] = {+ "choices": [+ {+ "delta": {+ "content": "Main content",+ },+ },+ ],+ }++ chunk_result = chat_model._convert_chunk_to_generation_chunk(+ chunk,+ AIMessageChunk,+ None,+ )+ if chunk_result is None:+ msg = "Expected chunk_result not to be None"+ raise AssertionError(msg)+ assert (+ chunk_result.message.response_metadata.get("model_provider") == "deepseek"+ )++ def test_create_chat_result_with_model_provider_multiple_generations(+ self,+ ) -> None:+ """Test that `model_provider` is added to all generations when `n > 1`."""+ chat_model = ChatDeepSeek(model=MODEL_NAME, api_key=SecretStr("api_key"))+ mock_message_1 = MagicMock()+ mock_message_1.content = "First response"+ mock_message_1.role = "assistant"+ mock_message_2 = MagicMock()+ mock_message_2.content = "Second response"+ mock_message_2.role = "assistant"++ mock_response = MockOpenAIResponse(+ choices=[+ MagicMock(message=mock_message_1),+ MagicMock(message=mock_message_2),+ ],+ error=None,+ )++ result = chat_model._create_chat_result(mock_response)+ assert len(result.generations) == 2 # noqa: PLR2004+ for generation in result.generations:+ assert (+ generation.message.response_metadata.get("model_provider") == "deepseek"+ )