Skip to content

Commit a674c86

Browse files
validation into the InteractiveChartResponse
1 parent 92a815e commit a674c86

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

pandasai/core/response/interactive_chart.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class InteractiveChartResponse(BaseResponse):
99
def __init__(self, value: Any, last_code_executed: str):
1010
super().__init__(value, "ichart", last_code_executed)
1111

12+
self._validate()
13+
1214
def _get_chart(self) -> dict:
1315
if isinstance(self.value, dict):
1416
return self.value
@@ -32,3 +34,16 @@ def __str__(self) -> str:
3234

3335
def get_dict_image(self) -> dict:
3436
return self._get_chart()
37+
38+
def _validate(self):
39+
if not isinstance(self.value, (dict, str)):
40+
raise ValueError("InteractiveChartResponse value must be a dict or a str representing a file path.")
41+
42+
# if a string, it can be a path to a file or a JSON string
43+
if isinstance(self.value, str):
44+
try:
45+
json.loads(self.value) # Check if it's a valid JSON string
46+
except json.JSONDecodeError:
47+
# If it fails, check if it's a valid file path
48+
if not os.path.exists(self.value):
49+
raise ValueError("InteractiveChartResponse value must be a valid file path or a JSON string.")

0 commit comments

Comments
 (0)