@@ -3350,7 +3350,7 @@ def insert_rows_json(
33503350 self ,
33513351 table : Union [Table , TableReference , str ],
33523352 json_rows : Sequence [Dict ],
3353- row_ids : Union [Sequence [str ], AutoRowIDs , None ] = AutoRowIDs .GENERATE_UUID ,
3353+ row_ids : Union [Iterable [str ], AutoRowIDs , None ] = AutoRowIDs .GENERATE_UUID ,
33543354 skip_invalid_rows : bool = None ,
33553355 ignore_unknown_values : bool = None ,
33563356 template_suffix : str = None ,
@@ -3372,15 +3372,19 @@ def insert_rows_json(
33723372 json_rows (Sequence[Dict]):
33733373 Row data to be inserted. Keys must match the table schema fields
33743374 and values must be JSON-compatible representations.
3375- row_ids (Union[Sequence [str], AutoRowIDs, None]):
3375+ row_ids (Union[Iterable [str], AutoRowIDs, None]):
33763376 Unique IDs, one per row being inserted. An ID can also be
33773377 ``None``, indicating that an explicit insert ID should **not**
33783378 be used for that row. If the argument is omitted altogether,
33793379 unique IDs are created automatically.
33803380
3381+ .. versionchanged:: 2.21.0
3382+ Can also be an iterable, not just a sequence, or an
3383+ :class:`AutoRowIDs` enum member.
3384+
33813385 .. deprecated:: 2.21.0
33823386 Passing ``None`` to explicitly request autogenerating insert IDs is
3383- deprecated, use :attr:`. AutoRowIDs.GENERATE_UUID` instead.
3387+ deprecated, use :attr:`AutoRowIDs.GENERATE_UUID` instead.
33843388
33853389 skip_invalid_rows (Optional[bool]):
33863390 Insert all valid rows of a request, even if invalid rows exist.
@@ -3429,15 +3433,28 @@ def insert_rows_json(
34293433 )
34303434 row_ids = AutoRowIDs .GENERATE_UUID
34313435
3432- for index , row in enumerate (json_rows ):
3436+ if not isinstance (row_ids , AutoRowIDs ):
3437+ try :
3438+ row_ids_iter = iter (row_ids )
3439+ except TypeError :
3440+ msg = "rows_ids is neither an iterable nor an AutoRowIDs enum member"
3441+ raise TypeError (msg )
3442+
3443+ for i , row in enumerate (json_rows ):
34333444 info = {"json" : row }
34343445
34353446 if row_ids is AutoRowIDs .GENERATE_UUID :
34363447 info ["insertId" ] = str (uuid .uuid4 ())
34373448 elif row_ids is AutoRowIDs .DISABLED :
34383449 info ["insertId" ] = None
34393450 else :
3440- info ["insertId" ] = row_ids [index ]
3451+ try :
3452+ insert_id = next (row_ids_iter )
3453+ except StopIteration :
3454+ msg = f"row_ids did not generate enough IDs, error at index { i } "
3455+ raise ValueError (msg )
3456+ else :
3457+ info ["insertId" ] = insert_id
34413458
34423459 rows_info .append (info )
34433460
0 commit comments