Skip to content
Open
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
22 changes: 18 additions & 4 deletions src/google/adk/models/lite_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4068,7 +4068,7 @@ async def _build_completion_inputs(
list[dict[str, Any]] | None,
dict[str, Any] | None,
dict[str, Any] | None,
str | None,
str | dict[str, Any] | None,
int | None,
]:
"""Converts an LlmRequest to litellm inputs, generation params, and cache index.
Expand Down Expand Up @@ -4372,15 +4372,29 @@ async def _build_completion_inputs(
generation_params = None

# 5. Extract tool_choice from tool_config
tool_choice: str | None = None
tool_choice: str | dict[str, Any] | None = None
if (
llm_request.config
and llm_request.config.tool_config
and llm_request.config.tool_config.function_calling_config
):
mode = llm_request.config.tool_config.function_calling_config.mode
function_calling_config = (
llm_request.config.tool_config.function_calling_config
)
mode = function_calling_config.mode
if mode == types.FunctionCallingConfigMode.ANY:
tool_choice = "required"
allowed_names = function_calling_config.allowed_function_names
# A single allowed name forces that specific tool.
if allowed_names and len(allowed_names) == 1:
declared_names = {
tool.get("function", {}).get("name") for tool in tools or []
}
if allowed_names[0] in declared_names:
tool_choice = {
"type": "function",
"function": {"name": allowed_names[0]},
}
elif mode == types.FunctionCallingConfigMode.NONE:
tool_choice = "none"
# AUTO → None (provider default)
Expand Down Expand Up @@ -4414,7 +4428,7 @@ async def _get_completion_inputs(
list[dict[str, Any]] | None,
dict[str, Any] | None,
dict[str, Any] | None,
str | None,
str | dict[str, Any] | None,
]:
"""Converts an LlmRequest to litellm inputs and extracts generation params."""
messages, tools, response_format, generation_params, tool_choice, _ = (
Expand Down
70 changes: 70 additions & 0 deletions tests/unittests/models/test_litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13452,6 +13452,76 @@ async def test_get_completion_inputs_tool_choice_required_for_any_mode():
assert tool_choice == "required"


@pytest.mark.asyncio
async def test_get_completion_inputs_tool_choice_forces_single_allowed_function():
llm_request = LlmRequest(
contents=[
types.Content(role="user", parts=[types.Part.from_text(text="Hello")])
],
config=types.GenerateContentConfig(
tools=[
types.Tool(
function_declarations=[
types.FunctionDeclaration(
name="my_func", description="A func"
),
types.FunctionDeclaration(
name="other_func", description="Another func"
),
]
)
],
tool_config=types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(
mode=types.FunctionCallingConfigMode.ANY,
allowed_function_names=["my_func"],
)
),
),
)

_, _, _, _, tool_choice = await _get_completion_inputs(
llm_request, model="openai/gpt-4o"
)

assert tool_choice == {"type": "function", "function": {"name": "my_func"}}


@pytest.mark.asyncio
async def test_get_completion_inputs_tool_choice_required_for_multiple_allowed_functions():
llm_request = LlmRequest(
contents=[
types.Content(role="user", parts=[types.Part.from_text(text="Hello")])
],
config=types.GenerateContentConfig(
tools=[
types.Tool(
function_declarations=[
types.FunctionDeclaration(
name="my_func", description="A func"
),
types.FunctionDeclaration(
name="other_func", description="Another func"
),
]
)
],
tool_config=types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(
mode=types.FunctionCallingConfigMode.ANY,
allowed_function_names=["my_func", "other_func"],
)
),
),
)

_, _, _, _, tool_choice = await _get_completion_inputs(
llm_request, model="openai/gpt-4o"
)

assert tool_choice == "required"


@pytest.mark.asyncio
async def test_get_completion_inputs_tool_choice_none_for_none_mode():
"""tool_choice must be 'none' when mode=NONE and tools are present."""
Expand Down