diff --git a/src/google/adk/models/lite_llm.py b/src/google/adk/models/lite_llm.py index 7300fab9ca7..631bee36e69 100644 --- a/src/google/adk/models/lite_llm.py +++ b/src/google/adk/models/lite_llm.py @@ -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. @@ -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) @@ -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, _ = ( diff --git a/tests/unittests/models/test_litellm.py b/tests/unittests/models/test_litellm.py index af993c02fb2..fe09a07ee4d 100644 --- a/tests/unittests/models/test_litellm.py +++ b/tests/unittests/models/test_litellm.py @@ -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."""