The sgalng api specs (#41585)

This commit is contained in:
Rain Jiang
2026-10-07 01:55:01 -07:00
committed by GitHub
parent ba77d30256
commit c52dcc5a01
95 changed files with 16648 additions and 4458 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,18 @@
"""The HTTP JSON contract gate: `proto/sglang/api/v1` is the schema for both
the Rust and the Python server, so a `/generate` body the Python dataclass
parse admitted but the schema rejects is refused here with the schema's own
error text. The generated decoder is the single source of that judgment.
"""
from typing import Any, Optional
from sglang.api.v1.api_types import GenerateRequest, JsonContractError
def generate_contract_error(body: Any) -> Optional[str]:
"""The contract violation in a parsed `/generate` JSON body, or None."""
try:
GenerateRequest.from_json_value(body)
except JsonContractError as e:
return str(e)
return None
@@ -73,6 +73,7 @@ from sglang.srt.entrypoints.anthropic.protocol import (
AnthropicMessagesRequest,
)
from sglang.srt.entrypoints.anthropic.serving import AnthropicServing
from sglang.srt.entrypoints.api_contract import generate_contract_error
from sglang.srt.entrypoints.engine import (
Engine,
init_tokenizer_manager,
@@ -908,6 +909,11 @@ if os.environ.get("DUMPER_SERVER_PORT") == "reuse":
)
async def generate_request(obj: GenerateReqInput, request: Request):
"""Handle a generate request."""
# Starlette caches the parsed body, so this is the schema check, not a
# second decode.
contract_error = generate_contract_error(await request.json())
if contract_error is not None:
return ORJSONResponse(status_code=400, content={"error": contract_error})
if envs.SGLANG_ENABLE_REQUEST_HEADER_OVERRIDES.get():
apply_header_overrides(obj, request.headers)
if obj.stream: