def _make_tool()

in google/generativeai/types/content_types.py [0:0]


def _make_tool(tool: ToolType) -> Tool:
    if isinstance(tool, Tool):
        return tool
    elif isinstance(tool, protos.Tool):
        if "code_execution" in tool:
            code_execution = tool.code_execution
        else:
            code_execution = None

        if "google_search_retrieval" in tool:
            google_search_retrieval = tool.google_search_retrieval
        else:
            google_search_retrieval = None

        return Tool(
            function_declarations=tool.function_declarations,
            google_search_retrieval=google_search_retrieval,
            code_execution=code_execution,
        )
    elif isinstance(tool, dict):
        if (
            "function_declarations" in tool
            or "google_search_retrieval" in tool
            or "code_execution" in tool
        ):
            return Tool(**tool)
        else:
            fd = tool
            return Tool(function_declarations=[protos.FunctionDeclaration(**fd)])
    elif isinstance(tool, str):
        if tool.lower() == "code_execution":
            return Tool(code_execution=protos.CodeExecution())
        # Check to see if one of the mode enums matches
        elif tool.lower() == "google_search_retrieval":
            return Tool(google_search_retrieval=protos.GoogleSearchRetrieval())
        else:
            raise ValueError(
                "The only string that can be passed as a tool is 'code_execution', or one of the specified values for the `mode` parameter for google_search_retrieval."
            )
    elif isinstance(tool, protos.CodeExecution):
        return Tool(code_execution=tool)
    elif isinstance(tool, protos.GoogleSearchRetrieval):
        return Tool(google_search_retrieval=tool)
    elif isinstance(tool, Iterable):
        return Tool(function_declarations=tool)
    else:
        try:
            return Tool(function_declarations=[tool])
        except Exception as e:
            raise TypeError(
                "Invalid input type. Expected an instance of `genai.ToolType`.\n"
                f"However, received an object of type: {type(tool)}.\n"
                f"Object Value: {tool}"
            ) from e