OpenAI GPT with PandasAI: Queries that worked before now throw errors or fail type conversions

I have been using PandasAI together with OpenAI models (both GPT-4o and GPT-3.5-turbo) to query Excel data using plain English questions.

For several days, everything worked perfectly. I had a set of test queries that always gave me the right answers.

But around late June 2025, some queries that used to work started breaking. Nothing changed on my end:

  • Same Excel files with identical data
  • Same code and prompts
  • Same query text

My current setup:

  • Loading Excel data with pandas.read_excel()
  • Using pandas-ai library with SmartDataframe or SmartDatalake
  • OpenAI API for GPT models
  • Custom prompts that add formatting rules
  • Field descriptions and custom parser

What I notice:

  • Query works one day, fails the next day with same inputs
  • The AI seems to generate bad Python code now (like trying to divide text strings or calling string methods on numbers)

Here’s my code:

from pandasai import SmartDataframe, SmartDatalake
from pandasai.llm.openai import OpenAI

# Setup OpenAI connection
model = OpenAI(api_token="my_api_key", temperature=0, model="gpt-4o")

# Custom formatting instructions
format_instructions = "Numbers over 1000 need commas, rates and percentages need % symbol"
column_info = (
    "Available columns: (Product Type, Event Count, Mean Value 2023/2024, "
    "Class X, Class Y, Success Rate (% 2f), etc.)"
)

# Choose configuration based on query type
if "percentage" not in query_input.lower():
    data_agent = SmartDataframe(
        st.session_state.main_data,
        config={
            "llm": model,
            "conversational": False,
            "custom_prompts": {
                "data_analysis": (
                    "Create grouped summaries with percentages when needed, "
                    "format output as tables by default."
                )
            },
            "verbose": True,
            "field_descriptions": descriptions,
            "response_parser": MyCustomParser,
        },
    )
    final_query = query_input + format_instructions + " Display as table."
else:
    data_agent = SmartDatalake(
        [st.session_state.main_data, st.session_state.backup_data],
        config={
            "llm": model,
            "conversational": False,
            "custom_prompts": {
                "data_analysis": (
                    "Create grouped summaries with percentages when needed, "
                    "format output as tables by default."
                )
            },
            "verbose": True,
            "field_descriptions": descriptions,
            "response_parser": MyCustomParser,
        },
    )
    final_query = query_input + column_info + format_instructions + " Display as table."

# Execute the query
result, timeout_occurred = execute_with_timer(data_agent, final_query)

Anyone else seeing this issue with PandasAI and OpenAI recently?

This screams LLM model drift messing with code generation. I’ve hit the same wall with production systems using GPT for pandas stuff.

What worked for me: throw a code validation layer in front before execution. I use a basic Python AST parser to catch obvious type mismatches before running anything. When validation breaks, I retry with a more explicit prompt that spells out data types for each column.

Try setting a fixed seed in your OpenAI config if you haven’t already. Temperature=0 helps but won’t guarantee identical outputs across API calls.

I also log the actual Python code PandasAI spits out so I can spot failure patterns. Most of my issues match yours - the model randomly flips numeric columns to strings or the other way around. Probably because context understanding shifted in recent model updates.

Same issue hit me around the same time. OpenAI models changed how they read dataframe schemas, so the generated pandas code now has wonky type inference. Here’s what fixed it for me: I add explicit dtype casting right in my custom prompts. Before any query, I specify “Product Type is string, Event Count is integer, Success Rate is float” directly in the prompt. Forces the AI to generate code with proper types. Also found that SmartDataframe vs SmartDatalake matters more than you’d think. The AI handles column references differently between single vs multi-dataframe setups - might explain your percentage query weirdness. Quick fix: wrap sketchy operations with explicit pandas astype() calls in your prompts. Something like “convert numeric columns with .astype(float) before calculations”. Not pretty, but it’ll stop those string division errors.

Same issue here since late June. OpenAI probably tweaked their model outputs and broke PandasAI’s code generation. Try adding explicit type hints to your custom prompts or pin a specific model version. Also check if pandas updated - dependency changes can mess with the AI’s dataframe method assumptions.

I’ve hit this exact problem on multiple projects. PandasAI relies too much on LLMs to generate correct pandas code, and like you said, model outputs drift over time.

I stopped fighting unreliable code generation and moved my Excel workflows to automation platforms. The trick is having predefined data processing logic that doesn’t need AI to write pandas operations.

I built flows that load Excel files, run transformations based on query patterns, and format outputs consistently. No more crossing fingers that the AI generates valid Python code or dealing with string/number type confusion.

For percentage calculations and table formatting, you can create automated workflows that handle these operations reliably. The flows detect query types and route to the right data processing logic.

This eliminated all the randomness I was getting with AI-generated pandas code. Instead of debugging why the AI suddenly thinks it can divide strings, the data operations are predictable and stable.

You can build these reliable data analysis automations at https://latenode.com