Repository navigation
Fix numeric array inputs from Gemini failing parameter conversion - #85
Draft
borisr-code-ibm wants to merge 1 commit into
Draft
borisr-code-ibm wants to merge 1 commit into
borisr-code-ibm wants to merge 1 commit into
Conversation
Array elements were hard-cast to the declared item type, so a string-array parameter crashed with ClassCastException when Gemini passed numbers (e.g. IDs [1, 2]), and boolean arrays failed on "true"/"false" strings. Convert elements per item type and report unconvertible elements with an error naming the parameter and element.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
What I have done and why
Fixes #62
Problem
When Gemini calls an AppFunction tool, its JSON arguments are parsed by
JsonElement.toPrimitive():JSON numbers become
Long/Double, stringsString, booleansBoolean.ConvertInputToAppFunctionDataUseCase.setArrayValuethen hard-cast every array element to thedeclared item type, so:
ClassCastException: Long cannot be cast to Stringwhenever Gemini sent numbers such as[101, 102];"true"/"false"strings;NumberFormatException: For input string: "abc"with no hint of which parameter was wrong.
The agent aborted the turn with "Failed to convert arguments for …".
The numeric branches already accept
Numberonmain, but the remaining hard casts reported in #62were still there:
setStringList(it as String) and boolean arrays. Bad elements also stillproduced errors with no context.
Solution
Array elements are now converted per item type through a single
convertElementshelper:toString()(matches how scalar String params already work);Numberor a parseableString;Booleanor"true"/"false"(case-insensitive);IllegalArgumentExceptionnaming the parameter, element index and value, e.g.Parameter 'ints' expects an array of int, but element 1 is 'abc'.The Gemini tool schema and the LLM payload are unchanged.
Evidence
Gemini-shaped JSON arguments run through
toArgumentsMap()and the use case, before and after the fix:{"messageIds": [101, 102, 3.5]}for a string arrayClassCastException: Long cannot be cast to String["101", "102", "3.5"]{"flags": ["true", false, "FALSE"]}for a boolean arrayClassCastException: String cannot be cast to Boolean[true, false, false]{"ints": [1, "2", 3.0], "longs": [9000000000, "4"]}[1, 2, 3],[9000000000, 4]{"ints": [1, "abc"]}NumberFormatException: For input string: "abc"IllegalArgumentException: Parameter 'ints' expects an array of int, but element 1 is 'abc'Testing
ConvertInputToAppFunctionDataUseCaseTest: numbers in a string array, mixednumeric arrays (including the
Doublevalues JSON parsing produces), boolean strings, andfailures for a bad element, a null element and a non-list value. Six of these fail on
main../gradlew spotlessCheck :app:testStandardDebugUnitTest -PGEMINI_API_KEY=dummy_key: all pass.Targets tested
agentapp,standardflavor (JVM / Robolectric). Theretailflavor uses the same code.