This document covers the FunctionTool system, which enables ordinary Python functions to be used as agent tools through automatic schema generation and execution wrapping. Python Function Tools convert function signatures, docstrings, and type hints into tool declarations that LLM agents can understand and invoke.
For information about:
Python Function Tools allow developers to expose Python functions directly to agents without manually defining tool schemas. The system automatically:
types.FunctionDeclaration src/google/adk/tools/function_tool.py90-101str to STRING, int to INTEGER) src/google/adk/tools/_automatic_function_calling_util.py41-58inspect.cleandoc src/google/adk/tools/function_tool.py72-82ToolContext injection for accessing session state and services src/google/adk/tools/function_tool.py168-169AgentTool src/google/adk/tools/agent_tool.py94-108Sources: src/google/adk/tools/function_tool.py39-101 src/google/adk/tools/_automatic_function_calling_util.py41-58 src/google/adk/tools/agent_tool.py94-108
Diagram: Bridging Python code entities to LLM Function Declarations.
Sources: src/google/adk/tools/function_tool.py39-101 src/google/adk/tools/_automatic_function_calling_util.py204-215 src/google/adk/tools/_function_parameter_parse_util.py214-221 src/google/adk/tools/agent_tool.py94-108
The FunctionTool class wraps a Python callable and implements the BaseTool interface. It:
build_function_declaration to create a schema for the LLM src/google/adk/tools/function_tool.py91-98Sources: src/google/adk/tools/function_tool.py39-200
The conversion of Python signatures to JSON schemas is handled by _automatic_function_calling_util.py and _function_parameter_parse_util.py.
The following mapping is used to translate Python types to Gemini/Vertex AI schema types:
| Python Type | LLM Schema Type (types.Type) | File Reference |
|---|---|---|
str, string | STRING | src/google/adk/tools/_automatic_function_calling_util.py42 |
int, integer | INTEGER | src/google/adk/tools/_automatic_function_calling_util.py43 |
float, number | NUMBER | src/google/adk/tools/_automatic_function_calling_util.py44 |
bool, boolean | BOOLEAN | src/google/adk/tools/_automatic_function_calling_util.py45 |
list, array, tuple | ARRAY | src/google/adk/tools/_automatic_function_calling_util.py50-52 |
dict, object | OBJECT | src/google/adk/tools/_automatic_function_calling_util.py53-54 |
Any | TYPE_UNSPECIFIED | src/google/adk/tools/_automatic_function_calling_util.py57 |
If a parameter is annotated with a Pydantic BaseModel, the system generates a nested OBJECT schema src/google/adk/tools/_automatic_function_calling_util.py161 During tool execution, the FunctionTool automatically validates the incoming JSON from the LLM against the Pydantic model using model_validate src/google/adk/tools/function_tool.py146-148
The _function_parameter_parse_util.py also provides advanced parsing for complex types like fixed-length tuples, generating schemas with prefixItems and unevaluatedItems: False src/google/adk/tools/_function_parameter_parse_util.py61-93
Sources: src/google/adk/tools/_automatic_function_calling_util.py41-58 src/google/adk/tools/function_tool.py123-157 src/google/adk/tools/_function_parameter_parse_util.py61-93
The AgentTool class allows an entire agent to be exposed as a tool to another agent src/google/adk/tools/agent_tool.py94-108
input_schema and output_schema from the wrapped agent to define the tool's parameters and response format src/google/adk/tools/agent_tool.py135-187Runner for the child agent, using the same app_name and services as the parent context src/google/adk/tools/agent_tool.py192-234Sources: src/google/adk/tools/agent_tool.py94-234 tests/unittests/tools/test_agent_tool.py143-181
Function tools can access the execution context by including a parameter annotated with ToolContext (or named tool_context).
The system uses find_context_parameter to identify which argument should receive the context src/google/adk/utils/context_utils.py65-97
Context, ToolContext, or CallbackContext src/google/adk/utils/context_utils.py36-62Optional[Context] and string-based forward references src/google/adk/utils/context_utils.py55-59FunctionTool defaults to checking for the name tool_context src/google/adk/tools/function_tool.py85Sources: src/google/adk/utils/context_utils.py36-97 src/google/adk/tools/function_tool.py84-87
Diagram: Logic flow within FunctionTool.run_async.
Sources: src/google/adk/tools/function_tool.py103-200 src/google/adk/tools/_automatic_function_calling_util.py61-93
Before calling the underlying function, FunctionTool checks if all required arguments (those without default values) are provided by the LLM src/google/adk/tools/function_tool.py179-182 If arguments are missing, it returns a structured error message encouraging the model to retry with the missing parameters src/google/adk/tools/function_tool.py186-189
Sources: src/google/adk/tools/function_tool.py174-190
The ADK provides integration wrappers for third-party tool ecosystems, enabling developers to use existing tools within the ADK framework.
ADK supports wrapping tools from the CrewAI and LangChain ecosystems:
CrewaiTool wrapper (moved to google.adk.integrations.crewai) allows CrewAI tools to be integrated src/google/adk/tools/crewai_tool.py19-20LangchainTool wrapper (moved to google.adk.integrations.langchain) supports LangChain's StructuredTool and @tool decorators src/google/adk/tools/langchain_tool.py19-20Sources: src/google/adk/tools/crewai_tool.py src/google/adk/tools/langchain_tool.py
FunctionTool supports a require_confirmation attribute src/google/adk/tools/function_tool.py50 This can be:
True, every call requires user approval.Sources: src/google/adk/tools/function_tool.py46-60
A feature flag JSON_SCHEMA_FOR_FUNC_DECL allows switching between the legacy types.Schema object and a modern parameters_json_schema approach for tool declarations src/google/adk/tools/_automatic_function_calling_util.py210-215 When enabled, it uses _function_tool_declarations.build_function_declaration_with_json_schema src/google/adk/tools/_automatic_function_calling_util.py212-214
Sources: src/google/adk/tools/_automatic_function_calling_util.py210-215 src/google/adk/features/_feature_registry.py23
| Entity | Role | File |
|---|---|---|
FunctionTool | Primary class wrapping Python callables into ADK tools. | src/google/adk/tools/function_tool.py39 |
AgentTool | Wraps an Agent instance to be used as a tool. | src/google/adk/tools/agent_tool.py94 |
build_function_declaration | Utility to generate types.FunctionDeclaration from a function. | src/google/adk/tools/_automatic_function_calling_util.py204 |
_get_pydantic_schema | Generates JSON schema using Pydantic's model_json_schema. | src/google/adk/tools/_automatic_function_calling_util.py153 |
find_context_parameter | Inspects signature to find where to inject ToolContext. | src/google/adk/utils/context_utils.py65 |
_preprocess_args | Handles type conversion (e.g., JSON to Pydantic) before invocation. | src/google/adk/tools/function_tool.py103 |
Sources: src/google/adk/tools/function_tool.py src/google/adk/tools/agent_tool.py src/google/adk/tools/_automatic_function_calling_util.py src/google/adk/utils/context_utils.py
Refresh this wiki