Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion langchain_apify/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading