Literal[None] / Literal[..., None] crashes in _handle_literal while Union/Pydantic paths work
Author: michaelkillgtaCreated Sep 25, 2026Updated Sep 28, 2026
Describe the issue
python_types_to_terms(Literal[None]) and Literal[\"a\", \"b\", None] raise:
TypeError: Type None is currently not supported. Please open an issue: ...
Meanwhile:
Optional[Literal[\"a\", \"b\"]]works via_handle_union(peelsNone)- Pydantic
Literal[\"a\", None]via aBaseModelworks through JsonSchema (null)
So the DSL Literal path is inconsistent with Union and Pydantic.
Steps to reproduce
from typing import Literal, Optional
from outlines.types.dsl import to_regex, python_types_to_terms
from pydantic import BaseModel
print(to_regex(python_types_to_terms(Optional[Literal[\"a\", \"b\"]]))) # works
class M(BaseModel):
x: Literal[\"a\", None] # type: ignore
print(to_regex(python_types_to_terms(M))) # works → (\"a\"|null)
to_regex(python_types_to_terms(Literal[None]))
to_regex(python_types_to_terms(Literal[\"a\", \"b\", None]))
# TypeError: Type None is currently not supported.Expected behavior
Literal[..., None] should peel None like _handle_union, or emit JSON null consistently with the Pydantic/JsonSchema path.
Environment
outlines==1.3.3- Python 3.13
Additional context
_handle_union specially strips type(None) / None; _handle_literal does Alternatives([python_types_to_terms(arg) for arg in args]) with no None peel. Distinct from #1971 / #1942 (generation spelling / bool literals).
Source: dottxt-ai/outlines