sgl-project/sglang · warning

Regular expression is not supported in the OpenAI backend.

Error message

Regular expression is not supported in the OpenAI backend.

What it means

SGLang IR's to_openai_kwargs drops the regex constraint because OpenAI's completion and chat APIs have no regex-guided decoding. The request succeeds but the model is free to emit text that violates the regex; downstream parsing that assumed the constraint may then fail.

Source

Thrown at python/sglang/lang/ir.py:67

            self.stop_regex,
            self.temperature,
            self.top_p,
            self.top_k,
            self.min_p,
            self.frequency_penalty,
            self.presence_penalty,
            self.ignore_eos,
            self.return_logprob,
            self.logprob_start_len,
            self.top_logprobs_num,
            self.return_text_in_logprobs,
            self.json_schema,
        )

    def to_openai_kwargs(self):
        # OpenAI does not support top_k, so we drop it here
        if self.regex is not None:
            warnings.warn("Regular expression is not supported in the OpenAI backend.")
        return {
            "max_tokens": self.max_new_tokens,
            "max_completion_tokens": self.max_new_tokens,
            "n": self.n,
            "stop": self.stop or None,
            "temperature": self.temperature,
            "top_p": self.top_p,
            "frequency_penalty": self.frequency_penalty,
            "presence_penalty": self.presence_penalty,
        }

    def to_vertexai_kwargs(self):
        if self.regex is not None:
            warnings.warn(
                "Regular expression is not supported in the VertexAI backend."
            )
        return {
            "candidate_count": 1,

View on GitHub (pinned to 0132848349)

Solutions

  1. Use OpenAI's structured outputs / json_schema / response_format instead of regex
  2. Route to the sglang runtime endpoint backend, which enforces regex via constrained decoding
  3. Add client-side re-sampling or validation when the output violates the expected pattern

Example fix

# before
g = sgl.gen("ans", regex=r"[A-Z]{2}\\d{4}")
# after (openai)
g = sgl.gen("ans", json_schema=...)  # or validate output client-side
Defensive patterns

Strategy: validation

Validate before calling

if backend_name == "openai" and ir.regex is not None:
    raise ValueError("regex unsupported on OpenAI; use json_schema")

Type guard

def supports_regex(backend) -> bool:
    return backend not in {"openai", "vertexai", "anthropic", "litellm"}

Prevention

When it happens

Trigger: Attaching .assert_regex(...) (or regex=) to a generation and routing it through the OpenAI backend; also hit via _prepare_spec_execution when speculative/parallel programs run against OpenAI.

Common situations: Writing backend-agnostic structured-output programs and then evaluating them on OpenAI models; regex validation in the test harness failing because the constraint never reached the model.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/ca2edfe304b2e631. Report an issue: GitHub.