🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
fix(models): map Cohere parallel tool calls to a single choice (#4187)
Ehsan1327d2a3d045
diff --git a/camel/models/cohere_model.py b/camel/models/cohere_model.pyindex 3f976f2b7..9bd5fd6ff 100644--- a/camel/models/cohere_model.py+++ b/camel/models/cohere_model.py@@ -132,50 +132,41 @@ class CohereModel(BaseModelBackend):usage = {}tool_calls = response.message.tool_calls- choices = []if tool_calls:- for tool_call in tool_calls:- openai_tool_calls = [- dict(- id=tool_call.id,- function={- "name": tool_call.function.name,- "arguments": tool_call.function.arguments,- }- if tool_call.function- else {},- type=tool_call.type,- )- ]-- choice = dict(- index=None,- message={- "role": "assistant",- "content": response.message.tool_plan,- "tool_calls": openai_tool_calls,- },- finish_reason=response.finish_reason- if response.finish_reason- else None,+ # A single Cohere turn may carry several parallel tool calls.+ # Collect all of them into one choice so none are dropped by+ # downstream code that only reads choices[0] (mirrors+ # MistralModel._to_openai_response).+ openai_tool_calls = [+ dict(+ id=tool_call.id,+ function={+ "name": tool_call.function.name,+ "arguments": tool_call.function.arguments,+ }+ if tool_call.function+ else {},+ type=tool_call.type,)- choices.append(choice)-+ for tool_call in tool_calls+ ]+ content = response.message.tool_planelse:openai_tool_calls = None-- choice = dict(- index=None,- message={- "role": "assistant",- "content": response.message.content[0].text, # type: ignore[union-attr,index]- "tool_calls": openai_tool_calls,- },- finish_reason=response.finish_reason- if response.finish_reason- else None,- )- choices.append(choice)+ content = response.message.content[0].text # type: ignore[union-attr,index]++ choice = dict(+ index=0,+ message={+ "role": "assistant",+ "content": content,+ "tool_calls": openai_tool_calls,+ },+ finish_reason=response.finish_reason+ if response.finish_reason+ else None,+ )+ choices = [choice]obj = ChatCompletion.construct(id=response.id,diff --git a/test/models/test_cohere_model.py b/test/models/test_cohere_model.pyindex e72f02d3e..840c73984 100644--- a/test/models/test_cohere_model.py+++ b/test/models/test_cohere_model.py@@ -12,6 +12,8 @@# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+from types import SimpleNamespace+import pytestfrom camel.configs import CohereConfig@@ -20,6 +22,84 @@ from camel.types import ModelTypefrom camel.utils import OpenAITokenCounter+def _mock_cohere_tool_call(call_id, name, arguments):+ return SimpleNamespace(+ id=call_id,+ type="function",+ function=SimpleNamespace(name=name, arguments=arguments),+ )+++def _mock_cohere_response(*, tool_calls=None, text=None):+ message = SimpleNamespace(+ tool_plan="calling tools" if tool_calls else None,+ content=None if tool_calls else [SimpleNamespace(text=text)],+ tool_calls=tool_calls,+ )+ return SimpleNamespace(+ id="gen-1",+ finish_reason="tool_call" if tool_calls else "complete",+ usage=SimpleNamespace(+ tokens=SimpleNamespace(input_tokens=11, output_tokens=7)+ ),+ message=message,+ )+++def test_to_openai_response_preserves_parallel_tool_calls():+ r"""A Cohere turn with several parallel tool calls must map to a single+ choices[0] that carries all of them. Downstream code reads only+ ``choices[0].message.tool_calls``, so splitting the calls across separate+ choices silently drops every call after the first."""+ model = CohereModel(ModelType.COHERE_COMMAND_R, api_key="dummy")+ response = _mock_cohere_response(+ tool_calls=[+ _mock_cohere_tool_call("t1", "get_weather", '{"city": "Paris"}'),+ _mock_cohere_tool_call("t2", "get_time", '{"tz": "UTC"}'),+ ]+ )++ result = model._to_openai_response(response)++ assert len(result.choices) == 1+ assert result.choices[0].index == 0+ tool_calls = result.choices[0].message.tool_calls+ assert [tc.id for tc in tool_calls] == ["t1", "t2"]+ assert [tc.function.name for tc in tool_calls] == [+ "get_weather",+ "get_time",+ ]+++def test_to_openai_response_single_tool_call_unchanged():+ r"""The common single-tool-call path still yields one choice, one call."""+ model = CohereModel(ModelType.COHERE_COMMAND_R, api_key="dummy")+ response = _mock_cohere_response(+ tool_calls=[+ _mock_cohere_tool_call("t1", "get_weather", '{"city": "Paris"}')+ ]+ )++ result = model._to_openai_response(response)++ assert len(result.choices) == 1+ assert len(result.choices[0].message.tool_calls) == 1+ assert result.choices[0].message.tool_calls[0].id == "t1"+++def test_to_openai_response_text_only():+ r"""A plain text turn maps to one choice with no tool calls."""+ model = CohereModel(ModelType.COHERE_COMMAND_R, api_key="dummy")+ response = _mock_cohere_response(text="hello there")++ result = model._to_openai_response(response)++ assert len(result.choices) == 1+ assert result.choices[0].index == 0+ assert result.choices[0].message.content == "hello there"+ assert result.choices[0].message.tool_calls is None++@pytest.mark.model_backend@pytest.mark.parametrize("model_type",