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
7 changes: 7 additions & 0 deletions libs/aws/langchain_aws/chat_models/bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ def validate_environment(self) -> Self:
self.supports_tool_choice_values = ("auto", "any")
elif "nova" in base_model:
self.supports_tool_choice_values = ("auto", "any", "tool")
elif "deepseek" in base_model and "r1-v1" not in base_model:
if "v3-v1" in base_model:
self.supports_tool_choice_values = ("any",)
else:
self.supports_tool_choice_values = ("any", "tool")
else:
self.supports_tool_choice_values = ()

Expand Down Expand Up @@ -1029,6 +1034,8 @@ def bind_tools(
f"for the latest documentation on models that support tool choice."
)
kwargs["tool_choice"] = _format_tool_choice(tool_choice)
elif "deepseek.v3" in self._get_base_model():
kwargs["tool_choice"] = _format_tool_choice("any")

return self.bind(tools=formatted_tools, **kwargs)

Expand Down
67 changes: 66 additions & 1 deletion libs/aws/tests/unit_tests/chat_models/test_bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import base64
import os
from typing import Any, Dict, List, Tuple, Type, Union, cast
from typing import Any, Dict, List, Literal, Tuple, Type, Union, cast
from unittest import mock

import pytest
Expand Down Expand Up @@ -198,6 +198,71 @@ def test_llama_bind_tools_tool_choice_variants(
chat_model.bind_tools([GetWeather], tool_choice="GetWeather")


@pytest.mark.parametrize(
"model,expected_values",
[
("us.deepseek.r1-v1:0", ()),
("deepseek.v3-v1:0", ("any",)),
(
"deepseek.v3-x:0",
(
"any",
"tool",
),
),
],
)
def test_deepseek_supports_tool_choice_values(
model: str, expected_values: tuple[Literal["auto", "any", "tool"], ...]
) -> None:
chat_model = ChatBedrockConverse(model=model, region_name="us-east-1")
assert chat_model.supports_tool_choice_values == expected_values


def test_deepseek_r1_no_tool_choice_support() -> None:
chat_model = ChatBedrockConverse(model="deepseek.r1-v1:0", region_name="us-east-1") # type: ignore[call-arg]

assert chat_model.supports_tool_choice_values == ()

with pytest.raises(ValueError):
chat_model.bind_tools([GetWeather], tool_choice="auto")

with pytest.raises(ValueError):
chat_model.bind_tools([GetWeather], tool_choice="any")

with pytest.raises(ValueError):
chat_model.bind_tools(
[GetWeather], tool_choice={"tool": {"name": "GetWeather"}}
)

with pytest.raises(ValueError):
chat_model.bind_tools([GetWeather], tool_choice="GetWeather")


def test_deepseek_v3_bind_tools_tool_choice_variants() -> None:
chat_model = ChatBedrockConverse(model="deepseek.v3-v1:0", region_name="us-east-1") # type: ignore[call-arg]

chat_model_with_tools = chat_model.bind_tools([GetWeather], tool_choice="any")
assert cast(RunnableBinding, chat_model_with_tools).kwargs["tool_choice"] == {
"any": {}
}

with pytest.raises(ValueError):
chat_model.bind_tools([GetWeather], tool_choice="auto")

with pytest.raises(ValueError):
chat_model.bind_tools([GetWeather], tool_choice="GetWeather")


def test_deepseek_v3_bind_tools_default_tool_choice() -> None:
chat_model = ChatBedrockConverse(model="deepseek.v3-v1:0", region_name="us-east-1") # type: ignore[call-arg]

chat_model_with_tools = chat_model.bind_tools([GetWeather])
assert cast(RunnableBinding, chat_model_with_tools).kwargs["tool_choice"] == {
"any": {}
}


def test__messages_to_bedrock() -> None:
messages = [
SystemMessage(content="sys1"),
Expand Down