fix(gemini): emit canonical camelCase tool parts and fix thought signature fallback

- In Google Gemini /v1beta and Proto3 JSON mapping, the canonical field names
  are lowerCamelCase functionCall and functionResponse.
- Emit canonical functionCall in convert_to_gemini_tool_call_invoke.
- Emit canonical functionResponse in convert_to_gemini_tool_call_result.
- Add functionCall and functionResponse to PartType TypedDict while preserving
  backwards-compatible input types for snake_case.
- Use stateful has_emitted_function_call tracking instead of len(_parts_list) == 0
  to ensure dummy thought signatures are attached to the first emitted tool call
  even when preceding message text is present, while strictly respecting Gemini's
  parallel function calling rule that only the first part in a parallel batch
  may carry a thought signature.
- Update test assertion in test_thought_signature_in_tool_call_id.py.
This commit is contained in:
Piyush Kumar 2026-10-31 07:47:49 +00:00
parent 57090e94a4
commit 6f8211c8ea
3 changed files with 11 additions and 8 deletions

View file

@ -1381,6 +1381,7 @@ def convert_to_gemini_tool_call_invoke(
)
needs_dummy_signature: Final = model is not None and VertexGeminiConfig.is_gemini_3_or_newer(model)
has_emitted_function_call: bool = False
if tool_calls is not None:
for tool in tool_calls:
@ -1390,17 +1391,17 @@ def convert_to_gemini_tool_call_invoke(
tool_call_id=(tool.get("id") if forward_function_call_id else None),
)
if gemini_function_call is not None:
part_dict: VertexPartType = {"function_call": gemini_function_call}
part_dict: VertexPartType = {"functionCall": gemini_function_call}
thought_signature = get_thought_signature_from_tool(dict(tool))
# Gemini signs only the first functionCall part of a parallel batch, so scope the
# placeholder fallback to that part instead of fabricating one per sibling call:
# https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/thinking/thought-signatures#parallel_function_calling_example
is_first_function_call = len(_parts_list) == 0
if not thought_signature and is_first_function_call and needs_dummy_signature:
if not thought_signature and not has_emitted_function_call and needs_dummy_signature:
thought_signature = _get_dummy_thought_signature()
if thought_signature:
part_dict["thoughtSignature"] = thought_signature
has_emitted_function_call = True
_parts_list.append(part_dict)
else: # don't silently drop params. Make it clear to user what's happening.
raise Exception(
@ -1409,7 +1410,7 @@ def convert_to_gemini_tool_call_invoke(
elif function_call is not None:
gemini_function_call = _gemini_tool_call_invoke_helper(function_call_params=function_call)
if gemini_function_call is not None:
part_dict_function: Final[VertexPartType] = {"function_call": gemini_function_call}
part_dict_function: Final[VertexPartType] = {"functionCall": gemini_function_call}
# Extract thought signature from function_call's provider_specific_fields
thought_signature = None
@ -1742,7 +1743,7 @@ def convert_to_gemini_tool_call_result(
if gemini_call_id:
_function_response["id"] = gemini_call_id
_part: Final[VertexPartType] = {"function_response": _function_response}
_part: Final[VertexPartType] = {"functionResponse": _function_response}
if media_parts:
if not supports_multimodal_function_responses:

View file

@ -62,8 +62,10 @@ class PartType(TypedDict, total=False):
text: str
inline_data: BlobType
file_data: FileDataType
function_call: FunctionCall
function_response: FunctionResponse
function_call: FunctionCall # Accepted on input
functionCall: FunctionCall # Canonical Proto3 JSON emitted to upstream
function_response: FunctionResponse # Accepted on input
functionResponse: FunctionResponse # Canonical Proto3 JSON emitted to upstream
thought: bool
thoughtSignature: str
media_resolution: Literal["low", "medium", "high"]

View file

@ -176,7 +176,7 @@ def test_convert_to_gemini_with_embedded_signature():
# Verify thought signature is extracted and sent to Gemini
assert len(gemini_parts) == 1
assert "function_call" in gemini_parts[0]
assert "functionCall" in gemini_parts[0] or "function_call" in gemini_parts[0]
assert "thoughtSignature" in gemini_parts[0]
assert gemini_parts[0]["thoughtSignature"] == test_signature