Skip to content

Raise clear error for unsupported parametrized set types - #2026

Open
sneha4175 wants to merge 1 commit into
dottxt-ai:mainfrom
sneha4175:fix/dsl-parametrized-set-types
Open

sneha4175 wants to merge 1 commit into
dottxt-ai:mainfrom
sneha4175:fix/dsl-parametrized-set-types

Conversation

@sneha4175

Copy link
Copy Markdown

What this does

python_types_to_terms raises a misleading error for unsupported parametrized set-like generics.

from typing import Set, FrozenSet
from outlines.types.dsl import python_types_to_terms

python_types_to_terms(set[int])       # ValueError: Each argument must have a type annotation
python_types_to_terms(Set[int])       # ValueError: Each argument must have a type annotation
python_types_to_terms(FrozenSet[str]) # ValueError: Each argument must have a type annotation

The bare types already raise the intended, clear error:

python_types_to_terms(set)   # TypeError: Type set is currently not supported. Please open an issue: ...

Root cause

Parametrized generics like set[int] are callable() but are not a type (isinstance(set[int], type) is False), so is_callable(...) returns True. They fall into the callable branch, which calls get_schema_from_signature(...) and raises ValueError: Each argument must have a type annotation — an error that has nothing to do with the actual problem (an unsupported container type).

Fix

Gate the callable branch with get_origin(ptype) is None so parametrized generics skip it and reach the clear TypeError: Type ... is currently not supported error, matching the behavior of their bare counterparts.

Testing

Added a regression test next to the existing bytes assertion in test_dsl_python_types_to_terms, covering set[int], Set[int], FrozenSet[str] and frozenset[int]. It fails before the fix (with the misleading ValueError) and passes after.

  • pytest tests/types/ — 346 passed
  • ruff check --config=pyproject.toml (v0.9.1) — clean
  • mypy --allow-redefinition src/outlines/types/dsl.py — clean

Parametrized generics such as `set[int]`, `Set[int]` and
`FrozenSet[str]` are `callable()` but are not a function or class
signature. They therefore fell through to the callable branch in
`python_types_to_terms` and raised a misleading
`ValueError: Each argument must have a type annotation` instead of the
intended `TypeError: Type ... is currently not supported` that their
bare counterparts (`set`, `frozenset`) already raise.

Gate the callable branch with `get_origin(ptype) is None` so
parametrized generics skip it and reach the clear unsupported-type
error. Add a regression test alongside the existing `bytes` assertion.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant