🐫 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: support vLLM 'reasoning' field as fallback for 'reasoning_content' (#3959)
Evod6515b5bbb77
diff --git a/camel/models/vllm_model.py b/camel/models/vllm_model.pyindex 514baeaf0..d684e5638 100644--- a/camel/models/vllm_model.py+++ b/camel/models/vllm_model.py@@ -11,14 +11,15 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+import inspectimport osimport subprocess-from typing import Any, Dict, Optional, Union+from typing import Any, AsyncGenerator, Dict, Generator, Optional, Unionfrom camel.configs import VLLMConfigfrom camel.logger import get_loggerfrom camel.models.openai_compatible_model import OpenAICompatibleModel-from camel.types import ModelType+from camel.types import ChatCompletionChunk, ModelTypefrom camel.utils import BaseTokenCounterlogger = get_logger(__name__)@@ -88,6 +89,46 @@ class VLLMModel(OpenAICompatibleModel):**kwargs,)+ def _normalize_reasoning_content(self, response: Any) -> Any:+ r"""Normalize vLLM ``reasoning`` to CAMEL ``reasoning_content``."""+ for choice in getattr(response, "choices", []) or []:+ message = getattr(choice, "message", None) or getattr(+ choice, "delta", None+ )+ if message is None:+ continue+ reasoning = getattr(message, "reasoning", None)+ if reasoning and not getattr(message, "reasoning_content", None):+ message.reasoning_content = reasoning+ return response++ def _wrap_stream_reasoning(+ self, stream: Any+ ) -> Generator[ChatCompletionChunk, None, None]:+ for chunk in stream:+ yield self._normalize_reasoning_content(chunk)++ async def _wrap_async_stream_reasoning(+ self, stream: Any+ ) -> AsyncGenerator[ChatCompletionChunk, None]:+ async for chunk in stream:+ yield self._normalize_reasoning_content(chunk)++ def postprocess_response(self, response: Any) -> Any:+ r"""Normalize vLLM reasoning fields before agent consumption."""+ response = super().postprocess_response(response)++ if inspect.isasyncgen(response) or (+ hasattr(response, "__aiter__") and hasattr(response, "__anext__")+ ):+ return self._wrap_async_stream_reasoning(response)+ if inspect.isgenerator(response) or (+ hasattr(response, "__iter__") and hasattr(response, "__next__")+ ):+ return self._wrap_stream_reasoning(response)++ return self._normalize_reasoning_content(response)+def _start_server(self) -> None:r"""Starts the vllm server in a subprocess."""try:diff --git a/test/models/test_vllm_model.py b/test/models/test_vllm_model.pyindex d840e1291..a43df4d16 100644--- a/test/models/test_vllm_model.py+++ b/test/models/test_vllm_model.py@@ -12,14 +12,69 @@# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+from typing import Any, cast+import pytest+from openai.types.chat.chat_completion import Choice+from openai.types.chat.chat_completion_chunk import (+ ChatCompletionChunk,+ ChoiceDelta,+)+from openai.types.chat.chat_completion_chunk import (+ Choice as ChunkChoice,+)+from openai.types.chat.chat_completion_message import ChatCompletionMessagefrom camel.configs import VLLMConfigfrom camel.models import VLLMModel-from camel.types import ModelType, UnifiedModelType+from camel.types import ChatCompletion, ModelType, UnifiedModelTypefrom camel.utils import OpenAITokenCounter+def _make_vllm_model() -> VLLMModel:+ return VLLMModel(+ ModelType.GPT_4O_MINI,+ api_key="vllm",+ url="http://localhost:8000/v1",+ )+++def _make_chat_completion(+ reasoning: str,+ reasoning_content: str | None = None,+) -> ChatCompletion:+ message = ChatCompletionMessage(role="assistant", content="answer")+ message_with_reasoning = cast(Any, message)+ message_with_reasoning.reasoning = reasoning+ if reasoning_content is not None:+ message_with_reasoning.reasoning_content = reasoning_content+ return ChatCompletion(+ id="test",+ model="test",+ object="chat.completion",+ created=0,+ choices=[Choice(index=0, finish_reason="stop", message=message)],+ )+++def _make_chat_completion_chunk(reasoning: str) -> ChatCompletionChunk:+ delta = ChoiceDelta(content=None, role="assistant")+ cast(Any, delta).reasoning = reasoning+ return ChatCompletionChunk(+ id="test",+ choices=[+ ChunkChoice(+ index=0,+ delta=delta,+ finish_reason=None,+ )+ ],+ created=0,+ model="test",+ object="chat.completion.chunk",+ )++@pytest.mark.model_backend@pytest.mark.parametrize("model_type",@@ -37,3 +92,67 @@ def test_vllm_model(model_type: ModelType):assert isinstance(model.token_counter, OpenAITokenCounter)assert isinstance(model.model_type, UnifiedModelType)assert isinstance(model.token_limit, int)+++def test_vllm_postprocess_maps_reasoning_to_reasoning_content():+ model = _make_vllm_model()+ response = _make_chat_completion(reasoning="provider reasoning")++ result = model.postprocess_response(response)++ assert result.choices[0].message.reasoning_content == "provider reasoning"+++def test_vllm_postprocess_preserves_reasoning_content():+ model = _make_vllm_model()+ response = _make_chat_completion(+ reasoning="provider reasoning",+ reasoning_content="canonical reasoning",+ )++ result = model.postprocess_response(response)++ assert result.choices[0].message.reasoning_content == "canonical reasoning"+++def test_vllm_run_maps_stream_reasoning_to_reasoning_content():+ class StreamingVLLMModel(VLLMModel):+ def _run(self, messages, response_format=None, tools=None):+ yield _make_chat_completion_chunk("streamed reasoning")++ model = StreamingVLLMModel(+ ModelType.GPT_4O_MINI,+ api_key="vllm",+ url="http://localhost:8000/v1",+ )++ chunks = list(model.run([{"role": "user", "content": "test"}]))++ assert chunks[0].choices[0].delta.reasoning_content == (+ "streamed reasoning"+ )+++async def test_vllm_arun_maps_stream_reasoning_to_reasoning_content():+ class AsyncStreamingVLLMModel(VLLMModel):+ async def _arun(self, messages, response_format=None, tools=None):+ async def stream():+ yield _make_chat_completion_chunk("async streamed reasoning")++ return stream()++ model = AsyncStreamingVLLMModel(+ ModelType.GPT_4O_MINI,+ api_key="vllm",+ url="http://localhost:8000/v1",+ )++ stream = await model.arun([{"role": "user", "content": "test"}])+ chunks = []+ async for chunk in stream:+ chunks.append(chunk)++ assert chunks[0].choices[0].delta.reasoning_content == (+ "async streamed reasoning"+ )