chore: Correct the callback signatures

PiperOrigin-RevId: 818781277
This commit is contained in:
Xiang (Sean) Zhou
2025-10-13 12:30:08 -07:00
committed by Copybara-Service
parent bb1ea74924
commit fa84bcb575
@@ -288,11 +288,11 @@ from typing import Optional
from google.genai import types
from google.adk.agents.callback_context import CallbackContext
def content_filter_callback(context: CallbackContext) -> Optional[types.Content]:
def content_filter_callback(callback_context: CallbackContext) -> Optional[types.Content]:
"""After agent callback to filter sensitive content."""
# Access the response content through context
if hasattr(context, 'response') and context.response:
response_text = str(context.response)
# Access the response content through callback_context
if hasattr(callback_context, 'response') and callback_context.response:
response_text = str(callback_context.response)
if "confidential" in response_text.lower():
filtered_text = response_text.replace("confidential", "[FILTERED]")
return types.Content(parts=[types.Part(text=filtered_text)])
@@ -308,12 +308,12 @@ from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.adk.agents.callback_context import CallbackContext
def log_model_request(context: CallbackContext, request: LlmRequest) -> Optional[LlmResponse]:
def log_model_request(callback_context: CallbackContext, request: LlmRequest) -> Optional[LlmResponse]:
"""Before model callback to log requests."""
print(f"Model request: {{request.contents}}")
return None # Return None to proceed with original request
def modify_model_response(context: CallbackContext, response: LlmResponse) -> Optional[LlmResponse]:
def modify_model_response(callback_context: CallbackContext, response: LlmResponse) -> Optional[LlmResponse]:
"""After model callback to modify response."""
# Modify response if needed
return response # Return modified response or None for original
@@ -327,25 +327,25 @@ from typing import Any, Dict, Optional
from google.adk.tools.base_tool import BaseTool
from google.adk.tools.tool_context import ToolContext
def validate_tool_input(tool: BaseTool, args: Dict[str, Any], context: ToolContext) -> Optional[Dict]:
def validate_tool_input(tool: BaseTool, tool_args: Dict[str, Any], tool_context: ToolContext) -> Optional[Dict]:
"""Before tool callback to validate input."""
# Validate or modify tool arguments
if "unsafe_param" in args:
del args["unsafe_param"]
return args # Return modified args or None for original
if "unsafe_param" in tool_args:
del tool_args["unsafe_param"]
return tool_args # Return modified args or None for original
def log_tool_result(tool: BaseTool, args: Dict[str, Any], context: ToolContext, result: Dict) -> Optional[Dict]:
def log_tool_result(tool: BaseTool, tool_args: Dict[str, Any], tool_context: ToolContext, result: Dict) -> Optional[Dict]:
"""After tool callback to log results."""
print(f"Tool {{tool.name}} executed with result: {{result}}")
return None # Return None to keep original result
```
## Callback Signature Summary:
- **Agent Callbacks**: `(CallbackContext) -> Optional[types.Content]`
- **Before Model**: `(CallbackContext, LlmRequest) -> Optional[LlmResponse]`
- **After Model**: `(CallbackContext, LlmResponse) -> Optional[LlmResponse]`
- **Before Tool**: `(BaseTool, Dict[str, Any], ToolContext) -> Optional[Dict]`
- **After Tool**: `(BaseTool, Dict[str, Any], ToolContext, Dict) -> Optional[Dict]`
- **Agent Callbacks**: `(callback_context: CallbackContext) -> Optional[types.Content]`
- **Before Model**: `(callback_context: CallbackContext, request: LlmRequest) -> Optional[LlmResponse]`
- **After Model**: `(callback_context: CallbackContext, response: LlmResponse) -> Optional[LlmResponse]`
- **Before Tool**: `(tool: BaseTool, tool_args: Dict[str, Any], tool_context: ToolContext) -> Optional[Dict]`
- **After Tool**: `(tool: BaseTool, tool_args: Dict[str, Any], tool_context: ToolContext, result: Dict) -> Optional[Dict]`
## Important ADK Requirements