Use Gemini function names for tool responses

This commit is contained in:
adavyas 2026-03-14 17:01:20 -07:00
parent d2ce88737d
commit 6dc080aeaf
2 changed files with 12 additions and 2 deletions

View File

@ -130,6 +130,7 @@ def _build_gemini_contents_from_messages(
"""Convert generic chat messages into Gemini contents and system instruction.""" """Convert generic chat messages into Gemini contents and system instruction."""
system_instruction_parts: list[str] = [] system_instruction_parts: list[str] = []
gemini_contents: list[dict[str, Any]] = [] gemini_contents: list[dict[str, Any]] = []
tool_use_names: dict[str, str] = {}
for msg in messages: for msg in messages:
role = msg.get("role", "user") role = msg.get("role", "user")
@ -168,6 +169,9 @@ def _build_gemini_contents_from_messages(
continue continue
if block_type == "tool_use" and isinstance(block.get("name"), str): if block_type == "tool_use" and isinstance(block.get("name"), str):
tool_use_id = block.get("id")
if isinstance(tool_use_id, str):
tool_use_names[tool_use_id] = block["name"]
tool_args = block.get("input") tool_args = block.get("input")
parts.append( parts.append(
{ {
@ -183,10 +187,16 @@ def _build_gemini_contents_from_messages(
tool_result = block.get("content") tool_result = block.get("content")
if not isinstance(tool_result, str): if not isinstance(tool_result, str):
tool_result = json.dumps(tool_result) tool_result = json.dumps(tool_result)
tool_use_id = block.get("tool_use_id")
function_name = (
tool_use_names.get(tool_use_id)
if isinstance(tool_use_id, str)
else None
)
parts.append( parts.append(
{ {
"function_response": { "function_response": {
"name": str(block.get("tool_use_id", "tool_result")), "name": function_name or "tool_result",
"response": { "response": {
"result": tool_result, "result": tool_result,
"is_error": bool(block.get("is_error", False)), "is_error": bool(block.get("is_error", False)),

View File

@ -982,7 +982,7 @@ class TestGoogleClient:
"parts": [ "parts": [
{ {
"function_response": { "function_response": {
"name": "toolu_1", "name": "search_memory",
"response": { "response": {
"result": "Alice likes tea", "result": "Alice likes tea",
"is_error": False, "is_error": False,