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(models): map the Bedrock and Mistral finish reasons that are not OpenAI values (#4262)

Ehsana130c891ea11
diff --git a/camel/models/aws_bedrock_converse_model.py b/camel/models/aws_bedrock_converse_model.py
index 5aa015197..e20e7c5e7 100644
--- a/camel/models/aws_bedrock_converse_model.py
+++ b/camel/models/aws_bedrock_converse_model.py
@@ -624,6 +624,9 @@ class AWSBedrockConverseModel(BaseModelBackend):
"max_tokens": "length",
"stop_sequence": "stop",
"tool_use": "tool_calls",
+ "content_filtered": "content_filter",
+ "guardrail_intervened": "content_filter",
+ "model_context_window_exceeded": "length",
}.get(stop_reason or "", "stop")
@staticmethod
diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.py
index f73d97568..2389572c3 100644
--- a/camel/models/mistral_model.py
+++ b/camel/models/mistral_model.py
@@ -57,6 +57,16 @@ else:
from camel.utils import observe
+# "model_length" is Mistral's own length stop, which OpenAI expresses as
+# "length".
+_FINISH_REASON_MAP = {
+ "stop": "stop",
+ "length": "length",
+ "tool_calls": "tool_calls",
+ "model_length": "length",
+}
+
+
class MistralModel(BaseModelBackend):
r"""Mistral API in a unified BaseModelBackend interface.
@@ -128,6 +138,21 @@ class MistralModel(BaseModelBackend):
**kwargs,
)
+ @staticmethod
+ def _map_finish_reason(mistral_reason: str) -> str:
+ r"""Map a Mistral finish reason to its OpenAI equivalent."""
+ if mistral_reason == "error":
+ raise RuntimeError(
+ "Mistral generation failed with finish reason "
+ f"{mistral_reason!r}"
+ )
+ try:
+ return _FINISH_REASON_MAP[mistral_reason]
+ except KeyError as exc:
+ raise ValueError(
+ f"Unknown Mistral finish reason: {mistral_reason!r}"
+ ) from exc
+
def _to_openai_response(
self, response: 'ChatCompletionResponse'
) -> ChatCompletion:
@@ -159,7 +184,9 @@ class MistralModel(BaseModelBackend):
"content": response.choices[0].message.content, # type: ignore[index,union-attr]
"tool_calls": tool_calls,
},
- finish_reason=response.choices[0].finish_reason # type: ignore[index]
+ finish_reason=self._map_finish_reason(
+ response.choices[0].finish_reason # type: ignore[index]
+ )
if response.choices[0].finish_reason # type: ignore[index]
else None,
)
diff --git a/test/models/test_aws_bedrock_converse_model.py b/test/models/test_aws_bedrock_converse_model.py
index 4a25d0489..0a0b316ac 100644
--- a/test/models/test_aws_bedrock_converse_model.py
+++ b/test/models/test_aws_bedrock_converse_model.py
@@ -352,3 +352,79 @@ def test_bedrock_client_supports_api_key_and_region_name(monkeypatch):
assert client["service_name"] == "bedrock-runtime"
assert client["kwargs"]["region_name"] == "us-east-1"
assert "aws_access_key_id" not in client["kwargs"]
+
+
+ "stop_reason,expected",
+ [
+ ("content_filtered", "content_filter"),
+ ("guardrail_intervened", "content_filter"),
+ ("model_context_window_exceeded", "length"),
+ ],
+)
+def test_converse_maps_non_default_stop_reasons(stop_reason, expected):
+ r"""Non-streaming: a filtered, guardrailed or context-exceeded turn
+ must not be reported as a normal completion."""
+
+ class DummyClient:
+ def converse(self, **kwargs):
+ return {
+ "output": {
+ "message": {
+ "role": "assistant",
+ "content": [{"text": "partial"}],
+ }
+ },
+ "stopReason": stop_reason,
+ }
+
+ model = _make_model(bedrock_client=DummyClient())
+ response = model._run([{"role": "user", "content": "hi"}])
+ assert response.choices[0].finish_reason == expected
+
+
+ "stop_reason,expected",
+ [
+ ("content_filtered", "content_filter"),
+ ("guardrail_intervened", "content_filter"),
+ ("model_context_window_exceeded", "length"),
+ ],
+)
+def test_converse_stream_maps_non_default_stop_reasons(stop_reason, expected):
+ r"""Streaming path reports the same finish_reason as non-streaming."""
+
+ class DummyEventStream:
+ def __init__(self, events):
+ self._events = events
+
+ def __iter__(self):
+ return iter(self._events)
+
+ class DummyClient:
+ def converse_stream(self, **kwargs):
+ return {
+ "stream": DummyEventStream(
+ [
+ {"messageStart": {"role": "assistant"}},
+ {"contentBlockDelta": {"delta": {"text": "partial"}}},
+ {"messageStop": {"stopReason": stop_reason}},
+ ]
+ )
+ }
+
+ model = _make_model(
+ BedrockConfig(stream=True).as_dict(),
+ bedrock_client=DummyClient(),
+ )
+ chunks = list(model._run([{"role": "user", "content": "hi"}]))
+ assert chunks[-1].choices[0].finish_reason == expected
+
+
+def test_converse_unknown_stop_reason_still_defaults_to_stop():
+ r"""Values with no OpenAI equivalent keep the generic default."""
+ for stop_reason in ("malformed_model_output", "malformed_tool_use"):
+ assert AWSBedrockConverseModel._map_stop_reason(stop_reason) == "stop"
diff --git a/test/models/test_mistral_model.py b/test/models/test_mistral_model.py
index 562fa9b3c..e6d11c4c5 100644
--- a/test/models/test_mistral_model.py
+++ b/test/models/test_mistral_model.py
@@ -12,6 +12,8 @@
# limitations under the License.
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+from types import SimpleNamespace
+
import pytest
from camel.configs import MistralConfig
@@ -20,6 +22,27 @@ from camel.types import ModelType
from camel.utils import OpenAITokenCounter
+def _mock_mistral_response(finish_reason):
+ r"""A minimal Mistral ChatCompletionResponse carrying one choice."""
+ return SimpleNamespace(
+ id="cmpl-1",
+ created=1700000000,
+ model="mistral-large-latest",
+ usage=SimpleNamespace(
+ prompt_tokens=11, completion_tokens=7, total_tokens=18
+ ),
+ choices=[
+ SimpleNamespace(
+ index=0,
+ finish_reason=finish_reason,
+ message=SimpleNamespace(
+ role="assistant", content="hi", tool_calls=None
+ ),
+ )
+ ],
+ )
+
+
@pytest.mark.model_backend
@pytest.mark.parametrize(
"model_type",
@@ -105,3 +128,66 @@ def test_to_mistral_chatmessage_preserves_tool_call_ids(monkeypatch):
second_tool_message = mistral_messages[3]
assert first_tool_message.tool_call_id == tool_call_id_1
assert second_tool_message.tool_call_id == tool_call_id_2
+
+
+def test_to_openai_response_maps_model_length_to_length(monkeypatch):
+ r"""Mistral's own context-limit stop must reach the caller as the
+ OpenAI term for a length stop. Left raw, a truncated generation is
+ unreadable to any consumer that branches on "length"."""
+ monkeypatch.setenv("MISTRAL_API_KEY", "test_key")
+ model = MistralModel(ModelType.MISTRAL_LARGE, MistralConfig().as_dict())
+
+ result = model._to_openai_response(_mock_mistral_response("model_length"))
+
+ assert result.choices[0].finish_reason == "length"
+
+
[email protected]("finish_reason", ["stop", "length", "tool_calls"])
+def test_to_openai_response_passes_through_openai_reasons(
+ monkeypatch, finish_reason
+):
+ r"""The three Mistral reasons that are already OpenAI terms are
+ unchanged by the mapping."""
+ monkeypatch.setenv("MISTRAL_API_KEY", "test_key")
+ model = MistralModel(ModelType.MISTRAL_LARGE, MistralConfig().as_dict())
+
+ result = model._to_openai_response(_mock_mistral_response(finish_reason))
+
+ assert result.choices[0].finish_reason == finish_reason
+
+
+def test_to_openai_response_raises_for_failed_generation(monkeypatch):
+ r"""Mistral's "error" reason has no OpenAI equivalent, so it is raised
+ rather than reported as a finished generation."""
+ monkeypatch.setenv("MISTRAL_API_KEY", "test_key")
+ model = MistralModel(ModelType.MISTRAL_LARGE, MistralConfig().as_dict())
+
+ with pytest.raises(RuntimeError, match="error"):
+ model._to_openai_response(_mock_mistral_response("error"))
+
+
+def test_to_openai_response_finish_reason_none_stays_none(monkeypatch):
+ r"""A response with no finish reason still maps to None, not to a
+ default, so an unfinished generation is not reported as completed."""
+ monkeypatch.setenv("MISTRAL_API_KEY", "test_key")
+ model = MistralModel(ModelType.MISTRAL_LARGE, MistralConfig().as_dict())
+
+ result = model._to_openai_response(_mock_mistral_response(None))
+
+ assert result.choices[0].finish_reason is None
+
+
+def test_to_openai_response_raises_for_unknown_finish_reason(monkeypatch):
+ r"""Unknown finish reasons must not reach OpenAI-compatible consumers."""
+ monkeypatch.setenv("MISTRAL_API_KEY", "test_key")
+ model = MistralModel(ModelType.MISTRAL_LARGE, MistralConfig().as_dict())
+
+ with pytest.raises(
+ ValueError, match="Unknown Mistral finish reason: 'some_new_reason'"
+ ):
+ model._to_openai_response(_mock_mistral_response("some_new_reason"))