From de450696ec7585ea140e568f4d27a22368cdc481 Mon Sep 17 00:00:00 2001 From: Victoria Usan Date: Fri, 15 May 2026 11:40:33 +0200 Subject: [PATCH] Adding offset clamp --- langchain_apify/tools.py | 5 ++++- tests/unit_tests/test_tools.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/langchain_apify/tools.py b/langchain_apify/tools.py index 710ee45..bfff3f8 100644 --- a/langchain_apify/tools.py +++ b/langchain_apify/tools.py @@ -372,6 +372,9 @@ def _clamp_memory(self, value: int | None) -> int | None: def _clamp_items(self, value: int) -> int: return max(1, min(value, self.max_items)) + def _clamp_offset(value: int) -> int: + return max(0, value) + # --------------------------------------------------------------------------- # Generic tools @@ -478,7 +481,7 @@ def _run( _run_manager: CallbackManagerForToolRun | None = None, ) -> str: try: - items = self._client.get_dataset_items(dataset_id, self._clamp_items(limit), offset) + items = self._client.get_dataset_items(dataset_id, self._clamp_items(limit), self._clamp_offset(offset)) except RuntimeError as exc: raise ToolException(str(exc)) from exc if not items: diff --git a/tests/unit_tests/test_tools.py b/tests/unit_tests/test_tools.py index 4702f24..39f3594 100644 --- a/tests/unit_tests/test_tools.py +++ b/tests/unit_tests/test_tools.py @@ -572,6 +572,22 @@ def test_clamp_items_floor_is_one(mock_tools_client: MagicMock) -> None: mock_tools_client.get_dataset_items.assert_called_once_with('ds-1', 1, 0) +def test_clamp_offset_floor_is_zero(mock_tools_client: MagicMock) -> None: + mock_tools_client.get_dataset_items.return_value = SAMPLE_ITEMS + tool = make_tool(ApifyGetDatasetItemsTool, mock_tools_client, max_items=100) + + tool._run(dataset_id='ds-1', offset=-5) + mock_tools_client.get_dataset_items.assert_called_once_with('ds-1', 100, 0) + + mock_tools_client.get_dataset_items.reset_mock() + tool._run(dataset_id='ds-1', offset=0) + mock_tools_client.get_dataset_items.assert_called_once_with('ds-1', 100, 0) + + mock_tools_client.get_dataset_items.reset_mock() + tool._run(dataset_id='ds-1', offset=10) + mock_tools_client.get_dataset_items.assert_called_once_with('ds-1', 100, 10) + + def test_values_below_max_pass_through(mock_tools_client: MagicMock) -> None: """When LLM values are within limits they should pass through unchanged.""" mock_tools_client.run_actor.return_value = SUCCEEDED_RUN