I run BQ jobs from a python code that first creates an empty table in BQ for the results with specific labels & description. later in the BQ SQL I insert the results into that empty table. The only problem is that I can't use ALTER to add columns to a table with no schema. I can't add the schema before because the SQL query is dynamically created by the Python code. The only way I found to solve this was to create the table with a column called 'x' and then remove it at the end of the SQL query.
Here is an idea of what the code looks like:
CREATE TEMP FUNCTION
... very_complicated_function ...;
CREATE TEMP TABLE features AS
... very_clever_code ...;
ALTER TABLE `table.created.by_python`
ADD COLUMN IF NOT EXISTS key INT64,
ADD COLUMN IF NOT EXISTS feature1 INT64;
ALTER TABLE `table.created.by_python` DROP COLUMN x;
INSERT INTO `table.created.by_python`
SELECT * except(nearest_centroids_distance)
from
ML.PREDICT(MODEL `brilliant.genius.amazing`,
(SELECT * FROM features)) M
The best possibility is just to insert the data into the empty table and let it create the schema itself if it doesn't exist.