Skip to content

Fix numeric array inputs from Gemini failing parameter conversion - #85

Draft
borisr-code-ibm wants to merge 1 commit into
android:mainfrom
borisr-code-ibm:bugfix/numeric-array-inputs
Draft

borisr-code-ibm wants to merge 1 commit into
android:mainfrom
borisr-code-ibm:bugfix/numeric-array-inputs

Conversation

@borisr-code-ibm

Copy link
Copy Markdown

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, strings String, booleans Boolean.
ConvertInputToAppFunctionDataUseCase.setArrayValue then hard-cast every array element to the
declared item type, so:

  • a string-array parameter (e.g. message or contact IDs) crashed with
    ClassCastException: Long cannot be cast to String whenever Gemini sent numbers such as
    [101, 102];
  • a boolean array failed on "true" / "false" strings;
  • an unconvertible element surfaced as an opaque 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 Number on main, but the remaining hard casts reported in #62
were still there: setStringList (it as String) and boolean arrays. Bad elements also still
produced errors with no context.

Solution

Array elements are now converted per item type through a single convertElements helper:

  • string arrays: any non-null element via toString() (matches how scalar String params already work);
  • int / long / double / float arrays: Number or a parseable String;
  • boolean arrays: Boolean or "true"/"false" (case-insensitive);
  • a null or unconvertible element, or a non-list value for an array parameter, fails with an
    IllegalArgumentException naming 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:

Input Before After
{"messageIds": [101, 102, 3.5]} for a string array ClassCastException: Long cannot be cast to String ["101", "102", "3.5"]
{"flags": ["true", false, "FALSE"]} for a boolean array ClassCastException: String cannot be cast to Boolean [true, false, false]
{"ints": [1, "2", 3.0], "longs": [9000000000, "4"]} [1, 2, 3], [9000000000, 4] unchanged
{"ints": [1, "abc"]} NumberFormatException: For input string: "abc" IllegalArgumentException: Parameter 'ints' expects an array of int, but element 1 is 'abc'

Testing

  • Added 7 tests to ConvertInputToAppFunctionDataUseCaseTest: numbers in a string array, mixed
    numeric arrays (including the Double values JSON parsing produces), boolean strings, and
    failures 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

agent app, standard flavor (JVM / Robolectric). The retail flavor uses the same code.

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.
@google-cla

google-cla Bot commented Nov 13, 2026

Copy link
Copy Markdown

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.

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.

[Bug]: Testing agent fails on numeric array parameters from the LLM (ClassCastException: Double cannot be cast to String)

1 participant