Skip to content

Commit d87b0cd

Browse files
perf: cache TypeAdapter instances across tool invocations
1 parent a59accb commit d87b0cd

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/google/adk/tools/function_tool.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(
9393
self._context_param_name = find_context_parameter(func) or 'tool_context'
9494
self._ignore_params = [self._context_param_name, 'input_stream']
9595
self._require_confirmation = require_confirmation
96+
self._type_adapter_cache: dict[Any, pydantic.TypeAdapter] = {}
9697

9798
@override
9899
def _get_declaration(self) -> Optional[types.FunctionDeclaration]:
@@ -157,7 +158,11 @@ def _preprocess_args(
157158
# Validate and coerce all annotated types using TypeAdapter.
158159
# This handles primitives, enums, Pydantic models, and container types.
159160
try:
160-
adapter = pydantic.TypeAdapter(target_type)
161+
if target_type not in self._type_adapter_cache:
162+
self._type_adapter_cache[target_type] = pydantic.TypeAdapter(
163+
target_type
164+
)
165+
adapter = self._type_adapter_cache[target_type]
161166
converted_args[param_name] = adapter.validate_python(
162167
args[param_name]
163168
)

0 commit comments

Comments
 (0)