Skip to content

Commit 967ea58

Browse files
committed
fix: build mapping on toolUse
1 parent 70a4fa9 commit 967ea58

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/strands/models/gemini.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ def _format_request_content_part(self, content: ContentBlock) -> genai.types.Par
146146
)
147147

148148
if "toolUse" in content:
149+
# Store the mapping from toolUseId to name for later use in toolResult formatting.
150+
# This mapping is built as we format the request, ensuring that when we encounter
151+
# toolResult blocks (which come after toolUse blocks in the message history),
152+
# we can look up the function name.
153+
self._tool_use_id_to_name[content["toolUse"]["toolUseId"]] = content["toolUse"]["name"]
154+
149155
return genai.types.Part(
150156
function_call=genai.types.FunctionCall(
151157
args=content["toolUse"]["input"],
@@ -269,15 +275,9 @@ def _format_chunk(self, event: dict[str, Any]) -> StreamEvent:
269275
case "content_start":
270276
match event["data_type"]:
271277
case "tool":
272-
# Generate a unique toolUseId that matches tooluse_<base64> pattern
273278
function_call = event["data"].function_call
274-
tool_use_id = f"tooluse_{secrets.token_urlsafe(16)}"
275-
276-
# Store mapping from toolUseId to function name for later lookup in
277-
# _format_request_content_part. This is done because Gemini requires the
278-
# function name to be set in the equivalent FunctionResponse type, but we
279-
# also want a unique identifier for the tool use.
280-
self._tool_use_id_to_name[tool_use_id] = function_call.name
279+
# Use Gemini's provided ID or generate one if missing
280+
tool_use_id = function_call.id or f"tooluse_{secrets.token_urlsafe(16)}"
281281

282282
return {
283283
"contentBlockStart": {

0 commit comments

Comments
 (0)