diff --git a/backend/utils/llm_utils.py b/backend/utils/llm_utils.py index 6ff6c2ce6..d1e461f1f 100644 --- a/backend/utils/llm_utils.py +++ b/backend/utils/llm_utils.py @@ -135,6 +135,10 @@ def call_llm_for_system_prompt( temperature=0.3, top_p=0.95, ) + # The evaluator consumes the response as a stream. Remove any + # construction-time stream value before forcing the call-level + # streaming mode, otherwise Python receives duplicate keywords. + completion_kwargs.pop("stream", None) current_request = llm.client.chat.completions.create(stream=True, **completion_kwargs) token_join: List[str] = [] is_thinking = False diff --git a/test/backend/utils/test_llm_utils.py b/test/backend/utils/test_llm_utils.py index dee16ab6e..06d99db95 100644 --- a/test/backend/utils/test_llm_utils.py +++ b/test/backend/utils/test_llm_utils.py @@ -162,6 +162,35 @@ def test_call_llm_for_system_prompt_success(self, mocker: MockFixture): timeout_seconds=None, ) + def test_call_llm_for_system_prompt_removes_prepared_stream(self, mocker: MockFixture): + mock_get_model_by_id = mocker.patch('backend.utils.llm_utils.get_model_by_model_id') + mock_adapter = mocker.patch('backend.utils.llm_utils.get_llm_adapter_from_config') + + mock_get_model_by_id.return_value = { + "base_url": "http://example.com", + "api_key": "fake-key", + "model_factory": "qwen", + } + + mock_llm_instance = mock_adapter.return_value + mock_chunk = MagicMock() + mock_chunk.choices = [MagicMock()] + mock_chunk.choices[0].delta.content = "Generated prompt" + mock_llm_instance.client = MagicMock() + mock_llm_instance.client.chat.completions.create.return_value = [mock_chunk] + mock_llm_instance._prepare_completion_kwargs.return_value = { + "stream": False, + "temperature": 0.3, + } + + result = call_llm_for_system_prompt(1, "user prompt", "system prompt") + + assert result == "Generated prompt" + mock_llm_instance.client.chat.completions.create.assert_called_once_with( + stream=True, + temperature=0.3, + ) + def test_call_llm_for_system_prompt_exception(self, mocker: MockFixture): from consts.error_code import ErrorCode from consts.exceptions import AppException