fix(ollama): handle no-parameter tool calls with empty arguments#572
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in OllamaChatModel where tool calls with no parameters (e.g., getTime()) were failing validation. The issue was that OllamaResponseParser was passing null as the content parameter when creating ToolUseBlock for empty arguments, causing validation errors in ToolExecutor.
Changes:
- Fixed OllamaResponseParser to convert empty input maps to JSON string "{}" for the content parameter
- Added test case for no-parameter tool calls in OllamaResponseParserTest
- Added test case for empty input validation in ToolValidatorTest
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| OllamaResponseParser.java | Converts empty input maps to JSON string "{}" for content parameter to fix validation |
| OllamaResponseParserTest.java | Adds test coverage for no-parameter tool calls |
| ToolValidatorTest.java | Adds test coverage for empty object validation with empty schema |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Convert input to JSON string for validation in ToolExecutor | ||
| // For tools with no parameters, input will be an empty map {} | ||
| String argumentsJson = "{}"; | ||
| if (input != null && !input.isEmpty()) { | ||
| argumentsJson = JsonUtils.getJsonCodec().toJson(input); | ||
| } |
There was a problem hiding this comment.
The fix correctly addresses the issue, but there's an inconsistency with how other formatters handle this case. OpenAI's approach (line 222-234 in OpenAIResponseParser) handles null arguments by setting them to empty string "", then parsing to create an empty map. Anthropic (line 72-75 in AnthropicResponseParser) uses an empty string "" when _input is null. For consistency across formatters and to handle potential null inputs more robustly, consider adding a null check: if (input == null || input.isEmpty()) { argumentsJson = "{}"; } instead of relying on the assumption that input is never null.
| assertEquals("getTime", toolBlock.getName()); | ||
| assertTrue(toolBlock.getInput().isEmpty(), "Input should be empty map"); | ||
|
|
||
| // This assertion will fail with the bug - getContent() returns null instead of "{}" |
There was a problem hiding this comment.
This comment describes a bug scenario that should no longer occur after the fix. The comment should be updated to reflect the current state: "Verify that getContent() returns '{}' for no-parameter tools" or similar, as the bug is now fixed.
| // This assertion will fail with the bug - getContent() returns null instead of "{}" | |
| // Verify that getContent() returns "{}" (not null) for no-parameter tools |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| callId, fn.getName(), input, argumentsJson, null // metadata | ||
| )); |
There was a problem hiding this comment.
The inline comment // metadata is misleading. Based on the ToolUseBlock constructor signature (5 parameters: id, name, input, content, metadata), the comment should be // raw content for the 4th parameter position (argumentsJson), not for the 5th parameter (null). The null value here is the metadata parameter.
| callId, fn.getName(), input, argumentsJson, null // metadata | |
| )); | |
| callId, fn.getName(), input, argumentsJson, // raw content | |
| null)); |
…ls-with-empty-arguments-' into fix/handle-no-parameter-tool-calls-with-empty-arguments-
…ntscope-ai#572) AgentScope-Java Version 1.0.8-SNAPSHOT Description Background When using OllamaChatModel to call tools with no parameters (like getTime()), the tool execution fails with a validation error, while the same tool works correctly with OpenAIChatModel. Reported in issue agentscope-ai#569. Problem The root cause is in OllamaResponseParser. When Ollama returns a tool call with no parameters, the arguments field is an empty map {}. The parser was passing null as the content parameter when creating ToolUseBlock: // Before fix contentBlocks.add( new ToolUseBlock(callId, fn.getName(), input, null) // content = null ); However, ToolValidator.validateInput(toolCall.getContent(), ...) expects a JSON string for schema validation. When content is null, validation fails with: Parameter validation failed for tool 'getTime': Schema validation error: argument "content" is null Solution Convert the input map to a JSON string for the content parameter. For tools with no parameters, use the empty JSON object "{}": // After fix String argumentsJson = "{}"; // default for no parameters if (input != null && !input.isEmpty()) { argumentsJson = JsonUtils.getJsonCodec().toJson(input); } contentBlocks.add( new ToolUseBlock(callId, fn.getName(), input, argumentsJson, null) ); This aligns with how other formatters handle tool arguments, ensuring ToolValidator receives a valid JSON string. Changes Made - Modified: OllamaResponseParser.java - Convert input map to JSON string for content parameter - Added: Test case testParseToolCallWithNoParameters in OllamaResponseParserTest.java to prevent regression - Added: Test case testEmptyInputWithEmptySchema in ToolValidatorTest.java to verify empty object validation How to Test Run the Ollama formatter tests: mvn test -Dtest=OllamaResponseParserTest Or test with a real Ollama instance: @tool(description = "获取当前时间") public String getTime() { return LocalDateTime.now().toString(); } Using OllamaChatModel with a tool like getTime (no parameters) should now work correctly. Checklist Please check the following items before code is ready to be reviewed. - Code has been formatted with mvn spotless:apply - All tests are passing (mvn test) - Javadoc comments are complete and follow project conventions - Related documentation has been updated (e.g. links, examples, etc.) - Code is ready for review
AgentScope-Java Version
1.0.8-SNAPSHOT
Description
Background
When using OllamaChatModel to call tools with no parameters (like getTime()), the tool execution fails with a validation error, while the same tool works correctly with OpenAIChatModel.
Reported in issue #569.
Problem
The root cause is in OllamaResponseParser. When Ollama returns a tool call with no parameters, the arguments field is an empty map {}. The parser was passing null as the content parameter when creating ToolUseBlock:
// Before fix
contentBlocks.add(
new ToolUseBlock(callId, fn.getName(), input, null) // content = null
);
However, ToolValidator.validateInput(toolCall.getContent(), ...) expects a JSON string for schema validation. When content is null, validation fails with:
Parameter validation failed for tool 'getTime':
Schema validation error: argument "content" is null
Solution
Convert the input map to a JSON string for the content parameter. For tools with no parameters, use the empty JSON object "{}":
// After fix
String argumentsJson = "{}"; // default for no parameters
if (input != null && !input.isEmpty()) {
argumentsJson = JsonUtils.getJsonCodec().toJson(input);
}
contentBlocks.add(
new ToolUseBlock(callId, fn.getName(), input, argumentsJson, null)
);
This aligns with how other formatters handle tool arguments, ensuring ToolValidator receives a valid JSON string.
Changes Made
How to Test
Run the Ollama formatter tests:
mvn test -Dtest=OllamaResponseParserTest
Or test with a real Ollama instance:
@tool(description = "获取当前时间")
public String getTime() {
return LocalDateTime.now().toString();
}
Using OllamaChatModel with a tool like getTime (no parameters) should now work correctly.
Checklist
Please check the following items before code is ready to be reviewed.