Skip to content

Commit abbc100

Browse files
committed
add name param to tool
1 parent 44d80f8 commit abbc100

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

aws_lambda_powertools/event_handler/bedrock_agent_function.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,26 @@ def __init__(self) -> None:
8080
self.current_event: BedrockAgentFunctionEvent | None = None
8181
self._response_builder_class = BedrockFunctionsResponseBuilder
8282

83-
def tool(self, description: str | None = None) -> Callable:
84-
"""Decorator to register a tool function"""
83+
def tool(
84+
self,
85+
description: str | None = None,
86+
name: str | None = None,
87+
) -> Callable:
88+
"""Decorator to register a tool function
89+
90+
Parameters
91+
----------
92+
description : str | None
93+
Description of what the tool does
94+
name : str | None
95+
Custom name for the tool. If not provided, uses the function name
96+
"""
8597

8698
def decorator(func: Callable) -> Callable:
8799
if not description:
88100
raise ValueError("Tool description is required")
89101

90-
function_name = func.__name__
102+
function_name = name or func.__name__
91103
if function_name in self._tools:
92104
raise ValueError(f"Tool '{function_name}' already registered")
93105

tests/functional/event_handler/required_dependencies/test_bedrock_agent_functions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,23 @@ def test_bedrock_agent_function_invalid_event():
128128
# THEN raise ValueError
129129
with pytest.raises(ValueError, match="Missing required field"):
130130
app.resolve({}, {})
131+
132+
133+
def test_bedrock_agent_function_with_custom_name():
134+
# GIVEN a Bedrock Agent Function resolver
135+
app = BedrockAgentFunctionResolver()
136+
137+
@app.tool(name="customName", description="Function with custom name")
138+
def test_function():
139+
return "Hello from custom named function"
140+
141+
# WHEN calling the event handler
142+
raw_event = load_event("bedrockAgentFunctionEvent.json")
143+
raw_event["function"] = "customName" # Use custom name instead of function name
144+
result = app.resolve(raw_event, {})
145+
146+
# THEN process event correctly
147+
assert result["messageVersion"] == "1.0"
148+
assert result["response"]["actionGroup"] == raw_event["actionGroup"]
149+
assert result["response"]["function"] == "customName"
150+
assert result["response"]["functionResponse"]["responseBody"]["TEXT"]["body"] == "Hello from custom named function"

0 commit comments

Comments
 (0)