Support PEP 604 unions (int | str) as output types#1932
Merged
Conversation
python_types_to_terms raised "Type int | str is currently not supported" for PEP 604 unions (int | str, str | None, list[int | str]) while the equivalent typing.Union/Optional forms worked, because is_union only checked get_origin(value) is typing.Union and get_origin of a types.UnionType returns types.UnionType. Make is_union also accept types.UnionType instances. No version guard is needed since requires-python is >=3.10 and types.UnionType exists since Python 3.10. get_args already handles both forms identically, so _handle_union needs no changes.
|
📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1932/ Preview updates automatically with each commit. |
RobinPicard
approved these changes
Jul 20, 2026
RobinPicard
left a comment
Contributor
There was a problem hiding this comment.
Good addition, thanks for opening a PR!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
Fixes
python_types_to_termsrejecting PEP 604 unions as output types. Passingint | str,str | None, or a nested form likelist[int | str]currently raises:while the semantically identical
typing.Union[int, str]/Optional[str]work fine.Root cause
is_unioninsrc/outlines/types/utils.pyonly checksget_origin(value) is typing.Union. For a PEP 604 union,get_origin(int | str)returnstypes.UnionTypeinstead, sois_unionreturnsFalseandpython_types_to_terms(src/outlines/types/dsl.py) falls through every handler into the generic unsupported-type error.Fix
Make
is_unionalso accepttypes.UnionTypeinstances:No version guard is needed:
pyproject.tomlrequires Python>=3.10andtypes.UnionTypeexists since 3.10.get_argsalready treats both union forms identically, so_handle_unionneeds no changes — PEP 604 unions now produce terms string-identical to theirtyping.Union/Optionalequivalents.Tests
tests/types/test_types_utils.py::test_is_union: addedis_union(int | str)andis_union(str | None)assertions.tests/types/test_dsl.py::test_dsl_python_types_to_terms: added assertions thatint | str,str | None, andlist[int | str]dispatch identically toUnion[int, str],Optional[str], andlist[Union[int, str]].Both tests fail on
mainwithout the one-line fix and pass with it. Fullpytest tests/types/passes: 232 passed.