Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: tool message sequence issue (#3781)

Wendong-Fan41309829323e
diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.yml
index b3b105d43..696f2a0e1 100644
--- a/.github/workflows/pytest_package.yml
+++ b/.github/workflows/pytest_package.yml
@@ -114,6 +114,7 @@ jobs:
--ignore=test/models/reward/test_nemotron_model.py \
--ignore=test/toolkits/test_video_analysis_toolkit.py \
--ignore=test/toolkits/test_jina_reranker_toolkit.py \
+ --ignore=test/agents/test_agent_e2e.py \
test/
pytest_package_llm_test:
@@ -209,6 +210,7 @@ jobs:
--ignore=test/models/reward/test_nemotron_model.py \
--ignore=test/toolkits/test_video_analysis_toolkit.py \
--ignore=test/toolkits/test_jina_reranker_toolkit.py \
+ --ignore=test/agents/test_agent_e2e.py \
test/
pytest_package_very_slow_test:
@@ -302,4 +304,5 @@ jobs:
--ignore=test/models/reward/test_nemotron_model.py \
--ignore=test/toolkits/test_video_analysis_toolkit.py \
--ignore=test/toolkits/test_jina_reranker_toolkit.py \
+ --ignore=test/agents/test_agent_e2e.py \
test/
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index feecb78d6..c6337408d 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -2896,6 +2896,10 @@ class ChatAgent(BaseAgent):
iteration_count: int = 0
prev_num_openai_messages: int = 0
+ # Track if we've recorded tool calls for the current response
+ # to avoid duplicate assistant message recording
+ recorded_tool_calls = False
+
while True:
if self.pause_event is not None and not self.pause_event.is_set():
# Use efficient blocking wait for threading.Event
@@ -2949,8 +2953,12 @@ class ChatAgent(BaseAgent):
"termination_triggered",
)
+ # Reset flag for each iteration
+ recorded_tool_calls = False
+
if tool_call_requests := response.tool_call_requests:
- # Process all tool calls
+ # Separate internal and external tool calls
+ internal_tool_requests = []
for tool_call_request in tool_call_requests:
if (
tool_call_request.tool_name
@@ -2960,17 +2968,33 @@ class ChatAgent(BaseAgent):
external_tool_call_requests = []
external_tool_call_requests.append(tool_call_request)
else:
- if (
- self.pause_event is not None
- and not self.pause_event.is_set()
- ):
- if isinstance(self.pause_event, threading.Event):
- self.pause_event.wait()
- else:
- while not self.pause_event.is_set():
- time.sleep(0.001)
- result = self._execute_tool(tool_call_request)
- tool_call_records.append(result)
+ internal_tool_requests.append(tool_call_request)
+
+ # Record the assistant message with ALL tool calls (internal +
+ # external)
+ response_content = ""
+ if response.output_messages:
+ response_content = (
+ response.output_messages[0].content or ""
+ )
+ self._record_assistant_tool_calls_from_requests(
+ tool_call_requests, content=response_content
+ )
+ recorded_tool_calls = True
+
+ # Execute internal tools only
+ for tool_call_request in internal_tool_requests:
+ if (
+ self.pause_event is not None
+ and not self.pause_event.is_set()
+ ):
+ if isinstance(self.pause_event, threading.Event):
+ self.pause_event.wait()
+ else:
+ while not self.pause_event.is_set():
+ time.sleep(0.001)
+ result = self._execute_tool(tool_call_request)
+ tool_call_records.append(result)
# If we found external tool calls, break the loop
if external_tool_call_requests:
@@ -3033,7 +3057,10 @@ class ChatAgent(BaseAgent):
response, original_response_format
)
- self._record_final_output(response.output_messages)
+ # Only record final output if we haven't already recorded tool calls
+ # for this response (to avoid duplicate assistant messages)
+ if not recorded_tool_calls:
+ self._record_final_output(response.output_messages)
# Clean tool call messages from memory after response generation
if self.prune_tool_calls_from_memory and tool_call_records:
@@ -3169,6 +3196,10 @@ class ChatAgent(BaseAgent):
iteration_count: int = 0
prev_num_openai_messages: int = 0
+ # Track if we've recorded tool calls for the current response
+ # to avoid duplicate assistant message recording
+ recorded_tool_calls = False
+
while True:
if self.pause_event is not None and not self.pause_event.is_set():
if isinstance(self.pause_event, asyncio.Event):
@@ -3221,8 +3252,12 @@ class ChatAgent(BaseAgent):
"termination_triggered",
)
+ # Reset flag for each iteration
+ recorded_tool_calls = False
+
if tool_call_requests := response.tool_call_requests:
- # Process all tool calls
+ # Separate internal and external tool calls
+ internal_tool_requests = []
for tool_call_request in tool_call_requests:
if (
tool_call_request.tool_name
@@ -3232,21 +3267,37 @@ class ChatAgent(BaseAgent):
external_tool_call_requests = []
external_tool_call_requests.append(tool_call_request)
else:
- if (
- self.pause_event is not None
- and not self.pause_event.is_set()
- ):
- if isinstance(self.pause_event, asyncio.Event):
- await self.pause_event.wait()
- elif isinstance(self.pause_event, threading.Event):
- loop = asyncio.get_event_loop()
- await loop.run_in_executor(
- None, self.pause_event.wait
- )
- tool_call_record = await self._aexecute_tool(
- tool_call_request
- )
- tool_call_records.append(tool_call_record)
+ internal_tool_requests.append(tool_call_request)
+
+ # Record the assistant message with ALL tool calls (internal +
+ # external) BEFORE executing any tools.
+ response_content = ""
+ if response.output_messages:
+ response_content = (
+ response.output_messages[0].content or ""
+ )
+ self._record_assistant_tool_calls_from_requests(
+ tool_call_requests, content=response_content
+ )
+ recorded_tool_calls = True
+
+ # Execute internal tools only
+ for tool_call_request in internal_tool_requests:
+ if (
+ self.pause_event is not None
+ and not self.pause_event.is_set()
+ ):
+ if isinstance(self.pause_event, asyncio.Event):
+ await self.pause_event.wait()
+ elif isinstance(self.pause_event, threading.Event):
+ loop = asyncio.get_event_loop()
+ await loop.run_in_executor(
+ None, self.pause_event.wait
+ )
+ tool_call_record = await self._aexecute_tool(
+ tool_call_request
+ )
+ tool_call_records.append(tool_call_record)
# If we found an external tool call, break the loop
if external_tool_call_requests:
@@ -3308,7 +3359,10 @@ class ChatAgent(BaseAgent):
response, original_response_format
)
- self._record_final_output(response.output_messages)
+ # Only record final output if we haven't already recorded tool calls
+ # for this response (to avoid duplicate assistant messages)
+ if not recorded_tool_calls:
+ self._record_final_output(response.output_messages)
# Clean tool call messages from memory after response generation
if self.prune_tool_calls_from_memory and tool_call_records:
@@ -3973,8 +4027,11 @@ class ChatAgent(BaseAgent):
mask_output: bool = False,
extra_content: Optional[Dict[str, Any]] = None,
):
- r"""Record the tool calling information in the memory, and return the
- tool calling record.
+ r"""Record the tool result in the memory.
+
+ Note: The assistant message with tool_calls should be recorded BEFORE
+ calling this method (via _record_assistant_tool_calls_from_requests).
+ This method only records the tool result message.
Args:
func_name (str): The name of the tool function called.
@@ -3999,16 +4056,9 @@ class ChatAgent(BaseAgent):
)
result_for_memory = truncated_result if was_truncated else result
- assist_msg = FunctionCallingMessage(
- role_name=self.role_name,
- role_type=self.role_type,
- meta_dict=None,
- content="",
- func_name=func_name,
- args=args,
- tool_call_id=tool_call_id,
- extra_content=extra_content,
- )
+ # Create the tool response message
+ # Note: assistant message with tool_calls is already recorded by
+ # _record_assistant_tool_calls_from_requests
func_msg = FunctionCallingMessage(
role_name=self.role_name,
role_type=self.role_type,
@@ -4021,25 +4071,10 @@ class ChatAgent(BaseAgent):
extra_content=extra_content,
)
- # Use precise timestamps to ensure correct ordering
- # This ensures the assistant message (tool call) always appears before
- # the function message (tool result) in the conversation context
- # Use time.time_ns() for nanosecond precision to avoid collisions
- current_time_ns = time.time_ns()
- base_timestamp = current_time_ns / 1_000_000_000 # Convert to seconds
-
- self.update_memory(
- assist_msg,
- OpenAIBackendRole.ASSISTANT,
- timestamp=base_timestamp,
- return_records=self._enable_snapshot_clean,
- )
-
- # Add minimal increment to ensure function message comes after
+ # Record only the tool result message
func_records = self.update_memory(
func_msg,
OpenAIBackendRole.FUNCTION,
- timestamp=base_timestamp + 1e-6,
return_records=self._enable_snapshot_clean,
)
@@ -4095,12 +4130,10 @@ class ChatAgent(BaseAgent):
image_list=pil_images,
)
- # Inject into conversation context with slight
- # timestamp increment
+ # Inject into conversation context
self.update_memory(
visual_msg,
OpenAIBackendRole.USER,
- timestamp=base_timestamp + 2e-6,
return_records=False,
)
logger.info(
@@ -4143,6 +4176,13 @@ class ChatAgent(BaseAgent):
content, tool calls, and other information as they become
available.
"""
+ # Handle response format compatibility with non-strict tools
+ input_message, response_format, _ = (
+ self._handle_response_format_with_non_strict_tools(
+ input_message, response_format
+ )
+ )
+
# Convert input message to BaseMessage if necessary
if isinstance(input_message, str):
input_message = BaseMessage.make_user_message(
@@ -4720,6 +4760,9 @@ class ChatAgent(BaseAgent):
return
yield # Make this a generator
+ # Record the assistant message with ALL tool calls
+ self._record_assistant_tool_calls_message(accumulated_tool_calls)
+
# Execute tools using ThreadPoolExecutor for proper timeout handling
# Use max_workers=len() for parallel execution, with min of 1
with concurrent.futures.ThreadPoolExecutor(
@@ -4779,7 +4822,12 @@ class ChatAgent(BaseAgent):
def _execute_tool_from_stream_data(
self, tool_call_data: Dict[str, Any]
) -> Optional[ToolCallingRecord]:
- r"""Execute a tool from accumulated stream data."""
+ r"""Execute a tool from accumulated stream data.
+
+ Note: The assistant message with tool_calls should be recorded BEFORE
+ calling this method (via _record_assistant_tool_calls_message).
+ This method only records the tool result message.
+ """
try:
function_name = tool_call_data['function']['name']
@@ -4812,20 +4860,9 @@ class ChatAgent(BaseAgent):
truncated_result if was_truncated else result
)
- # First, create and record the assistant message with tool
- # call
- assist_msg = FunctionCallingMessage(
- role_name=self.role_name,
- role_type=self.role_type,
- meta_dict=None,
- content="",
- func_name=function_name,
- args=args,
- tool_call_id=tool_call_id,
- extra_content=extra_content,
- )
-
- # Then create the tool response message
+ # Create the tool response message
+ # Note: assistant message with tool_calls is already
+ # recorded by _record_assistant_tool_calls_message
func_msg = FunctionCallingMessage(
role_name=self.role_name,
role_type=self.role_type,
@@ -4838,23 +4875,8 @@ class ChatAgent(BaseAgent):
extra_content=extra_content,
)
- # Record both messages with precise timestamps to ensure
- # correct ordering
- current_time_ns = time.time_ns()
- base_timestamp = (
- current_time_ns / 1_000_000_000
- ) # Convert to seconds
-
- self.update_memory(
- assist_msg,
- OpenAIBackendRole.ASSISTANT,
- timestamp=base_timestamp,
- )
- self.update_memory(
- func_msg,
- OpenAIBackendRole.FUNCTION,
- timestamp=base_timestamp + 1e-6,
- )
+ # Record only the tool result message
+ self.update_memory(func_msg, OpenAIBackendRole.FUNCTION)
tool_record = ToolCallingRecord(
tool_name=function_name,
@@ -4929,7 +4951,12 @@ class ChatAgent(BaseAgent):
async def _aexecute_tool_from_stream_data(
self, tool_call_data: Dict[str, Any]
) -> Optional[ToolCallingRecord]:
- r"""Async execute a tool from accumulated stream data."""
+ r"""Async execute a tool from accumulated stream data.
+
+ Note: The assistant message with tool_calls should be recorded BEFORE
+ calling this method (via _record_assistant_tool_calls_message).
+ This method only records the tool result message.
+ """
try:
function_name = tool_call_data['function']['name']
@@ -4938,24 +4965,6 @@ class ChatAgent(BaseAgent):
extra_content = tool_call_data.get('extra_content')
if function_name in self._internal_tools:
- # Create the tool call message
- assist_msg = FunctionCallingMessage(
- role_name=self.role_name,
- role_type=self.role_type,
- meta_dict=None,
- content="",
- func_name=function_name,
- args=args,
- tool_call_id=tool_call_id,
- extra_content=extra_content,
- )
- assist_ts = time.time_ns() / 1_000_000_000
- self.update_memory(
- assist_msg,
- OpenAIBackendRole.ASSISTANT,
- timestamp=assist_ts,
- )
-
tool = self._internal_tools[function_name]
try:
# Try different invocation paths in order of preference
@@ -5010,6 +5019,8 @@ class ChatAgent(BaseAgent):
)
# Create the tool response message
+ # Note: assistant message with tool_calls is already
+ # recorded by _record_assistant_tool_calls_message
func_msg = FunctionCallingMessage(
role_name=self.role_name,
role_type=self.role_type,
@@ -5021,12 +5032,7 @@ class ChatAgent(BaseAgent):
mask_output=self.mask_tool_output,
extra_content=extra_content,
)
- func_ts = time.time_ns() / 1_000_000_000
- self.update_memory(
- func_msg,
- OpenAIBackendRole.FUNCTION,
- timestamp=func_ts,
- )
+ self.update_memory(func_msg, OpenAIBackendRole.FUNCTION)
tool_record = ToolCallingRecord(
tool_name=function_name,
@@ -5055,12 +5061,7 @@ class ChatAgent(BaseAgent):
tool_call_id=tool_call_id,
extra_content=extra_content,
)
- func_ts = time.time_ns() / 1_000_000_000
- self.update_memory(
- func_msg,
- OpenAIBackendRole.FUNCTION,
- timestamp=func_ts,
- )
+ self.update_memory(func_msg, OpenAIBackendRole.FUNCTION)
tool_record = ToolCallingRecord(
tool_name=function_name,
@@ -5397,7 +5398,10 @@ class ChatAgent(BaseAgent):
# Create a BaseMessage with tool_calls information in meta_dict
# This will be converted to the proper OpenAI format when needed
tool_calls_list = []
- for tool_call_data in accumulated_tool_calls.values():
+ for key, tool_call_data in accumulated_tool_calls.items():
+ # Skip internal mapping key
+ if key == '_index_to_key_map':
+ continue
if tool_call_data.get('complete', False):
tool_call_dict = {
"id": tool_call_data["id"],
@@ -5414,6 +5418,10 @@ class ChatAgent(BaseAgent):
]
tool_calls_list.append(tool_call_dict)
+ # Early return if no tool calls to record
+ if not tool_calls_list:
+ return
+
# Create an assistant message with tool calls
assist_msg = BaseMessage(
role_name=self.role_name,
@@ -5425,6 +5433,51 @@ class ChatAgent(BaseAgent):
# Record this assistant message
self.update_memory(assist_msg, OpenAIBackendRole.ASSISTANT)
+ def _record_assistant_tool_calls_from_requests(
+ self, tool_call_requests: List["ToolCallRequest"], content: str = ""
+ ) -> None:
+ r"""Record assistant message with tool calls from requests.
+
+ This method creates and records an assistant message that includes
+ all the tool calls from a list of ToolCallRequest objects.
+ Used for non-streaming tool execution to ensure proper message
+ sequence.
+
+ Args:
+ tool_call_requests: List of tool call requests from model response.
+ content: Optional content to include in the assistant message.
+ """
+ import json
+
+ tool_calls_list = []
+ for request in tool_call_requests:
+ tool_call_dict = {
+ "id": request.tool_call_id,
+ "type": "function",
+ "function": {
+ "name": request.tool_name,
+ "arguments": json.dumps(request.args, ensure_ascii=False),
+ },
+ }
+ # Include extra_content if present
+ if request.extra_content:
+ tool_call_dict["extra_content"] = request.extra_content
+ tool_calls_list.append(tool_call_dict)
+
+ if not tool_calls_list:
+ return
+
+ # Create an assistant message with all tool calls
+ assist_msg = BaseMessage(
+ role_name=self.role_name,
+ role_type=self.role_type,
+ meta_dict={"tool_calls": tool_calls_list},
+ content=content or "",
+ )
+
+ # Record this assistant message
+ self.update_memory(assist_msg, OpenAIBackendRole.ASSISTANT)
+
async def _aprocess_stream_chunks_with_accumulator(
self,
stream: Union[
@@ -5608,6 +5661,10 @@ class ChatAgent(BaseAgent):
proper content accumulation."""
import asyncio
+ # Record the assistant message with ALL tool calls
+ # Note: _record_assistant_tool_calls_message has early return if empty
+ self._record_assistant_tool_calls_message(accumulated_tool_calls)
+
# Phase 1: Start all tools and yield "Calling function"
# statuses immediately
tool_tasks = []
diff --git a/test/agents/test_agent_e2e.py b/test/agents/test_agent_e2e.py
new file mode 100644
index 000000000..17cee51f9
--- /dev/null
+++ b/test/agents/test_agent_e2e.py
@@ -0,0 +1,112 @@
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+"""
+E2E tests for ChatAgent with tools:
+1. Normal (non-streaming) with tools
+2. Structured output with tools
+3. Streaming with tools
+4. Streaming with structured output and tools #TODO
+"""
+
+import pytest
+from pydantic import BaseModel, Field
+
+from camel.agents import ChatAgent
+from camel.models import ModelFactory
+from camel.toolkits import MathToolkit, SearchToolkit
+from camel.types import ModelPlatformType, ModelType
+
+
+def test_normal_with_tools():
+ """Normal (non-streaming) example with parallel tool calls."""
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ )
+
+ agent = ChatAgent(
+ system_message="You are a helpful assistant. ",
+ model=model,
+ tools=[*MathToolkit().get_tools(), SearchToolkit().search_wiki],
+ )
+
+ response = agent.step(
+ "Calculate: 1) 123.45 + 678.90 2) 100 * 3.14159 3) 1000 / 7, "
+ "also search what is the capital of Japan"
+ )
+
+ # Verify tool calls were made and results are correct
+ tool_calls = response.info.get("tool_calls", [])
+ assert len(tool_calls) >= 3, "Should have at least 3 tool calls"
+
+ content = response.msgs[0].content
+ assert "802" in content
+ assert "314" in content
+ assert "142" in content
+
+ class Result(BaseModel):
+ sum_result: str = Field(description="Result of the addition")
+ product_result: str = Field(description="Result of the multiplication")
+ division_result: str = Field(description="Result of the division")
+ capital_result: str = Field(description="Result of the capital search")
+
+ response = agent.step(
+ "Calculate: 1) 123.45 + 678.90 2) 100 * 3.14159 3) 1000 / 7, "
+ "also search what is the capital of China",
+ response_format=Result,
+ )
+
+ # Verify structured response (allow for rounding)
+ parsed = response.msgs[0].parsed
+ assert "802" in parsed.sum_result
+ assert "314" in parsed.product_result
+ assert "142" in parsed.division_result
+
+
+def test_streaming_with_tools():
+ """Streaming example with parallel tool calls."""
+ streaming_model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ model_config_dict={
+ "stream": True,
+ "stream_options": {"include_usage": True},
+ },
+ )
+
+ agent = ChatAgent(
+ system_message="You are a helpful assistant. ",
+ model=streaming_model,
+ tools=[*MathToolkit().get_tools(), SearchToolkit().search_wiki],
+ stream_accumulate=True, # Accumulate content
+ )
+
+ streaming_response = agent.step(
+ "Calculate: 1) 123.45 + 678.90 2) 100 * 3.14159 3) 1000 / 7, "
+ "also search what is the capital of France"
+ )
+
+ for _ in streaming_response:
+ pass # Consume the stream
+
+ # Verify tool calls were made and results are correct
+ tool_calls = streaming_response.info.get("tool_calls", [])
+ assert len(tool_calls) >= 3, "Should have at least 3 tool calls"
+
+ content = streaming_response.msgs[0].content
+ assert "802" in content
+ assert "314" in content
+ assert "142" in content