mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
chore: Add instructions for callback signatures
PiperOrigin-RevId: 815549924
This commit is contained in:
committed by
Copybara-Service
parent
84c1faeeef
commit
4b47a0a552
@@ -275,6 +275,76 @@ Use other tools only when the knowledge agent doesn't have enough information.
|
|||||||
7. **Keep TODO for complex**: For complex business logic, leave TODO comments
|
7. **Keep TODO for complex**: For complex business logic, leave TODO comments
|
||||||
8. **Follow current ADK patterns**: Always search for and reference the latest examples from contributing/samples
|
8. **Follow current ADK patterns**: Always search for and reference the latest examples from contributing/samples
|
||||||
|
|
||||||
|
### 🚨 CRITICAL: Callback Correct Signatures
|
||||||
|
ADK supports different callback types with DIFFERENT signatures. Use FUNCTION-based callbacks (never classes):
|
||||||
|
|
||||||
|
## 1. Agent Callbacks (before_agent_callbacks / after_agent_callbacks)
|
||||||
|
|
||||||
|
**✅ CORRECT Agent Callback:**
|
||||||
|
```python
|
||||||
|
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]:
|
||||||
|
"""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)
|
||||||
|
if "confidential" in response_text.lower():
|
||||||
|
filtered_text = response_text.replace("confidential", "[FILTERED]")
|
||||||
|
return types.Content(parts=[types.Part(text=filtered_text)])
|
||||||
|
return None # Return None to keep original response
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Model Callbacks (before_model_callbacks / after_model_callbacks)
|
||||||
|
|
||||||
|
**✅ CORRECT Model Callback:**
|
||||||
|
```python
|
||||||
|
from typing import Optional
|
||||||
|
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]:
|
||||||
|
"""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]:
|
||||||
|
"""After model callback to modify response."""
|
||||||
|
# Modify response if needed
|
||||||
|
return response # Return modified response or None for original
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Tool Callbacks (before_tool_callbacks / after_tool_callbacks)
|
||||||
|
|
||||||
|
**✅ CORRECT Tool Callback:**
|
||||||
|
```python
|
||||||
|
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]:
|
||||||
|
"""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
|
||||||
|
|
||||||
|
def log_tool_result(tool: BaseTool, args: Dict[str, Any], 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]`
|
||||||
|
|
||||||
## Important ADK Requirements
|
## Important ADK Requirements
|
||||||
|
|
||||||
**File Naming & Structure:**
|
**File Naming & Structure:**
|
||||||
|
|||||||
Reference in New Issue
Block a user