sgl-project/sglang · warning

Regular expression is not supported in the Anthropic backend

Error message

Regular expression is not supported in the Anthropic backend.

What it means

to_anthropic_kwargs warns that Anthropic's Messages API has no regex-guided decoding, so the constraint is dropped (along with frequency/presence penalties). Generation proceeds unconstrained.

Source

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

    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,
            "max_output_tokens": self.max_new_tokens,
            "stop_sequences": self.stop,
            "temperature": self.temperature,
            "top_p": self.top_p,
            "top_k": self.top_k if self.top_k > 0 else None,
        }

    def to_anthropic_kwargs(self):
        # Anthropic does not support frequency_penalty or presence_penalty, so we drop it here
        if self.regex is not None:
            warnings.warn(
                "Regular expression is not supported in the Anthropic backend."
            )
        return {
            "max_tokens": self.max_new_tokens,
            "stop_sequences": (
                self.stop if isinstance(self.stop, (list, tuple)) else [self.stop]
            ),
            "temperature": self.temperature,
            "top_p": self.top_p,
            "top_k": self.top_k,
        }

    def to_litellm_kwargs(self):
        if self.regex is not None:
            warnings.warn("Regular expression is not supported in the LiteLLM backend.")
        return {
            "max_tokens": self.max_new_tokens,
            "stop": self.stop or None,

View on GitHub (pinned to 0132848349)

Solutions

  1. Use Anthropic tool use / JSON mode for structured output instead of regex
  2. Route regex-dependent generations to the sglang runtime endpoint backend
  3. Parse and retry on the client when output does not match the pattern

Example fix

# before
g = sgl.gen("ans", regex=r"yes|no")
# after
out = backend.generate(...);  assert out in {"yes","no"} or retry
Defensive patterns

Strategy: validation

Validate before calling

if backend_name == "anthropic" and ir.regex is not None:
    ir.regex = None  # use tool-use / JSON mode

Prevention

When it happens

Trigger: Attaching .assert_regex to a generation executed via the Anthropic backend (generate/generate_stream).

Common situations: Multi-provider benchmarking of the same constrained program; relying on regex guarantees for tool-call output parsing with Claude models.

Related errors


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