fix(v2): derive Iterable name from union members instead of "Union"#2406
Open
Sanjays2402 wants to merge 1 commit into
Open
fix(v2): derive Iterable name from union members instead of "Union"#2406Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
When building an Iterable response model for a union task type, the generated class name collapsed to `IterableUnion` for both `typing.Union[A, B]` and PEP 604 `A | B`. The naming logic looked up `getattr(subtask_class, "__name__", None)` first and only fell back to joining the member names when that returned None. But a union type's `__name__` is `"Union"` (not None) -- and on modern Python `A | B` (`types.UnionType`) normalizes to `typing.Union` as well -- so the member-joining branch was never reached and every union produced `IterableUnion`. Detect a union origin first and build the name from its members (`IterableAOrB`), falling back to `__name__`/`str()` only for non-union types. Adds regression tests for the pipe union, the typing.Union form, and a three-way union.
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 fixes
When building an
Iterableresponse model for a union task type, the generated class name collapses toIterableUnionfor bothtyping.Union[A, B]and PEP 604A | B, instead of a member-derived name likeIterableAOrB.Root cause
In
instructor/v2/dsl/iterable.py, the naming logic was:The comment's assumption is incorrect: a union type's
__name__is"Union", notNone. (And on modern Python,A | B/types.UnionTypenormalizes totyping.Union, whose__name__is also"Union".) Sogetattr(..., "__name__", None)returns"Union", theif task_name is Noneguard is never entered, the member-joining branch is skipped, and every union becomesIterableUnion.This affected both union spellings:
The fix
Detect a union origin first, and only fall back to
__name__/str()for non-union types:Result:
list[A | B]IterableUnionIterableAOrBList[Union[A, B]]IterableUnionIterableAOrBlist[A | B | C]IterableUnionIterableAOrBOrClist[User](non-union)IterableUserIterableUser(unchanged)Tests
The existing
test_list_of_model_pipe_union_generates_clean_namewas failing onmain(it assertsIterableAOrBbut gotIterableUnion) — this fix makes it pass. I also added two regression tests for thetyping.Unionform and a three-way union.(The two deselected tests instantiate
OpenAI()and require an API key — unrelated to this change; they fail identically onmain.)ruff checkpasses on both changed files.blackreports one pre-existing reformat elsewhere initerable.pythat is present onmaintoo and is left untouched.Type of change