diff --git a/swift/pipelines/infer/deploy.py b/swift/pipelines/infer/deploy.py index c0dd5833b4..f1f751f445 100644 --- a/swift/pipelines/infer/deploy.py +++ b/swift/pipelines/infer/deploy.py @@ -151,7 +151,7 @@ def _post_process(self, request_info, response, return_cmpl_response: bool = Fal is_finished = all(response.choices[i].finish_reason for i in range(len(response.choices))) if 'stream' in response.__class__.__name__.lower(): - request_info['response'] += response.choices[0].delta.content + request_info['response'] += response.choices[0].delta.content or '' else: request_info['response'] = response.choices[0].message.content if return_cmpl_response: diff --git a/tests/deploy/test_deploy.py b/tests/deploy/test_deploy.py new file mode 100644 index 0000000000..65fc51dc59 --- /dev/null +++ b/tests/deploy/test_deploy.py @@ -0,0 +1,27 @@ +from types import SimpleNamespace + +from swift.infer_engine.protocol import ChatCompletionResponseStreamChoice, ChatCompletionStreamResponse, DeltaMessage +from swift.pipelines.infer.deploy import SwiftDeploy + + +def test_post_process_reasoning_only_stream_chunk(): + deploy = object.__new__(SwiftDeploy) + deploy.args = SimpleNamespace() + request_info = {'response': ''} + response = ChatCompletionStreamResponse( + model='test', + choices=[ + ChatCompletionResponseStreamChoice( + index=0, + delta=DeltaMessage(content=None, reasoning_content='thinking'), + finish_reason=None, + ) + ], + ) + + processed = deploy._post_process(request_info, response) + + assert processed is response + assert processed.choices[0].delta.content is None + assert processed.choices[0].delta.reasoning_content == 'thinking' + assert request_info['response'] == ''