{"record":{"id":"48f56a17b19af2c9","repo":"sgl-project/sglang","slug":"ebnf-is-not-officially-supported-by-openai-endpoin","errorCode":null,"errorMessage":"EBNF is not officially supported by OpenAI endpoints. Ignoring.","messagePattern":"EBNF is not officially supported by OpenAI endpoints\\. Ignoring\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"python/sglang/lang/backend/openai.py","lineNumber":388,"sourceCode":"            assert hit\n\n            if np.sum(valid) <= 1:\n                break\n\n            prompt_tokens.append(ret_token)\n\n        return ChoicesDecision(\n            decision=choices[np.argmax(scores)],\n            meta_info={\"scores\": scores},\n        )\n\n\ndef openai_completion(\n    client, token_usage, is_chat=None, retries=3, prompt=None, **kwargs\n) -> Union[str, List[str]]:\n    # if \"ebnf\" is in kwargs, warn and remove\n    if \"ebnf\" in kwargs:\n        warnings.warn(\"EBNF is not officially supported by OpenAI endpoints. Ignoring.\")\n        del kwargs[\"ebnf\"]\n\n    for attempt in range(retries):\n        try:\n            if is_chat:\n                if \"stop\" in kwargs and kwargs[\"stop\"] is None:\n                    kwargs.pop(\"stop\")\n                ret = client.chat.completions.create(messages=prompt, **kwargs)\n                if len(ret.choices) == 1:\n                    comp = ret.choices[0].message.content\n                else:\n                    comp = [c.message.content for c in ret.choices]\n            else:\n                ret = client.completions.create(prompt=prompt, **kwargs)\n                if isinstance(prompt, (list, tuple)):\n                    comp = [c.text for c in ret.choices]\n                else:\n                    comp = ret.choices[0].text","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/lang/backend/openai.py#L370-L406","documentation":"This warning is emitted by sglang's OpenAI backend wrapper when the sampling/request kwargs contain an 'ebnf' key. OpenAI's completion/chat APIs have no EBNF grammar parameter, so sglang strips the option and proceeds without grammar-constrained decoding. It is informational: the request still runs, just unconstrained.","triggerScenarios":"Calling sgl_gen/generate through the OpenAI backend with an EBNF grammar set on the SGLang IR or sampling params (e.g. setting .assert_regex/grammar on a program routed to an OpenAI model, or passing ebnf=... kwargs that reach openai_completion).","commonSituations":"Writing a frontend-agnostic SGLang program with grammar constraints and then switching the backend to OpenAI (gpt-4o etc.); porting code from the local runtime endpoint to the hosted API; passing a dict of sampling params wholesale to the OpenAI client.","solutions":["Remove or clear the EBNF/grammar constraint before routing the request to the OpenAI backend (set regex/ebnf to None)","If grammar-constrained generation is required, route the request to an sglang runtime endpoint backend which supports EBNF natively","Suppress the warning with warnings.filterwarnings if the behavior is acceptable"],"exampleFix":"# before\ngen = sgl.gen(\"answer\", ebnf=grammar)\n# after (OpenAI backend)\ngen = sgl.gen(\"answer\")","handlingStrategy":"validation","validationCode":"kwargs.pop(\"ebnf\", None)\n# or before building the request:\nif backend_name == \"openai\" and grammar is not None:\n    grammar = None","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Backend-capability check before attaching constraints","Keep grammar constraints in backend-specific config layers"],"tags":["ebnf","openai","grammar-constraint","warning","backend"],"backgroundTag":"unsupported-feature-warning","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}