-
Notifications
You must be signed in to change notification settings - Fork 214
fix(ollama): handle no-parameter tool calls with empty arguments #572
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
base: main
Are you sure you want to change the base?
fix(ollama): handle no-parameter tool calls with empty arguments #572
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | ||
| } |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 "{}" |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | ||
| )); |
Copilot
AI
Jan 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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-
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.