fix: strip id field from FunctionCall/FunctionResponse before sending to Gemini API#973
Open
YuqiGuo105 wants to merge 1 commit intogoogleapis:mainfrom
Open
Conversation
…ng to Gemini API The Gemini API does not accept an 'id' field in contents[].parts[].function_call or contents[].parts[].function_response, returning HTTP 400 'Unknown name id' when the field is present. The SDK was forwarding the 'id' field from the FunctionCall and FunctionResponse type objects directly into the outbound JSON payload via the converter methods in Models, Batches, Caches, LiveConverters, and TokensConverters. Fix: remove the 'id' copy-block from functionCallToMldev() and strip 'id' from the functionResponse node in partToMldev() across all five converter files. The 'id' field is intentionally retained in the type definitions so it can still be deserialized from inbound API responses. Fixes googleapis#694
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.
Problem
Fixes #694
The Gemini API rejects requests that include an
idfield insidecontents[].parts[].function_callorcontents[].parts[].function_response, returning:This surfaces in multi-turn / multi-agent workflows (e.g. Google ADK) where the SDK serializes
FunctionCallandFunctionResponseobjects that carry anidfield populated from a previous model response. The field is valid on the inbound (model → client) side but is not accepted on the outbound (client → API) side.Root Cause
The converter methods in the five converter files (
Models,Batches,Caches,LiveConverters,TokensConverters) were copying every field from the type object into the outbound JSON, includingid.Before —
functionCallToMldevin each converter copiedidunconditionally:Before —
partToMldevpassedfunctionResponsethrough as-is (includingid):Fix
After — the
idcopy-block is removed fromfunctionCallToMldevin all five files.After —
partToMldevconvertsfunctionResponseto aJsonNodeand explicitly removesidbefore forwarding:The
idfield is intentionally kept inFunctionCall.javaandFunctionResponse.javaso the SDK can still deserialize it from inbound model responses.Files Changed
Models.javaidfromfunctionCallToMldev; stripidinpartToMldevBatches.javaCaches.javapartToMldev)LiveConverters.javapartToMldev)TokensConverters.javaFunctionCallIdStrippingTest.javaTesting
Added
FunctionCallIdStrippingTest.javawith 20 JUnit 5 tests:idis stripped fromFunctionCallin all 5 convertersidis stripped fromFunctionResponsein all 5 convertersidfields (name,args,response) are preservedFunctionCall/FunctionResponsetype objects still deserializeidfrom JSON (regression guard)idpresent, name-only function callAll 20 tests pass.