{"record":{"id":"2b8412efd23a27a4","repo":"apache/beam","slug":"only-dataframes-with-single-rows-are-supported","errorCode":null,"errorMessage":"Only dataframes with single rows are supported.","messagePattern":"Only dataframes with single rows are supported\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/sklearn_inference.py","lineNumber":322,"sourceCode":"      inference_args: Optional[dict[str, Any]] = None\n  ) -> Iterable[PredictionResult]:\n    \"\"\"\n    Runs inferences on a batch of pandas dataframes.\n\n    Args:\n      batch: A sequence of examples as numpy arrays. They should\n        be single examples.\n      model: A dataframe model or pipeline. Must implement predict(X).\n        Where the parameter X is a pandas dataframe.\n      inference_args: Any additional arguments for an inference.\n\n    Returns:\n      An Iterable of type PredictionResult.\n    \"\"\"\n    # sklearn_inference currently only supports single rowed dataframes.\n    for dataframe in iter(batch):\n      if dataframe.shape[0] != 1:\n        raise ValueError('Only dataframes with single rows are supported.')\n\n    predictions, splits = self._model_inference_fn(model, batch, inference_args)\n\n    return utils._convert_to_result(\n        splits, predictions, model_id=self._model_uri)\n\n  def get_num_bytes(self, batch: Sequence[pandas.DataFrame]) -> int:\n    \"\"\"\n    Returns:\n      The number of bytes of data for a batch.\n    \"\"\"\n    return sum(df.memory_usage(deep=True).sum() for df in batch)\n\n  def get_metrics_namespace(self) -> str:\n    \"\"\"\n    Returns:\n       A namespace for metrics collected by the RunInference transform.\n    \"\"\"","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/sklearn_inference.py#L304-L340","documentation":"Raised in the KeyedModelHandler's run_inference for sklearn when a pandas DataFrame in the batch has more (or fewer) than one row. The sklearn dataframe inference path only supports single-row dataframes per element, and each batched dataframe is validated with dataframe.shape[0] != 1.","triggerScenarios":"Using SklearnModelHandlerKeyedModel (dataframe input) with batch_size > 1 or elements that are multi-row DataFrames; batching logic combining multiple rows into one DataFrame before run_inference is invoked.","commonSituations":"Setting batch_size larger than 1 with dataframe inputs; preprocessing code that concatenates rows into a single DataFrame per key; misusing the keyed dataframe handler with tensors/arrays meant for the numpy handler.","solutions":["Ensure each input element is a DataFrame with exactly one row","Set batch_size=1 (or use the default) with the dataframe-based sklearn handler","Switch to the numpy-based handler if your inputs are arrays and you want multi-row batching"],"exampleFix":"// before\nhandler = SklearnModelHandlerKeyedModel(model_uri=uri)\n_ = pcoll | RunInference(handler)  # elements are multi-row DataFrames\n// after\ndf_single = df.iloc[[0]]  # one row per element\nhandler = SklearnModelHandlerKeyedModel(model_uri=uri, batch_size=1)","handlingStrategy":"validation","validationCode":"def validate_dataframe_inputs(elements):\n    for k, df in elements:\n        if getattr(df, 'shape', (0,))[0] != 1:\n            raise ValueError(f'Element for key {k!r} has {df.shape[0]} rows; sklearn handler supports exactly 1 row per DataFrame.')","typeGuard":"def is_single_row_dataframe(x) -> bool:\n    import pandas as pd\n    return isinstance(x, pd.DataFrame) and x.shape[0] == 1","tryCatchPattern":"try:\n    result = pcoll | RunInference(handler)\nexcept ValueError as e:\n    if 'single rows' in str(e):\n        raise RuntimeError('Reshape inputs to one row per DataFrame or lower batch_size') from e\n    raise","preventionTips":["Keep batch_size at default/1 for dataframe-based sklearn handlers","Split multi-row frames into single-row elements before RunInference","Use the numpy handler for array inputs that need multi-row batching"],"tags":["python","apache-beam","sklearn","pandas","batching"],"backgroundTag":"shape-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}