Skip to main content
This document describes the /v1/classify API endpoint in SGLang.

Overview

The classification API allows you to classify text inputs using classification models.

API Endpoint

Output

Request Format

Config

Parameters

  • model (string, optional): The name of the classification model to use. Defaults to default
  • input (string, list of strings, or list of token IDs, required): The text to classify, a batch of texts, or the token IDs of one input
  • user (string, optional): User identifier for tracking
  • rid (string, optional): Request ID for tracking
  • priority (integer, optional): Request priority

Response Format

Config

Response Fields

  • id: Unique identifier for the classification request
  • object: Always “list”
  • created: Unix timestamp when the request was created
  • model: The model used for classification
  • data: Array of classification results
    • index: Index of the result
    • label: Predicted class label, or one label per input token for token classifiers
    • probs: Array of probabilities for each class, or one such array per input token for token classifiers
    • num_classes: Total number of classes
  • usage: Token usage information
    • prompt_tokens: Number of input tokens
    • total_tokens: Total number of tokens
    • completion_tokens: Number of completion tokens (always 0 for classification)
    • prompt_tokens_details: Additional token details (optional)

Multi-label classification

When the model’s config.json sets "problem_type": "multi_label_classification", /v1/classify applies an elementwise sigmoid to the logits and returns an independent probability for each class in probs. These probabilities do not need to sum to one. For example, logits [2.0, 2.0] produce approximately [0.880797, 0.880797], so both classes can be positive. label remains the top-scoring class for response compatibility. To select multiple labels, apply your chosen thresholds to the complete probs vector. Models with another or unspecified problem_type continue to use softmax.

Token classification

When every declared architecture ends in ForTokenClassification, the checkpoint is treated as a token classifier. If the served model returns one row of class scores per input token, /v1/classify returns one prediction per token:
  • probs has shape [num_tokens, num_classes], with one row per input token, including special tokens added by the tokenizer, and one column per label in id2label. Each row uses softmax, or an elementwise sigmoid for multi-label models as described above.
  • label lists the top-scoring class for each token. For multi-label models, apply your thresholds to each row of probs.
Config
Iris Content Guard (reflection/Iris-Content-Guard) uses the native BeamForTokenClassification head, which is available in the lmsysorg/sglang:project-iris Docker image and the iris branch until it reaches a release. Send one list of token IDs as input to /v1/classify. Each row in data[0].probs contains 28 independent probabilities, in the checkpoint’s id2label order. The first 14 score the request and the next 14 score the response. On B300 the default launch uses breakable prefill CUDA graphs. For full prefill graphs, add --cuda-graph-backend-prefill full --attention-backend fa4. Both return only real token rows, even when a graph pads the batch. The Transformers backend does not load token classification heads, and other token classification checkpoints need --is-embedding at launch. Without it, they are served as generation models and /v1/classify returns 400 Bad Request. In embedding mode, scores without one row per input token and one column per label return 500 Internal Server Error instead of a result for the whole input.

Example Usage

Using curl

Command

Using Python

Example

Supported Models

The classification API works with any classification model supported by SGLang, including:

Classification Models (Multi-class)

  • LlamaForSequenceClassification - Multi-class classification
  • Qwen2ForSequenceClassification - Multi-class classification
  • Qwen3ForSequenceClassification - Multi-class classification
  • BertForSequenceClassification - Multi-class classification
  • Gemma2ForSequenceClassification - Multi-class classification
Label Mapping: The API automatically uses the id2label mapping from the model’s config.json file to provide meaningful label names instead of generic class names. If id2label is not available, it falls back to LABEL_0, LABEL_1, etc., or Class_0, Class_1 as a last resort.

Token Classification Models

  • BeamForTokenClassification - Per-token classification, such as the multi-label reflection/Iris-Content-Guard

Reward Models (Single score)

  • InternLM2ForRewardModel - Single reward score
  • Qwen2ForRewardModel - Single reward score
  • LlamaForSequenceClassificationWithNormal_Weights - Special reward model
Note: The /classify endpoint in SGLang was originally designed for reward models but now supports all non-generative models. Our /v1/classify endpoint provides a standardized interface for classification tasks.

Error Handling

The API returns appropriate HTTP status codes and error messages:
  • 400 Bad Request: Invalid request format or missing required fields
  • 500 Internal Server Error: Server-side processing error, including model scores that do not match the model’s classification task or its id2label labels
Error response format:
Config

Implementation Details

The classification API is implemented using:
  1. Python HTTP Server: Implements the actual endpoint in python/sglang/srt/entrypoints/http_server.py
  2. Classification Service: Handles the classification logic in python/sglang/srt/entrypoints/openai/serving_classify.py

Testing

Use the provided test script to verify the implementation:
Command