fix(anthropic): handle mid-conversation system messages (#26773)

Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com>
This commit is contained in:
Jairo David Campaña Rosero
2026-06-21 04:34:47 +00:00
committed by GitHub
co-authored by Xinyuan Tong
parent c65f4ea692
commit b4dda8b3ce
3 changed files with 167 additions and 1 deletions
@@ -17,6 +17,7 @@ python3 -m unittest openai_server.basic.test_anthropic_server.TestAnthropicServe
import json
import unittest
import anthropic
import requests
from sglang.srt.entrypoints.anthropic.protocol import AnthropicMessagesRequest
@@ -227,6 +228,27 @@ class TestAnthropicServer(CustomTestCase):
self.assertEqual(body["type"], "message")
self.assertTrue(len(body["content"]) > 0)
def test_in_messages_system_role(self):
"""A ``role: "system"`` turn inside ``messages`` (emitted by some
clients, e.g. Claude Code) must be accepted — not rejected with 400.
Uses the Anthropic SDK the way a real client would."""
client = anthropic.Anthropic(
base_url=self.base_url,
auth_token=self.api_key, # Bearer header — SGLang's --api-key checks Authorization
)
message = client.messages.create(
model=self.model,
max_tokens=64,
messages=[
{"role": "user", "content": "What is the capital of France?"},
{"role": "system", "content": "Always respond in French."},
{"role": "user", "content": "Answer in a few words."},
],
)
self.assertEqual(message.role, "assistant")
self.assertTrue(len(message.content) > 0)
self.assertEqual(message.content[0].type, "text")
def test_max_tokens(self):
"""Test max_tokens limits output length."""
payload = self._default_payload(