fix(core): Handle empty prompt in MockLLM.stream_complete #19521
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.
Fixes #19353
Description
This pull request addresses an issue where
MockLLM.stream_complete()raises apydantic.ValidationErrorwhen called with an empty prompt ("") andmax_tokens=None.The root cause was that the internal generator for the stream would complete without yielding any
CompletionResponseobjects. Thellm_completion_callbackdecorator would then attempt to build a final event with aNoneresponse, leading to the validation failure.This fix introduces a check at the beginning of the
gen_promptinner function. If the providedpromptis empty, it now yields a singleCompletionResponsewith emptytextanddeltaattributes. This ensures the generator always produces a valid stream, even for empty inputs, thus resolving the bug.New Package?
Version Bump?
Type of Change
How Has This Been Tested?
Note: A temporary, specific unit test was created to validate this fix, which passed successfully. The test was then removed to maintain a clean test suite, as its sole purpose was to confirm the resolution of this specific bug.
Suggested Checklist:
uv run make format; uv run make lintto appease the lint gods