Skip to content

Commit 7b43d61

Browse files
Fix unsupported operation exception in execute tool API (#4325)
Signed-off-by: Nathalie Jonathan <nathhjo@amazon.com>
1 parent 305cb8e commit 7b43d61

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/tool/MLToolExecutor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ public void execute(Input input, ActionListener<Output> listener) {
8383
}
8484

8585
try {
86-
Tool tool = toolFactory.create(new HashMap<>(parameters));
87-
if (!tool.validate(parameters)) {
86+
Map<String, String> mutableParams = new HashMap<>(parameters);
87+
Tool tool = toolFactory.create(mutableParams);
88+
if (!tool.validate(mutableParams)) {
8889
listener.onFailure(new IllegalArgumentException("Invalid parameters for tool: " + toolName));
8990
return;
9091
}
9192

92-
tool.run(parameters, ActionListener.wrap(result -> {
93+
tool.run(mutableParams, ActionListener.wrap(result -> {
9394
List<ModelTensor> modelTensors = new ArrayList<>();
9495
processOutput(result, modelTensors);
9596
ModelTensors tensors = ModelTensors.builder().mlModelTensors(modelTensors).build();

ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/tool/MLToolExecutorTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.mockito.ArgumentMatchers.any;
99
import static org.mockito.Mockito.when;
1010

11+
import java.util.Collections;
1112
import java.util.HashMap;
1213
import java.util.Map;
1314
import java.util.Set;
@@ -41,7 +42,6 @@ public class MLToolExecutorTest {
4142
private Client client;
4243
@Mock
4344
private SdkClient sdkClient;
44-
private Settings settings;
4545
@Mock
4646
private ClusterService clusterService;
4747
@Mock
@@ -176,4 +176,30 @@ public void test_EmptyInputData() {
176176
mlToolExecutor.execute(toolMLInput, actionListener);
177177
});
178178
}
179+
180+
@Test
181+
public void test_ImmutableEmptyParametersMap() {
182+
Map<String, String> immutableEmptyMap = Collections.emptyMap();
183+
184+
when(toolMLInput.getToolName()).thenReturn("TestTool");
185+
when(toolMLInput.getInputDataset()).thenReturn(inputDataSet);
186+
when(inputDataSet.getParameters()).thenReturn(immutableEmptyMap);
187+
when(toolFactory.create(any())).thenReturn(tool);
188+
when(tool.validate(any())).thenReturn(true);
189+
190+
Mockito.doAnswer(invocation -> {
191+
Map<String, String> params = invocation.getArgument(0);
192+
// Tool tries to modify parameters, should not throw exception
193+
params.put("test_key", "test_value");
194+
ActionListener<Object> listener = invocation.getArgument(1);
195+
listener.onResponse("test result");
196+
return null;
197+
}).when(tool).run(any(), any());
198+
199+
mlToolExecutor.execute(toolMLInput, actionListener);
200+
201+
Mockito.verify(actionListener).onResponse(outputCaptor.capture());
202+
Output output = outputCaptor.getValue();
203+
Assert.assertTrue(output instanceof ModelTensorOutput);
204+
}
179205
}

0 commit comments

Comments
 (0)