You've already forked adk-python
mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Safely handle FunctionDeclaration without a required attribute
Update `_function_declaration_to_tool_param` to use `getattr` when accessing the `required` field within `function_declaration.parameters`. This prevents `AttributeError` when a `FunctionDeclaration`'s parameters schema does not include a `required` attribute Close #2891 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 830225582
This commit is contained in:
committed by
Copybara-Service
parent
2882995289
commit
93aad61198
@@ -525,13 +525,13 @@ def _function_declaration_to_tool_param(
|
||||
},
|
||||
}
|
||||
|
||||
if (
|
||||
function_declaration.parameters
|
||||
and function_declaration.parameters.required
|
||||
):
|
||||
tool_params["function"]["parameters"][
|
||||
"required"
|
||||
] = function_declaration.parameters.required
|
||||
required_fields = (
|
||||
getattr(function_declaration.parameters, "required", None)
|
||||
if function_declaration.parameters
|
||||
else None
|
||||
)
|
||||
if required_fields:
|
||||
tool_params["function"]["parameters"]["required"] = required_fields
|
||||
|
||||
return tool_params
|
||||
|
||||
|
||||
@@ -1066,6 +1066,40 @@ def test_function_declaration_to_tool_param(
|
||||
)
|
||||
|
||||
|
||||
def test_function_declaration_to_tool_param_without_required_attribute():
|
||||
"""Ensure tools without a required field attribute don't raise errors."""
|
||||
|
||||
class SchemaWithoutRequired:
|
||||
"""Mimics a Schema object that lacks the required attribute."""
|
||||
|
||||
def __init__(self):
|
||||
self.properties = {
|
||||
"optional_arg": types.Schema(type=types.Type.STRING),
|
||||
}
|
||||
|
||||
func_decl = types.FunctionDeclaration(
|
||||
name="function_without_required_attr",
|
||||
description="Function missing required attribute",
|
||||
)
|
||||
func_decl.parameters = SchemaWithoutRequired()
|
||||
|
||||
expected = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "function_without_required_attr",
|
||||
"description": "Function missing required attribute",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"optional_arg": {"type": "string"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
assert _function_declaration_to_tool_param(func_decl) == expected
|
||||
|
||||
|
||||
def test_function_declaration_to_tool_param_with_parameters_json_schema():
|
||||
"""Ensure function declarations using parameters_json_schema are handled.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user