@@ -2162,6 +2162,54 @@ def shell(command: str):
21622162 assert agent .messages [- 1 ] == {"content" : [{"text" : "I invoked a tool!" }], "role" : "assistant" }
21632163
21642164
2165+ def test_agent_does_not_include_tools_in_trace_by_default (tool_decorated ):
2166+ """Verify that by default, the agent does not add tool specs to the trace."""
2167+ with unittest .mock .patch ("strands.agent.agent.get_tracer" ) as mock_get_tracer :
2168+ mock_tracer_instance = unittest .mock .MagicMock ()
2169+ mock_span = unittest .mock .MagicMock ()
2170+ mock_tracer_instance .start_agent_span .return_value = mock_span
2171+ mock_tracer_instance .include_tool_definitions = False # Default behavior
2172+ mock_get_tracer .return_value = mock_tracer_instance
2173+
2174+ mock_model = MockedModelProvider ([{"role" : "assistant" , "content" : [{"text" : "hello!" }]}])
2175+
2176+ agent = Agent (tools = [tool_decorated ], model = mock_model )
2177+ agent ("test prompt" )
2178+
2179+ # Check that set_attribute was not called for our specific key
2180+ called_attributes = [call .args [0 ] for call in mock_span .set_attribute .call_args_list ]
2181+ assert "gen_ai.tool.definitions" not in called_attributes
2182+
2183+
2184+ def test_agent_includes_tools_in_trace_when_enabled (tool_decorated ):
2185+ """Verify that the agent adds tool specs to the trace when the flag is enabled."""
2186+ with unittest .mock .patch ("strands.agent.agent.get_tracer" ) as mock_get_tracer :
2187+ mock_tracer_instance = unittest .mock .MagicMock ()
2188+ mock_span = unittest .mock .MagicMock ()
2189+ mock_tracer_instance .start_agent_span .return_value = mock_span
2190+ mock_tracer_instance .include_tool_definitions = True # Enable the feature
2191+ mock_get_tracer .return_value = mock_tracer_instance
2192+
2193+ mock_model = MockedModelProvider ([{"role" : "assistant" , "content" : [{"text" : "hello!" }]}])
2194+
2195+ agent = Agent (tools = [tool_decorated ], model = mock_model )
2196+ agent ("test prompt" )
2197+
2198+ # Verify the correct data is serialized and set as an attribute
2199+ tool_spec = tool_decorated .tool_spec
2200+ expected_tool_details = [
2201+ {
2202+ "name" : tool_spec .get ("name" ),
2203+ "description" : tool_spec .get ("description" ),
2204+ "inputSchema" : tool_spec .get ("inputSchema" ),
2205+ "outputSchema" : tool_spec .get ("outputSchema" ),
2206+ }
2207+ ]
2208+ expected_json = serialize (expected_tool_details )
2209+
2210+ mock_span .set_attribute .assert_called_with ("gen_ai.tool.definitions" , expected_json )
2211+
2212+
21652213@pytest .mark .parametrize (
21662214 "content, expected" ,
21672215 [
0 commit comments