Skip to content

Commit f3f698e

Browse files
fix(load): update error message in case of dataset not found locally and missing api keys (#1589)
* tests: add tests for config, smart dataframe and smart datalake * fix(dataset): update exception message --------- Co-authored-by: Gabriele Venturi <[email protected]>
1 parent 6e8b3f9 commit f3f698e

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

pandasai/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,17 @@ def load(dataset_path: str) -> DataFrame:
212212
raise ValueError("The path must be in the format 'organization/dataset'.")
213213

214214
dataset_full_path = os.path.join(find_project_root(), "datasets", dataset_path)
215-
if not os.path.exists(dataset_full_path):
215+
216+
local_dataset_exists = os.path.exists(dataset_full_path)
217+
218+
if not local_dataset_exists:
216219
api_key = os.environ.get("PANDABI_API_KEY", None)
217220
api_url = os.environ.get("PANDABI_API_URL", DEFAULT_API_URL)
221+
218222
if not api_url or not api_key:
219-
raise PandaAIApiKeyError()
223+
raise PandaAIApiKeyError(
224+
f'The dataset "{dataset_path}" does not exist in your local datasets directory. In addition, no API Key has been provided. Set an API key with valid permits if you want to fetch the dataset from the remote server.'
225+
)
220226

221227
request_session = get_pandaai_session()
222228

@@ -232,7 +238,16 @@ def load(dataset_path: str) -> DataFrame:
232238
zip_file.extractall(dataset_full_path)
233239

234240
loader = DatasetLoader.create_loader_from_path(dataset_path)
235-
return loader.load()
241+
df = loader.load()
242+
243+
message = (
244+
"Dataset loaded successfully."
245+
if local_dataset_exists
246+
else "Dataset fetched successfully from the remote server."
247+
)
248+
print(message)
249+
250+
return df
236251

237252

238253
def read_csv(filepath: str) -> DataFrame:

tests/unit_tests/test_pandasai_init.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,39 @@ def test_load_dataset_not_found(self, mockenviron, mock_bytes_io, mock_zip_file)
137137
with pytest.raises(DatasetNotFound):
138138
pandasai.load(dataset_path)
139139

140+
@patch("pandasai.os.path.exists")
141+
@patch("pandasai.os.environ", {})
142+
@patch("pandasai.get_pandaai_session")
143+
def test_load_missing_not_found_locally_and_no_remote_key(
144+
self, mock_session, mock_exists
145+
):
146+
"""Test loading when API URL is missing."""
147+
mock_exists.return_value = False
148+
mock_response = MagicMock()
149+
mock_response.status_code = 404
150+
mock_session.return_value.get.return_value = mock_response
151+
dataset_path = "org/dataset_name"
152+
153+
with pytest.raises(
154+
PandaAIApiKeyError,
155+
match='The dataset "org/dataset_name" does not exist in your local datasets directory. In addition, no API Key has been provided. Set an API key with valid permits if you want to fetch the dataset from the remote server.',
156+
):
157+
pandasai.load(dataset_path)
158+
140159
@patch("pandasai.os.path.exists")
141160
@patch("pandasai.os.environ", {"PANDABI_API_KEY": "key"})
142161
def test_load_missing_api_url(self, mock_exists):
143162
"""Test loading when API URL is missing."""
144163
mock_exists.return_value = False
145164
dataset_path = "org/dataset_name"
146165

147-
with pytest.raises(PandaAIApiKeyError):
166+
with pytest.raises(DatasetNotFound):
148167
pandasai.load(dataset_path)
149168

150169
@patch("pandasai.os.path.exists")
151170
@patch("pandasai.os.environ", {"PANDABI_API_KEY": "key"})
152171
@patch("pandasai.get_pandaai_session")
153-
def test_load_missing_api_url(self, mock_session, mock_exists):
172+
def test_load_missing_not_found(self, mock_session, mock_exists):
154173
"""Test loading when API URL is missing."""
155174
mock_exists.return_value = False
156175
mock_response = MagicMock()
@@ -202,7 +221,7 @@ def test_load_without_api_credentials(
202221
pandasai.load("test/dataset")
203222
assert (
204223
str(exc_info.value)
205-
== "PandaAI API key not found. Please set your API key using PandaAI.set_api_key() or by setting the PANDASAI_API_KEY environment variable."
224+
== 'The dataset "test/dataset" does not exist in your local datasets directory. In addition, no API Key has been provided. Set an API key with valid permits if you want to fetch the dataset from the remote server.'
206225
)
207226

208227
def test_clear_cache(self):

0 commit comments

Comments
 (0)