#4299·camel

[BUG] ChatAgent.reset() never clears OpenAIModel previous_response_id chain state, so a pooled agent's next task can continue the wrong conversation

Author: AmirF194Created Aug 27, 2026Updated Sep 27, 2026

Required prerequisites

  • I have read the documentation.
  • I have searched the Issue Tracker and Discussions that this hasn't already been reported.

What version of camel are you using?

0.2.91a5 (current master, commit e88b5eebe9f8bb5597196384c818fb4e3c63b25c)

System information

Reproduced in a clean python:3.12-slim Docker container, pip-installed from an unmodified checkout of the commit above (pip install -e .), Python 3.12.14, no network calls.

Problem description

OpenAIModel stores the OpenAI Responses API chaining state (previous_response_id, message count) in two dicts keyed by a session key, written in _save_response_chain_state and read in _prepare_responses_input_and_chain (camel/models/openai_model.py). ChatAgent.reset() (camel/agents/chat_agent.py:656) clears the agent's own memory, tool-output history and terminators, but never calls OpenAIModel._clear_response_chain_state, so this state survives a reset.

ChatAgent.clone() (chat_agent.py:6193) passes model=self.model_backend.models, the existing model backend, into the new agent rather than constructing a fresh one. workforce.AgentPool (camel/societies/workforce/single_agent_worker.py) builds its pool with clone(with_memory=False) and calls agent.reset() in both get_agent() and return_agent() to hand a pooled agent to the next unrelated task. Because the clone shares the same OpenAIModel instance, and reset() never clears its response-chain dicts, a pooled agent's next task inherits the previous task's previous_response_id.

Two effects follow from this: the new task's turn is silently chained onto a different task's OpenAI conversation, and because _prepare_responses_input_and_chain only sends messages past the stored last_message_count, the new task's own system prompt can be dropped from what is actually sent to the API.

Reproducible example code

python
from camel.models.openai_model import OpenAIModel
from camel.types import ModelType

model = OpenAIModel(model_type=ModelType.GPT_4O_MINI, api_key="sk-test")
session_key = model._get_response_chain_session_key()

# Task A finishes and its response is saved, as _save_response_chain_state does
# after a real API call.
messages_a = [{"role": "user", "content": "Task A message"}]
model._prepare_responses_input_and_chain(messages_a)
model._responses_previous_response_id_by_session[session_key] = "resp_from_task_A"
model._responses_last_message_count_by_session[session_key] = len(messages_a)

# AgentPool.get_agent()/return_agent() calls agent.reset() here, which never
# touches this state.

# Task B reuses the same pooled agent/model instance for an unrelated task.
messages_b = [
    {"role": "system", "content": "Task B system prompt"},
    {"role": "user", "content": "Task B message"},
]
result_b = model._prepare_responses_input_and_chain(messages_b)
print(result_b["previous_response_id"])   # resp_from_task_A -- leaked from task A
print(result_b["input_messages"])         # task B's system prompt is missing

Steps to reproduce:

  1. Install this exact commit and run the snippet above.
  2. Observe result_b["previous_response_id"] equals task A's saved id.
  3. Observe result_b["input_messages"] omits task B's system prompt.

Expected behavior

reset() should clear the model backend's response-chain state (call _clear_response_chain_state for the current session key) so a reused or pooled agent starts each new task with no inherited chaining, and clone() reusing the same model instance shouldn't let two unrelated tasks share this state at all.

Additional context

Same defect exists in camel/models/openai_compatible_model.py, which duplicates the identical pattern, and azure_openai_model.py inherits it. AgentPool (single_agent_worker.py) is the concrete production caller that exercises the reuse path (get_agent()/return_agent() both call reset() on a pooled agent handed to a different task).

Happy to send a PR for this if useful.