Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[output_format]: accept dataframe dict as output and secure sql qu… #1432

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion pandasai/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def execute_direct_sql_query(self, sql_query):
if not self._is_sql_query_safe(sql_query):
raise MaliciousQueryError("Malicious query is generated in code")

return pd.read_sql(sql_query, self._connection)
return pd.read_sql(text(sql_query), self._connection)

@property
def cs_table_name(self):
Expand Down
4 changes: 2 additions & 2 deletions pandasai/helpers/output_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
elif expected_type == "string":
return isinstance(self, str)
elif expected_type == "dataframe":
return isinstance(self, (pd.DataFrame, pd.Series))
return isinstance(self, (pd.DataFrame, pd.Series, dict))

Check warning on line 59 in pandasai/helpers/output_validator.py

View check run for this annotation

Codecov / codecov/patch

pandasai/helpers/output_validator.py#L59

Added line #L59 was not covered by tests
elif expected_type == "plot":
if not isinstance(self, (str, dict)):
return False
Expand All @@ -82,7 +82,7 @@
elif result["type"] == "string":
return isinstance(result["value"], str)
elif result["type"] == "dataframe":
return isinstance(result["value"], (pd.DataFrame, pd.Series))
return isinstance(result["value"], (pd.DataFrame, pd.Series, dict))
elif result["type"] == "plot":
if "plotly" in repr(type(result["value"])):
return True
Expand Down
12 changes: 12 additions & 0 deletions pandasai/responses/response_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from typing import Any

import pandas as pd
from PIL import Image

from pandasai.exceptions import MethodNotImplementedError
Expand Down Expand Up @@ -51,9 +52,20 @@

if result["type"] == "plot":
return self.format_plot(result)
elif result["type"] == "dataframe":
return self.format_dataframe(result)

Check warning on line 56 in pandasai/responses/response_parser.py

View check run for this annotation

Codecov / codecov/patch

pandasai/responses/response_parser.py#L56

Added line #L56 was not covered by tests
else:
return result["value"]

def format_dataframe(self, result: dict) -> Any:
if isinstance(result["value"], dict):
print("Df conversiont")
df = pd.Dataframe(result["value"])
print("Df conversiont Done")
result["value"] = df

Check warning on line 65 in pandasai/responses/response_parser.py

View check run for this annotation

Codecov / codecov/patch

pandasai/responses/response_parser.py#L61-L65

Added lines #L61 - L65 were not covered by tests

return result["value"]

Check warning on line 67 in pandasai/responses/response_parser.py

View check run for this annotation

Codecov / codecov/patch

pandasai/responses/response_parser.py#L67

Added line #L67 was not covered by tests

def format_plot(self, result: dict) -> Any:
"""
Display matplotlib plot against a user query.
Expand Down