fix(prediction): output_iterator silently yields individual characters for string output#461
Open
devteamaegis wants to merge 1 commit into
Open
Conversation
…acter-by-character Both output_iterator and async_output_iterator used ``self.output or []`` to coerce the model's output to a list. This idiom is only safe when output is None (not started yet) or an actual list. For non-array model outputs (plain string, URL, dict) the falsy-or pattern silently returns the raw value. Calling ``yield from "hello world"`` then iterates over 11 individual characters instead of the intended single token, and ``output[len(previous_output):]`` on a string produces a string slice rather than a list slice — causing subtly wrong behaviour with no error. Fix: replace ``value or []`` with an explicit isinstance check that accepts None (treated as an empty list, meaning "no output yet") and list, and raises a descriptive ValueError for anything else so callers know to use ``prediction.output`` directly for non-streaming models. Also fixes the variable shadowing bug in async_output_iterator where the loop variable was named ``output`` instead of ``item``, clobbering the outer ``output`` reference on each iteration. Resolves the long-standing ``# TODO: check output is list`` comments.
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.
Summary
output_iterator()andasync_output_iterator()useself.output or []to coerce the model's output to a list. This is only safe whenoutputisNone(not started yet) or an actuallist.For non-array model outputs — plain strings, URLs, dicts — the falsy-or pattern silently returns the raw value unchanged:
Fix
Replace
value or []with an explicitisinstancecheck:This resolves the long-standing
# TODO: check output is listcomments in both methods.Bonus fix
The
async_output_iteratorloop also had a variable-shadowing bug:Tests
Six new unit tests added to
tests/test_prediction.pycovering:Noneoutput → empty iteration (no error)ValueError(sync and async)ValueError(sync)