From 7ae20ebd1d3327c941314ce068a2450f1adc8d60 Mon Sep 17 00:00:00 2001 From: Br1an67 <932039080@qq.com> Date: Sun, 1 Mar 2026 14:41:59 +0800 Subject: [PATCH] fix: reset last_code_generated and last_prompt_used on new conversation start_new_conversation() cleared memory but left last_code_generated and last_prompt_used on the agent state, causing stale context to leak into subsequent conversations. Reset both fields to None alongside the memory clear. Closes #1855 --- pandasai/agent/base.py | 2 ++ tests/unit_tests/agent/test_agent.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/pandasai/agent/base.py b/pandasai/agent/base.py index 4cca5112b..75f1e1cd4 100644 --- a/pandasai/agent/base.py +++ b/pandasai/agent/base.py @@ -267,6 +267,8 @@ def start_new_conversation(self): Clears the previous conversation """ self.clear_memory() + self._state.last_code_generated = None + self._state.last_prompt_used = None def _process_query(self, query: str, output_type: Optional[str] = None): """Process a user query and return the result.""" diff --git a/tests/unit_tests/agent/test_agent.py b/tests/unit_tests/agent/test_agent.py index 555a65aa9..de1735340 100644 --- a/tests/unit_tests/agent/test_agent.py +++ b/tests/unit_tests/agent/test_agent.py @@ -256,6 +256,14 @@ def test_start_new_conversation(self, sample_df, config): memory = agent._state.memory.all() assert len(memory) == 0 + def test_start_new_conversation_resets_state(self, sample_df, config): + agent = Agent(sample_df, config, memory_size=10) + agent._state.last_code_generated = "result = df.head()" + agent._state.last_prompt_used = "Show me the first 5 rows" + agent.start_new_conversation() + assert agent._state.last_code_generated is None + assert agent._state.last_prompt_used is None + def test_code_generation_success(self, agent: Agent): # Mock the code generator agent._code_generator = Mock()