Skip to content

Commit 9e7291f

Browse files
authored
fix(evaluation): avoid duplicate stream keyword (#3836)
1 parent 830e2c9 commit 9e7291f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

backend/utils/llm_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def call_llm_for_system_prompt(
135135
temperature=0.3,
136136
top_p=0.95,
137137
)
138+
# The evaluator consumes the response as a stream. Remove any
139+
# construction-time stream value before forcing the call-level
140+
# streaming mode, otherwise Python receives duplicate keywords.
141+
completion_kwargs.pop("stream", None)
138142
current_request = llm.client.chat.completions.create(stream=True, **completion_kwargs)
139143
token_join: List[str] = []
140144
is_thinking = False

test/backend/utils/test_llm_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ def test_call_llm_for_system_prompt_success(self, mocker: MockFixture):
162162
timeout_seconds=None,
163163
)
164164

165+
def test_call_llm_for_system_prompt_removes_prepared_stream(self, mocker: MockFixture):
166+
mock_get_model_by_id = mocker.patch('backend.utils.llm_utils.get_model_by_model_id')
167+
mock_adapter = mocker.patch('backend.utils.llm_utils.get_llm_adapter_from_config')
168+
169+
mock_get_model_by_id.return_value = {
170+
"base_url": "http://example.com",
171+
"api_key": "fake-key",
172+
"model_factory": "qwen",
173+
}
174+
175+
mock_llm_instance = mock_adapter.return_value
176+
mock_chunk = MagicMock()
177+
mock_chunk.choices = [MagicMock()]
178+
mock_chunk.choices[0].delta.content = "Generated prompt"
179+
mock_llm_instance.client = MagicMock()
180+
mock_llm_instance.client.chat.completions.create.return_value = [mock_chunk]
181+
mock_llm_instance._prepare_completion_kwargs.return_value = {
182+
"stream": False,
183+
"temperature": 0.3,
184+
}
185+
186+
result = call_llm_for_system_prompt(1, "user prompt", "system prompt")
187+
188+
assert result == "Generated prompt"
189+
mock_llm_instance.client.chat.completions.create.assert_called_once_with(
190+
stream=True,
191+
temperature=0.3,
192+
)
193+
165194
def test_call_llm_for_system_prompt_exception(self, mocker: MockFixture):
166195
from consts.error_code import ErrorCode
167196
from consts.exceptions import AppException

0 commit comments

Comments
 (0)