Conversation
External OpenAI-compatible frontends that must interpret Anthropic Messages exactly as this server does (e.g. token-in-token-out session routers) previously had to vendor protocol.py/serving.py and maintain a derived codec. Add anthropic/utils.py so the server and external callers share one conversion implementation, with serving.py and protocol.py untouched: - to_openai_request / to_anthropic_response delegate to the original AnthropicServing methods through a runtime-detached instance (the conversion paths only read _merge_inline_system; the reasoning paths that need OpenAIServingChat are rejected up front) - AnthropicRequestContext feature gates let a frontend that verified only a subset of typed features fail closed with a 400 instead of the server's accept-and-log policy; thinking is always rejected - to_anthropic_error returns the envelope DTO against a composite map: serving.py's ERROR_TYPE_MAP plus the 413/422 entries the HTTP layer maps in http_server.py's /v1/messages exception handler - to_anthropic_fake_sse_events eagerly synthesizes the Anthropic SSE event sequence from one complete ChatCompletionResponse for callers that proxy a finished non-streaming response test_utils.py pins delegation equivalence against a normally constructed AnthropicServing (so new instance-state dependencies in the serving methods fail loudly), absolute goldens for the conversion semantics, a depth-2 gate canary, and an AST cross-check of the 413/422 policy against http_server.py's handler source. Co-authored-by: Shi Dong <shi.dong@radixark.ai> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
added 2 commits
August 20, 2026 03:50
Move request and response conversion into module-level functions used by both AnthropicServing and standalone utilities. This removes the half-initialized conversion-only serving instance while preserving reasoning hooks and wire behavior.
Keep request and response conversion in the module-level serving helpers. Limit utils to error mapping and eager fake-SSE adaptation, removing the unused request context and deployment-policy matrix.
yueming-yuan
force-pushed
the
sglang-miles
branch
from
August 27, 2026 07:57
a6100a0 to
97e43b4
Compare
yueming-yuan
requested review from
CatherineSue,
Fridge003,
HaiShaw,
Qiaolin-Yu,
Ying1123,
b8zhong,
ch-wan,
fzyzcjy,
hnyls2002,
ispobock,
jybsuper,
key4ng,
kpham-sgl,
lifuhuang,
merrymercy,
mickqian,
mmangkad,
slin1237,
xiezhq-hermann,
yctseng0211,
yhyang201,
yuan-luo and
yushengsu-thu
as code owners
August 27, 2026 07:57
yueming-yuan
requested review from
1am9trash,
Alisehen,
AniZpZ,
BBuf,
ByronHsu,
DarkSharpness,
Duyi-Wang,
Edwardf0t1,
FlamingoPg,
HydraQYH,
OrangeRedeng,
ShangmingCai,
YAMY1234,
celve,
hebiao064,
hubertlu-tw,
kkHuang-amd,
rainj-me,
sogalin and
yizhang2077
as code owners
August 27, 2026 07:57
guapisolo
force-pushed
the
refactor/anthropic-conversion-utils
branch
from
August 28, 2026 07:06
1ed0c90 to
e95eae4
Compare
Kh4L
pushed a commit
to Kh4L/sglang
that referenced
this pull request
Sep 8, 2026
…l-project#35127) Co-authored-by: Shi Dong <shi.dong@radixark.ai> (cherry picked from commit 4e230c3)
46 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Expose SGLang's Anthropic/OpenAI conversion for reuse by external frontends.
Motivation
External OpenAI-compatible frontends that must interpret Anthropic Messages exactly as this server does (e.g. Miles PR #2358) previously had to vendor
protocol.py/serving.pyand maintain a derived codec, plus a sync ledger to keep the copies honest. This gives the server and external callers one conversion implementation, maintained here.Usage
An external handler validates
AnthropicMessagesRequest, then wraps its own OpenAI path with the shared converters:The result uses the same conversion as SGLang's
/v1/messages; endpoint validation, routing, and transport remain caller-owned.Design Notes
/v1/messageshandler parsesAnthropicMessagesRequest; passes its deployment'smerge_inline_systemchoice plus optional reasoning callbacks intoconvert_to_chat_completion_request; runs the resultingChatCompletionRequestthrough its OpenAI-compatible backend; then converts the completed response withconvert_responseorto_anthropic_fake_sse_events. SGLang owns format conversion; the caller owns endpoint policy, session state, and transport.convert_to_chat_completion_requestreceives request data plus explicit inline-system policy and optional reasoning hooks;convert_responseis data-only.AnthropicServinginvokes both while supplying hooks fromOpenAIServingChat, preserving server routing plus live streaming.utils.pycontains Anthropic error-envelope mapping plus eager fake-SSE construction from a completed OpenAI response. Raw-body parsing, feature policy, routing, and state remain caller-owned; no fake serving instance,AnthropicRequestContext, orallow_*matrix is exposed.Verification
PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=python python -m pytest -p no:cacheprovider -q test/registered/unit/entrypoints/anthropic/test_utils.py test/registered/unit/entrypoints/anthropic/test_serving.py:84 passed, 55 subtests passed; covers request/response goldens, existing serving behavior, error mapping, fake SSE, and import hygiene.tests/fast/router/test_session_anthropic.pyplustests/fast/utils/test_utils/test_mock_sglang_server.py:58 passed, 1 skipped; verifies the paired session/TITO integration.pre-commit run --files python/sglang/srt/entrypoints/anthropic/utils.py test/registered/unit/entrypoints/anthropic/test_utils.py: passed.Review Focus
convert_to_chat_completion_request/AnthropicServing: reasoning-callback injection and preservation of server conversion behavior.utils.py: error mapping, message scrubbing, and the eager fake-SSE boundary.merge_inline_system, request/response types, and completed-response streaming.CI States
Latest PR Test (Base): ❌ Run #33150255041
Latest PR Test (Extra): ❌ Run #33150254812
Latest PR Test (AMD ROCm 7.2): ❌ Run #33150255067