Skip to content

Commit 6e44f37

Browse files
pharshalharche
authored andcommitted
fix: handle raw dict output_schema in SetModelResponseTool
When output_schema is a raw dict instance (e.g., {"type": "object", "properties": {...}}), SetModelResponseTool sets it as the parameter annotation directly. This crashes in _is_builtin_primitive_or_compound because dict instances are not hashable and the check does `annotation in dict.keys()`. Fix: add an explicit isinstance(output_schema, dict) branch that uses `dict` (the type) as the annotation instead of the dict instance. Fixes #5469
1 parent 909a8c2 commit 6e44f37

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/google/adk/tools/set_model_response_tool.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,17 @@ def set_model_response() -> str:
8787
annotation=list[inner_type],
8888
)
8989
]
90+
elif isinstance(output_schema, dict):
91+
# Use `dict` type, not the instance — dict instances are unhashable.
92+
params = [
93+
inspect.Parameter(
94+
'response',
95+
inspect.Parameter.KEYWORD_ONLY,
96+
annotation=dict,
97+
)
98+
]
9099
else:
91-
# For other schema types (list[str], dict, etc.),
100+
# For other schema types (list[str], Schema, GenericAlias, etc.),
92101
# create a single parameter with the actual schema type
93102
params = [
94103
inspect.Parameter(

tests/unittests/tools/test_set_model_response_tool.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,18 @@ async def test_run_async_dict_schema():
467467
assert result is not None
468468
assert isinstance(result, dict)
469469
assert result == {'a': 1, 'b': 2, 'c': 3}
470+
471+
472+
def test_tool_initialization_raw_dict_instance():
473+
"""Test tool initialization with a raw dict schema instance."""
474+
raw_schema = {'type': 'object', 'properties': {'name': {'type': 'string'}}}
475+
tool = SetModelResponseTool(raw_schema)
476+
477+
assert tool.output_schema == raw_schema
478+
assert not tool._is_basemodel
479+
assert not tool._is_list_of_basemodel
480+
481+
sig = inspect.signature(tool.func)
482+
assert 'response' in sig.parameters
483+
assert len(sig.parameters) == 1
484+
assert sig.parameters['response'].annotation is dict

0 commit comments

Comments
 (0)