fix(tool): allow null for optional nested object fields#1170
Merged
LearningGp merged 4 commits intoApr 24, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes tool input validation so that optional object fields explicitly set to null are treated the same as omitted fields, preventing brittle failures during JSON Schema validation (Fixes #1163).
Changes:
- Normalize tool-call JSON inputs by pruning
nullobject properties before running JSON Schema validation. - Add regression tests covering generated nested bean schemas accepting missing vs explicit-
nulloptional fields. - Add protection tests confirming nested bean parameter conversion already handles missing vs explicit-
nulloptional fields.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
agentscope-core/src/main/java/io/agentscope/core/tool/ToolValidator.java |
Adds pre-validation normalization that prunes null-valued object properties before schema validation. |
agentscope-core/src/test/java/io/agentscope/core/tool/ToolValidatorTest.java |
Adds regression tests for generated bean schemas around missing vs explicit null optional nested fields. |
agentscope-core/src/test/java/io/agentscope/core/tool/ToolMethodInvokerTest.java |
Adds conversion tests showing bean parameter conversion behaves correctly for missing vs explicit null optional fields. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
LearningGp
approved these changes
Apr 24, 2026
liangxingguang
pushed a commit
to liangxingguang/agentscope-java
that referenced
this pull request
May 21, 2026
…i#1170) ## AgentScope-Java Version 1.0.12-SNAPSHOT ## Description Fixes agentscope-ai#1163 ### Background When a tool parameter contains a nested object, AgentScope-Java currently treats these two cases differently for optional fields: 1. The optional field is omitted 2. The optional field is explicitly set to `null` Case 1 passes validation, but case 2 fails during JSON Schema validation with an error like: `null found, string expected` This makes tool calling brittle in practice, because LLMs often emit `null` for optional fields instead of omitting them entirely. ### Purpose Make optional nested object fields behave consistently: - missing optional field: valid - optional field explicitly set to `null`: also valid Required field validation should continue to work as before. ### Changes made #### 1. Fix tool input validation for explicit `null` optional fields Updated `ToolValidator.validateInput(...)` to normalize tool input before JSON Schema validation. It now recursively removes `null`-valued object properties before validating the payload. This means: - optional fields with `null` are treated the same as omitted fields - required fields with `null` still fail validation, because the field is removed and then checked by `required` Modified file: - `agentscope-core/src/main/java/io/agentscope/core/tool/ToolValidator.java` #### 2. Add regression tests for generated nested bean schema Added tests to verify: - missing optional nested bean field is accepted - explicit `null` optional nested bean field is accepted Modified file: - `agentscope-core/src/test/java/io/agentscope/core/tool/ToolValidatorTest.java` #### 3. Add protection tests for object parameter conversion Added tests to confirm that nested bean parameter conversion itself already works correctly for: - missing optional field - explicit `null` optional field This verifies the bug is in validation behavior rather than conversion behavior. Modified file: - `agentscope-core/src/test/java/io/agentscope/core/tool/ToolMethodInvokerTest.java` ### How to test Run: ```bash mvn -pl agentscope-core -Dtest=ToolValidatorTest,ToolMethodInvokerTest test
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
AgentScope-Java Version
1.0.12-SNAPSHOT
Description
Fixes #1163
Background
When a tool parameter contains a nested object, AgentScope-Java currently treats these two cases differently for optional fields:
nullCase 1 passes validation, but case 2 fails during JSON Schema validation with an error like:
null found, string expectedThis makes tool calling brittle in practice, because LLMs often emit
nullfor optional fields instead of omitting them entirely.Purpose
Make optional nested object fields behave consistently:
null: also validRequired field validation should continue to work as before.
Changes made
1. Fix tool input validation for explicit
nulloptional fieldsUpdated
ToolValidator.validateInput(...)to normalize tool input before JSON Schema validation.It now recursively removes
null-valued object properties before validating the payload. This means:nullare treated the same as omitted fieldsnullstill fail validation, because the field is removed and then checked byrequiredModified file:
agentscope-core/src/main/java/io/agentscope/core/tool/ToolValidator.java2. Add regression tests for generated nested bean schema
Added tests to verify:
nulloptional nested bean field is acceptedModified file:
agentscope-core/src/test/java/io/agentscope/core/tool/ToolValidatorTest.java3. Add protection tests for object parameter conversion
Added tests to confirm that nested bean parameter conversion itself already works correctly for:
nulloptional fieldThis verifies the bug is in validation behavior rather than conversion behavior.
Modified file:
agentscope-core/src/test/java/io/agentscope/core/tool/ToolMethodInvokerTest.javaHow to test
Run:
mvn -pl agentscope-core -Dtest=ToolValidatorTest,ToolMethodInvokerTest test