add _is_bedrock_tool_block

This commit is contained in:
Ishaan Jaffer 2026-01-31 11:25:10 -08:00
parent 8b575f4656
commit e6c1a656f4

View file

@ -4395,6 +4395,32 @@ def add_cache_point_tool_block(tool: dict) -> Optional[BedrockToolBlock]:
return None
def _is_bedrock_tool_block(tool: dict) -> bool:
"""
Check if a tool is already a BedrockToolBlock.
BedrockToolBlock has one of: systemTool, toolSpec, or cachePoint.
This is used to detect tools that are already in Bedrock format
(e.g., systemTool for Nova grounding) vs OpenAI-style function tools
that need transformation.
Args:
tool: The tool dict to check
Returns:
True if the tool is already a BedrockToolBlock, False otherwise
Examples:
>>> _is_bedrock_tool_block({"systemTool": {"name": "nova_grounding"}})
True
>>> _is_bedrock_tool_block({"type": "function", "function": {...}})
False
"""
return isinstance(tool, dict) and (
"systemTool" in tool or "toolSpec" in tool or "cachePoint" in tool
)
def _bedrock_tools_pt(tools: List) -> List[BedrockToolBlock]:
"""
OpenAI tools looks like:
@ -4448,7 +4474,13 @@ def _bedrock_tools_pt(tools: List) -> List[BedrockToolBlock]:
tool_block_list: List[BedrockToolBlock] = []
for tool in tools:
# Handle regular function tools
# Check if tool is already a BedrockToolBlock (e.g., systemTool for Nova grounding)
if _is_bedrock_tool_block(tool):
# Already a BedrockToolBlock, pass it through
tool_block_list.append(tool) # type: ignore
continue
# Handle regular OpenAI-style function tools
parameters = tool.get("function", {}).get(
"parameters", {"type": "object", "properties": {}}
)