Skip to content

Commit 0024441

Browse files
perf: cache TypeAdapter instances across tool invocations
1 parent 04c9cab commit 0024441

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/google/adk/tools/function_tool.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(
9090
self.func = func
9191
self._ignore_params = ['tool_context', 'input_stream']
9292
self._require_confirmation = require_confirmation
93+
self._type_adapter_cache: dict[Any, pydantic.TypeAdapter] = {}
9394

9495
@override
9596
def _get_declaration(self) -> Optional[types.FunctionDeclaration]:
@@ -154,7 +155,11 @@ def _preprocess_args(
154155
# Validate and coerce all annotated types using TypeAdapter.
155156
# This handles primitives, enums, Pydantic models, and container types.
156157
try:
157-
adapter = pydantic.TypeAdapter(target_type)
158+
if target_type not in self._type_adapter_cache:
159+
self._type_adapter_cache[target_type] = pydantic.TypeAdapter(
160+
target_type
161+
)
162+
adapter = self._type_adapter_cache[target_type]
158163
converted_args[param_name] = adapter.validate_python(
159164
args[param_name]
160165
)

0 commit comments

Comments
 (0)