Skip to content

Conversation

@JGoP-L
Copy link
Contributor

@JGoP-L JGoP-L commented Jan 15, 2026

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

  • 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

Copy link
Contributor

Copilot AI left a 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.

Comment on lines 70 to 75
// 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);
}
Copy link

Copilot AI Jan 15, 2026

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.

Copilot uses AI. Check for mistakes.
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 "{}"
Copy link

Copilot AI Jan 15, 2026

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.

Suggested change
// This assertion will fail with the bug - getContent() returns null instead of "{}"
// Verify that getContent() returns "{}" (not null) for no-parameter tools

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...pe/core/formatter/ollama/OllamaResponseParser.java 75.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a 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.

Comment on lines 79 to 80
callId, fn.getName(), input, argumentsJson, null // metadata
));
Copy link

Copilot AI Jan 16, 2026

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.

Suggested change
callId, fn.getName(), input, argumentsJson, null // metadata
));
callId, fn.getName(), input, argumentsJson, // raw content
null));

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant