-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix tool call with empty arguments in streaming mode #4027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huidongyin
wants to merge
5
commits into
spring-projects:main
Choose a base branch
from
huidongyin:fix/stream-tool-call-empty-arguments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4027b90
Fix incorrect example code in documentation
huidongyin eaca392
Merge branch 'spring-projects:main' into main
huidongyin 91aadd3
Fix tool call with empty arguments in streaming mode
huidongyin 68f1396
Apply Spring Java format to fix MCP module code style violations
huidongyin 1ad800a
Fix wildcard import in DefaultToolCallingManager
huidongyin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
...-model/src/test/java/org/springframework/ai/model/tool/DefaultToolCallingManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.model.tool; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.ai.chat.messages.AssistantMessage; | ||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.ai.chat.model.ChatResponse; | ||
import org.springframework.ai.chat.model.Generation; | ||
import org.springframework.ai.chat.prompt.Prompt; | ||
import org.springframework.ai.tool.ToolCallback; | ||
import org.springframework.ai.tool.definition.DefaultToolDefinition; | ||
import org.springframework.ai.tool.definition.ToolDefinition; | ||
import org.springframework.ai.tool.metadata.ToolMetadata; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
|
||
/** | ||
* Tests for {@link DefaultToolCallingManager} with empty/null arguments handling. | ||
* | ||
* @author Spring AI Team | ||
*/ | ||
class DefaultToolCallingManagerTest { | ||
|
||
@Test | ||
void shouldHandleNullArgumentsInStreamMode() { | ||
// Create a mock tool callback | ||
ToolCallback mockToolCallback = new ToolCallback() { | ||
@Override | ||
public ToolDefinition getToolDefinition() { | ||
return DefaultToolDefinition.builder() | ||
.name("testTool") | ||
.description("A test tool") | ||
.inputSchema("{}") | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ToolMetadata getToolMetadata() { | ||
return ToolMetadata.builder().build(); | ||
} | ||
|
||
@Override | ||
public String call(String toolInput) { | ||
// Verify the input is not null or empty | ||
assertThat(toolInput).isNotNull(); | ||
assertThat(toolInput).isNotEmpty(); | ||
return "{\"result\": \"success\"}"; | ||
} | ||
}; | ||
|
||
// Create DefaultToolCallingManager with tool callback | ||
DefaultToolCallingManager manager = DefaultToolCallingManager.builder() | ||
.observationRegistry(ObservationRegistry.NOOP) | ||
.build(); | ||
|
||
// Create a ToolCall with empty parameters | ||
AssistantMessage.ToolCall toolCall = new AssistantMessage.ToolCall("1", "function", "testTool", null); | ||
|
||
// Create a ChatResponse | ||
AssistantMessage assistantMessage = new AssistantMessage("", Map.of(), List.of(toolCall)); | ||
Generation generation = new Generation(assistantMessage); | ||
ChatResponse chatResponse = new ChatResponse(List.of(generation)); | ||
|
||
// Create a Prompt with tool callbacks | ||
Prompt prompt = new Prompt(List.of(new UserMessage("test"))); | ||
|
||
// Mock the tool callbacks resolution by creating a custom ToolCallbackResolver | ||
DefaultToolCallingManager managerWithCallback = DefaultToolCallingManager.builder() | ||
.observationRegistry(ObservationRegistry.NOOP) | ||
.toolCallbackResolver(toolName -> { | ||
if ("testTool".equals(toolName)) { | ||
return mockToolCallback; | ||
} | ||
return null; | ||
}) | ||
.build(); | ||
|
||
// Verify that no exception is thrown | ||
assertThatNoException().isThrownBy(() -> { | ||
managerWithCallback.executeToolCalls(prompt, chatResponse); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldHandleEmptyArgumentsInStreamMode() { | ||
// Create a mock tool callback | ||
ToolCallback mockToolCallback = new ToolCallback() { | ||
@Override | ||
public ToolDefinition getToolDefinition() { | ||
return DefaultToolDefinition.builder() | ||
.name("testTool") | ||
.description("A test tool") | ||
.inputSchema("{}") | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ToolMetadata getToolMetadata() { | ||
return ToolMetadata.builder().build(); | ||
} | ||
|
||
@Override | ||
public String call(String toolInput) { | ||
// Verify the input is not null or empty | ||
assertThat(toolInput).isNotNull(); | ||
assertThat(toolInput).isNotEmpty(); | ||
return "{\"result\": \"success\"}"; | ||
} | ||
}; | ||
|
||
// Create DefaultToolCallingManager with tool callback | ||
DefaultToolCallingManager manager = DefaultToolCallingManager.builder() | ||
.observationRegistry(ObservationRegistry.NOOP) | ||
.build(); | ||
|
||
// Create a ToolCall with empty parameters | ||
AssistantMessage.ToolCall toolCall = new AssistantMessage.ToolCall("1", "function", "testTool", ""); | ||
|
||
// Create a ChatResponse | ||
AssistantMessage assistantMessage = new AssistantMessage("", Map.of(), List.of(toolCall)); | ||
Generation generation = new Generation(assistantMessage); | ||
ChatResponse chatResponse = new ChatResponse(List.of(generation)); | ||
|
||
// Create a Prompt with tool callbacks | ||
Prompt prompt = new Prompt(List.of(new UserMessage("test"))); | ||
|
||
// Mock the tool callbacks resolution by creating a custom ToolCallbackResolver | ||
DefaultToolCallingManager managerWithCallback = DefaultToolCallingManager.builder() | ||
.observationRegistry(ObservationRegistry.NOOP) | ||
.toolCallbackResolver(toolName -> { | ||
if ("testTool".equals(toolName)) { | ||
return mockToolCallback; | ||
} | ||
return null; | ||
}) | ||
.build(); | ||
|
||
// Verify that no exception is thrown | ||
assertThatNoException().isThrownBy(() -> { | ||
managerWithCallback.executeToolCalls(prompt, chatResponse); | ||
}); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.