Skip to content

Commit 9307a0e

Browse files
committed
fix: bug referencing wrong variable
1 parent 5def990 commit 9307a0e

File tree

3 files changed

+91
-22
lines changed

3 files changed

+91
-22
lines changed

src/strands/tools/decorator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def validate_input(self, input_data: dict[str, Any]) -> dict[str, Any]:
268268
error_msg = str(e)
269269
raise ValueError(f"Validation failed for input parameters: {error_msg}") from e
270270

271-
def _inject_special_parameters(
271+
def inject_special_parameters(
272272
self, validated_input: dict[str, Any], tool_use: ToolUse, invocation_state: dict[str, Any]
273273
) -> None:
274274
"""Inject special framework-provided parameters into the validated input.
@@ -444,7 +444,7 @@ async def stream(self, tool_use: ToolUse, invocation_state: dict[str, Any], **kw
444444
validated_input = self._metadata.validate_input(tool_input)
445445

446446
# Inject special framework-provided parameters
447-
self._metadata._inject_special_parameters(validated_input, tool_use, invocation_state)
447+
self._metadata.inject_special_parameters(validated_input, tool_use, invocation_state)
448448

449449
# "Too few arguments" expected, hence the type ignore
450450
if inspect.iscoroutinefunction(self._tool_func):

tests/strands/tools/test_decorator.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
import strands
11+
from strands import Agent
1112
from strands.types.tools import StrandsContext, ToolUse
1213

1314

@@ -1043,7 +1044,7 @@ async def test_strands_context_injection(alist):
10431044
"""Test that StrandsContext is properly injected into tools that request it."""
10441045

10451046
@strands.tool
1046-
def context_tool(message: str, strands_context: StrandsContext) -> dict:
1047+
def context_tool(message: str, agent: Agent, strands_context: StrandsContext) -> dict:
10471048
"""Tool that uses StrandsContext to access tool_use_id."""
10481049
tool_use_id = strands_context["tool_use"]["toolUseId"]
10491050
tool_name = strands_context["tool_use"]["name"]
@@ -1052,7 +1053,13 @@ def context_tool(message: str, strands_context: StrandsContext) -> dict:
10521053
return {
10531054
"status": "success",
10541055
"content": [
1055-
{"text": f"Tool '{tool_name}' (ID: {tool_use_id}) with agent '{agent_info}' processed: {message}"}
1056+
{
1057+
"text": f"""
1058+
Tool '{tool_name}' (ID: {tool_use_id})
1059+
with agent '{agent_info}'
1060+
and injected agent '{agent}' processed: {message}
1061+
"""
1062+
}
10561063
],
10571064
}
10581065

@@ -1067,10 +1074,12 @@ def context_tool(message: str, strands_context: StrandsContext) -> dict:
10671074
assert result["toolUseId"] == "test-context-123"
10681075
assert "Tool 'context_tool' (ID: test-context-123)" in result["content"][0]["text"]
10691076
assert "with agent 'test-agent'" in result["content"][0]["text"]
1077+
assert "and injected agent 'test-agent'" in result["content"][0]["text"]
10701078
assert "processed: hello world" in result["content"][0]["text"]
10711079

1072-
# Verify strands_context is excluded from schema
1080+
# Verify strands_context and agent are excluded from schema
10731081
tool_spec = context_tool.tool_spec
10741082
schema_properties = tool_spec["inputSchema"]["json"].get("properties", {})
10751083
assert "message" in schema_properties
10761084
assert "strands_context" not in schema_properties
1085+
assert "agent" not in schema_properties

tests_integ/test_strands_context_integration.py

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,91 @@
1414
@tool
1515
def tool_with_context(message: str, strands_context: StrandsContext) -> dict:
1616
"""Tool that uses StrandsContext to access tool_use_id."""
17-
tool_use_id = strands_context["tool_use"]["toolUseId"]
18-
return {"status": "success", "content": [{"text": f"Context tool processed '{message}' with ID: {tool_use_id}"}]}
17+
try:
18+
print(f"DEBUG: tool_with_context called with message='{message}'")
19+
print(f"DEBUG: strands_context type: {type(strands_context)}")
20+
print(f"DEBUG: strands_context contents: {strands_context}")
21+
22+
tool_use_id = strands_context["tool_use"]["toolUseId"]
23+
print(f"DEBUG: Successfully extracted tool_use_id: {tool_use_id}")
24+
25+
result = {
26+
"status": "success",
27+
"content": [{"text": f"Context tool processed '{message}' with ID: {tool_use_id}"}],
28+
}
29+
print(f"DEBUG: Returning result: {result}")
30+
return result
31+
32+
except Exception as e:
33+
print(f"ERROR in tool_with_context: {type(e).__name__}: {e}")
34+
print(f"ERROR: strands_context = {strands_context}")
35+
import traceback
36+
37+
traceback.print_exc()
38+
return {"status": "error", "content": [{"text": f"Error: {str(e)}"}]}
1939

2040

2141
@tool
22-
def tool_with_agent_and_context(message: str, agent, strands_context: StrandsContext) -> dict:
42+
def tool_with_agent_and_context(message: str, agent: Agent, strands_context: StrandsContext) -> dict:
2343
"""Tool that uses both agent and StrandsContext."""
24-
tool_use_id = strands_context["tool_use"]["toolUseId"]
25-
agent_name = getattr(agent, "name", "unknown-agent")
26-
return {
27-
"status": "success",
28-
"content": [{"text": f"Agent '{agent_name}' processed '{message}' with ID: {tool_use_id}"}],
29-
}
44+
try:
45+
print(f"DEBUG: tool_with_agent_and_context called with message='{message}'")
46+
print(f"DEBUG: agent type: {type(agent)}")
47+
print(f"DEBUG: strands_context type: {type(strands_context)}")
48+
print(f"DEBUG: strands_context contents: {strands_context}")
49+
50+
tool_use_id = strands_context["tool_use"]["toolUseId"]
51+
print(f"DEBUG: Successfully extracted tool_use_id: {tool_use_id}")
52+
53+
agent_name = getattr(agent, "name", "unknown-agent")
54+
print(f"DEBUG: Agent name: {agent_name}")
55+
56+
result = {
57+
"status": "success",
58+
"content": [{"text": f"Agent '{agent_name}' processed '{message}' with ID: {tool_use_id}"}],
59+
}
60+
print(f"DEBUG: Returning result: {result}")
61+
return result
62+
63+
except Exception as e:
64+
print(f"ERROR in tool_with_agent_and_context: {type(e).__name__}: {e}")
65+
print(f"ERROR: agent = {agent}")
66+
print(f"ERROR: strands_context = {strands_context}")
67+
import traceback
68+
69+
traceback.print_exc()
70+
return {"status": "error", "content": [{"text": f"Error: {str(e)}"}]}
3071

3172

3273
def test_strands_context_integration():
3374
"""Test StrandsContext functionality with real agent interactions."""
75+
try:
76+
print("DEBUG: Starting test_strands_context_integration")
77+
78+
# Initialize agent with tools
79+
print("DEBUG: Initializing agent with tools")
80+
agent = Agent(tools=[tool_with_context, tool_with_agent_and_context])
81+
print(f"DEBUG: Agent created: {agent}")
82+
83+
# Test tool with StrandsContext
84+
print("DEBUG: Testing tool_with_context")
85+
result1 = agent.tool.tool_with_context(message="hello world")
86+
print(f"DEBUG: tool_with_context result: {result1}")
87+
assert result1.get("status") == "success"
88+
print("DEBUG: tool_with_context assertion passed")
89+
90+
# Test tool with both agent and StrandsContext
91+
print("DEBUG: Testing tool_with_agent_and_context")
92+
result = agent.tool.tool_with_agent_and_context(message="hello agent")
93+
print(f"DEBUG: tool_with_agent_and_context result: {result}")
94+
assert result.get("status") == "success"
95+
print("DEBUG: tool_with_agent_and_context assertion passed")
3496

35-
# Initialize agent with tools
36-
agent = Agent(tools=[tool_with_context, tool_with_agent_and_context])
97+
print("DEBUG: All tests passed successfully")
3798

38-
# Test tool with StrandsContext
39-
result1 = agent.tool.tool_with_context(message="hello world")
40-
assert result1.get("status") == "success"
99+
except Exception as e:
100+
print(f"ERROR in test_strands_context_integration: {type(e).__name__}: {e}")
101+
import traceback
41102

42-
# Test tool with both agent and StrandsContext
43-
result = agent.tool.tool_with_agent_and_context(message="hello agent")
44-
assert result.get("status") == "success"
103+
traceback.print_exc()
104+
raise

0 commit comments

Comments
 (0)