Skip to content

Commit f5b9e13

Browse files
fix bugs in batched retrieval
1 parent 0585adc commit f5b9e13

1 file changed

Lines changed: 27 additions & 25 deletions

File tree

mp_api/client/core/client.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -805,27 +805,29 @@ def _query_resource(
805805
except RequestException as ex:
806806
raise MPRestError(str(ex))
807807

808-
def _submit_requests( # noqa
808+
def _submit_requests(
809809
self,
810-
url,
811-
criteria,
812-
use_document_model,
813-
chunk_size,
814-
num_chunks=None,
815-
timeout=None,
810+
url: str,
811+
criteria: dict[str, Any],
812+
use_document_model: bool,
813+
chunk_size: int,
814+
num_chunks: int | None = None,
815+
timeout: float | None = None,
816+
max_batch_size: int = 100,
816817
) -> dict:
817818
"""Handle submitting requests sequentially with pagination.
818819
819820
If criteria contains comma-separated parameters (except those that are naturally comma-separated),
820821
split them into multiple sequential requests and combine results.
821822
822823
Arguments:
823-
criteria: dictionary of criteria to filter down
824-
url: url used to make request
825-
use_document_model: if None, will defer to the self.use_document_model attribute
826-
num_chunks: Maximum number of chunks of data to yield. None will yield all possible.
827-
chunk_size: Number of data entries per chunk.
828-
timeout: Time in seconds to wait until a request timeout error is thrown
824+
url (str): url used to make request
825+
criteria (dict of str): dictionary of criteria to filter down
826+
use_document_model (bool): whether to use the document model
827+
num_chunks (int or None): Maximum number of chunks of data to yield. None will yield all possible.
828+
chunk_size (int): Number of data entries per chunk.
829+
timeout (float): Time in seconds to wait until a request timeout error is thrown
830+
max_batch_size (int) : Maximum size of a batch for when retrieving batches in parallel
829831
830832
Returns:
831833
Dictionary containing data and metadata
@@ -884,14 +886,6 @@ def _submit_requests( # noqa
884886
timeout=timeout,
885887
)
886888

887-
# Check if we got 0 results - some parameters are silently ignored by the API
888-
# when passed as comma-separated values, so we need to split them anyway
889-
if total_num_docs == 0 and len(split_values) > 1:
890-
# Treat this the same as a 422 error - split into batches
891-
raise MPRestError(
892-
"Got 0 results for comma-separated parameter, will try splitting"
893-
)
894-
895889
# If successful, continue with normal pagination
896890
data_chunks = [data["data"]]
897891
total_data: dict[str, Any] = {"data": []}
@@ -904,17 +898,25 @@ def _submit_requests( # noqa
904898

905899
except MPRestError as e:
906900
# If we get 422 or 414 error, or 0 results for comma-separated params, split into batches
907-
if any(trace in str(e) for trace in ("422", "414", "Got 0 results")):
901+
if any(
902+
trace in str(e)
903+
for trace in (
904+
"422",
905+
"414",
906+
)
907+
):
908908
total_data = {"data": []}
909909
total_num_docs = 0
910910
data_chunks = []
911911

912912
# Batch the split values to reduce number of requests
913913
# Use batches of up to 100 values to balance URL length and request count
914-
batch_size = min(100, max(1, len(split_values) // 10))
914+
num_batches = min(
915+
max_batch_size, max(1, len(split_values) // max_batch_size)
916+
)
917+
batch_size = min(len(split_values), max_batch_size)
915918

916919
# Setup progress bar for split parameter requests
917-
num_batches = ceil(len(split_values) / batch_size)
918920
pbar_message = f"Retrieving {len(split_values)} {split_param} values in {num_batches} batches"
919921
pbar = (
920922
tqdm(
@@ -998,7 +1000,7 @@ def _submit_requests( # noqa
9981000
desc=pbar_message,
9991001
total=num_docs_needed,
10001002
)
1001-
if not self.mute_progress_bars
1003+
if not self.mute_progress_bars and total_num_docs > 0
10021004
else None
10031005
)
10041006

0 commit comments

Comments
 (0)