Skip to content

fix(io_models): guard against IndexError for bare iterator return annotations#5626

Open
devteamaegis wants to merge 1 commit into
bentoml:mainfrom
devteamaegis:fix/indexerror-bare-iterator
Open

fix(io_models): guard against IndexError for bare iterator return annotations#5626
devteamaegis wants to merge 1 commit into
bentoml:mainfrom
devteamaegis:fix/indexerror-bare-iterator

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

IODescriptor.from_output() raises IndexError: tuple index out of range whenever a service method is annotated with an unparameterized iterator type (t.Iterator, t.Generator, t.AsyncIterator, t.AsyncGenerator, or their collections.abc equivalents). Service startup crashes before any request is served.

Why it happens

is_iterator_type() correctly returns True for bare generics, but get_args() returns an empty tuple () for types with no type parameters. Line 435 unconditionally indexes position 0 without a bounds check. The surrounding try/except only catches ValueError and TypeError, so the IndexError escapes.

Fix

Added a one-line guard: args = get_args(return_annotation); return_annotation = args[0] if args else t.Any. When no type argument is present the descriptor defaults to t.Any, which is the same behaviour used when the return annotation is absent entirely.

Test

Added tests/unit/_bentoml_sdk/test_io_models.py::test_from_output_bare_iterator_does_not_raise which calls IODescriptor.from_output() with a -> t.Iterator annotated function and asserts it returns without raising.

Fixes #5625

…otations

get_args() returns an empty tuple for unparameterized generics like t.Iterator,
so indexing [0] unconditionally raised IndexError. Fall back to t.Any when no
type argument is present.

Fixes bentoml#5625
@devteamaegis devteamaegis requested a review from a team as a code owner June 1, 2026 19:21
@devteamaegis devteamaegis requested review from jianshen92 and removed request for a team June 1, 2026 19:21
@devteamaegis devteamaegis force-pushed the fix/indexerror-bare-iterator branch from 18deb82 to 808d8d2 Compare June 11, 2026 19:44
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.

BUG: IndexError in IODescriptor.from_output() with bare (unparameterized) iterator return annotations

2 participants