{"record":{"id":"ca2edfe304b2e631","repo":"sgl-project/sglang","slug":"regular-expression-is-not-supported-in-the-openai","errorCode":null,"errorMessage":"Regular expression is not supported in the OpenAI backend.","messagePattern":"Regular expression is not supported in the OpenAI backend\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"python/sglang/lang/ir.py","lineNumber":67,"sourceCode":"            self.stop_regex,\n            self.temperature,\n            self.top_p,\n            self.top_k,\n            self.min_p,\n            self.frequency_penalty,\n            self.presence_penalty,\n            self.ignore_eos,\n            self.return_logprob,\n            self.logprob_start_len,\n            self.top_logprobs_num,\n            self.return_text_in_logprobs,\n            self.json_schema,\n        )\n\n    def to_openai_kwargs(self):\n        # OpenAI does not support top_k, so we drop it here\n        if self.regex is not None:\n            warnings.warn(\"Regular expression is not supported in the OpenAI backend.\")\n        return {\n            \"max_tokens\": self.max_new_tokens,\n            \"max_completion_tokens\": self.max_new_tokens,\n            \"n\": self.n,\n            \"stop\": self.stop or None,\n            \"temperature\": self.temperature,\n            \"top_p\": self.top_p,\n            \"frequency_penalty\": self.frequency_penalty,\n            \"presence_penalty\": self.presence_penalty,\n        }\n\n    def to_vertexai_kwargs(self):\n        if self.regex is not None:\n            warnings.warn(\n                \"Regular expression is not supported in the VertexAI backend.\"\n            )\n        return {\n            \"candidate_count\": 1,","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/lang/ir.py#L49-L85","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use OpenAI's structured outputs / json_schema / response_format instead of regex","Route to the sglang runtime endpoint backend, which enforces regex via constrained decoding","Add client-side re-sampling or validation when the output violates the expected pattern"],"exampleFix":"# before\ng = sgl.gen(\"ans\", regex=r\"[A-Z]{2}\\\\d{4}\")\n# after (openai)\ng = sgl.gen(\"ans\", json_schema=...)  # or validate output client-side","handlingStrategy":"validation","validationCode":"if backend_name == \"openai\" and ir.regex is not None:\n    raise ValueError(\"regex unsupported on OpenAI; use json_schema\")","typeGuard":"def supports_regex(backend) -> bool:\n    return backend not in {\"openai\", \"vertexai\", \"anthropic\", \"litellm\"}","tryCatchPattern":null,"preventionTips":["Capability matrix for backends in test suite","Client-side re-validation of outputs that relied on regex"],"tags":["regex","openai","structured-output","constraint-dropped","warning"],"backgroundTag":"unsupported-feature-warning","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}