fix: normalize tool message content for GLM5.1 chat template (#22595)

This commit is contained in:
ybyang
2026-04-16 16:48:38 +08:00
committed by GitHub
parent aaa682346e
commit fbd6dc3565
2 changed files with 67 additions and 1 deletions
@@ -19,7 +19,10 @@ from sglang.srt.entrypoints.openai.protocol import (
ChatCompletionRequest,
MessageProcessingResult,
)
from sglang.srt.entrypoints.openai.serving_chat import OpenAIServingChat
from sglang.srt.entrypoints.openai.serving_chat import (
OpenAIServingChat,
normalize_tool_content,
)
from sglang.srt.managers.io_struct import GenerateReqInput
from sglang.srt.utils import get_or_create_event_loop
from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci
@@ -982,5 +985,42 @@ class TestProcessToolCallsWithRequiredToolChoice(unittest.TestCase):
self.assertIsNone(tool_calls)
class TestNormalizeToolContent(unittest.TestCase):
"""Unit tests for normalize_tool_content()."""
def test_openai_text_parts_flattened(self):
result = normalize_tool_content("tool", [{"type": "text", "text": "10525"}])
self.assertEqual(result, "10525")
def test_multiple_text_parts_joined(self):
result = normalize_tool_content(
"tool",
[{"type": "text", "text": "hello"}, {"type": "text", "text": "world"}],
)
self.assertEqual(result, "hello world")
def test_non_text_part_list_preserved(self):
content = [{"name": "func", "output": "result"}]
result = normalize_tool_content("tool", content)
self.assertIs(result, content)
def test_string_content_unchanged(self):
self.assertEqual(normalize_tool_content("tool", "hello"), "hello")
def test_empty_list_returns_empty_string(self):
self.assertEqual(normalize_tool_content("tool", []), "")
def test_non_tool_role_unchanged(self):
content = [{"type": "text", "text": "hi"}]
result = normalize_tool_content("user", content)
self.assertIs(result, content)
def test_mixed_str_and_dict_parts(self):
result = normalize_tool_content(
"tool", ["plain", {"type": "text", "text": "rich"}]
)
self.assertEqual(result, "plain rich")
if __name__ == "__main__":
unittest.main(verbosity=2)