Release v3 is currently in beta. This documentation reflects the features and functionality in progress and may change before the final release.

How does PandasAI NL Layer work?

The Natural Language Layer uses generative AI to transform natural language queries into production-ready code generated by LLMs. When you use the .chat method on a dataframe, PandasAI passes to the LLM the question, the table headers, and 5-10 rows of the Dataframe. It then instructs the LLM to generate the most relevant code, whether Python or SQL. The code is then executed locally. There are different output formats supported by PandasAI, which can be found here.

Configure the NL Layer

PandasAI allows you to configure the NL Layer with the config.set() method.

Example:

import pandasai as pai
from pandasai_litellm.litellm import LiteLLM

# Initialize LiteLLM with your OpenAI model
llm = LiteLLM(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY")

pai.config.set({
   "llm": llm,
   "save_logs": True,
   "verbose": False,
   "max_retries": 3
})

Parameters

llm

  • Description: The LLM to use. You can pass an instance of an LLM or the name of an LLM. See supported LLMs for setup instructions and configuration options.

save_logs

  • Type: bool
  • Default: True
  • Description: Whether to save the logs of the LLM. You will find the logs in the pandasai.log file in the root of your project.

verbose

  • Type: bool
  • Default: False
  • Description: Whether to print the logs in the console as PandasAI is executed.

max_retries

  • Type: int
  • Default: 3
  • Description: The maximum number of retries to use when using the error correction framework. You can use this setting to override the default number of retries.