From c144b5347cc459496d4fd41e0c63715ffffb4952 Mon Sep 17 00:00:00 2001 From: Thiago Salvatore Date: Wed, 20 Aug 2025 11:31:22 -0700 Subject: [PATCH] fix: Add support for required params Merge https://github.com/google/adk-python/pull/2212 This PR closes issue #2202 ADK was not parsing the required attribute when using LiteLLM, letting the LLM decide what is required vs not, not respecting function definitions. ## Test Plan There's a fork of adk-python that is being running live for over 2 weeks in our production environment with millions of requests per day. Below you can find a screenshot of the unit tests passing. I've also added one change to the test cases to cover this scenario image COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/2212 from thiagosalvatore:main 7de4037d8016389313f3fb22df40c12bac578523 PiperOrigin-RevId: 797393698 --- src/google/adk/models/lite_llm.py | 9 ++++++++- tests/unittests/models/test_litellm.py | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/google/adk/models/lite_llm.py b/src/google/adk/models/lite_llm.py index 7c78d6f1..d160d5f2 100644 --- a/src/google/adk/models/lite_llm.py +++ b/src/google/adk/models/lite_llm.py @@ -380,7 +380,7 @@ def _function_declaration_to_tool_param( for key, value in function_declaration.parameters.properties.items(): properties[key] = _schema_to_dict(value) - return { + tool_params = { "type": "function", "function": { "name": function_declaration.name, @@ -392,6 +392,13 @@ def _function_declaration_to_tool_param( }, } + if function_declaration.parameters.required: + tool_params["function"]["parameters"][ + "required" + ] = function_declaration.parameters.required + + return tool_params + def _model_response_to_chunk( response: ModelResponse, diff --git a/tests/unittests/models/test_litellm.py b/tests/unittests/models/test_litellm.py index 1328ee81..4bd64242 100644 --- a/tests/unittests/models/test_litellm.py +++ b/tests/unittests/models/test_litellm.py @@ -558,8 +558,10 @@ function_declaration_test_cases = [ "nested_key1": types.Schema(type=types.Type.STRING), "nested_key2": types.Schema(type=types.Type.STRING), }, + required=["nested_key1"], ), }, + required=["nested_arg"], ), ), { @@ -581,8 +583,10 @@ function_declaration_test_cases = [ "nested_key2": {"type": "string"}, }, "type": "object", + "required": ["nested_key1"], }, }, + "required": ["nested_arg"], }, }, },