[PR #13571] Non-Streaming Models Do Not Return Results Properly in _handle_invoke_result #27996

Closed
opened 2026-02-21 20:42:36 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/13571

State: closed
Merged: Yes


Fix: Non-Streaming Models Do Not Return Results Properly in _handle_invoke_result
Summary
This PR fixes an issue where models that do not support streaming (stream=False) fail to return results properly. The _handle_invoke_result function was not yielding any events when it received an LLMResult, leading to an empty generator. As a result, the for event in generator: loop in _run() never executed, causing missing responses for non-streaming models.

Issue:
Non-streaming models (stream=False) return an LLMResult, but _handle_invoke_result does not yield anything.
This causes _run() to receive an empty generator, resulting in no response being sent.
Streaming models (stream=True) work fine, as their results are handled iteratively.
Root Cause:
The _handle_invoke_result method contained this condition:

FIx https://github.com/langgenius/dify/issues/13569

if isinstance(invoke_result, LLMResult):
return # This exits without yielding any event
Since _handle_invoke_result did not yield an event for non-streaming models, the generator was empty.
_run() expects to iterate over generator, but since it was empty, the loop never executed.
Fix Implemented
The fix ensures _handle_invoke_result always yields a ModelInvokeCompletedEvent when stream=False, preventing an empty generator.

Updated _handle_invoke_result Implementation:

def _handle_invoke_result(self, invoke_result: LLMResult | Generator) -> Generator[NodeEvent, None, None]:

# Handle non-streaming models
if isinstance(invoke_result, LLMResult):
    yield ModelInvokeCompletedEvent(
        text=invoke_result.message.content,
        usage=invoke_result.usage,
        finish_reason=None,  # Adjust as needed
    )
    return  # Ensures at least one event is yielded

Impact of the Fix
Non-streaming models now return responses correctly.
Streaming models (stream=True) remain unaffected.
Prevents missing responses by ensuring _handle_invoke_result always yields an event.

Testing & Validation
Verified that non-streaming models (stream=False) now return valid responses.
Ensured that streaming models (stream=True) function as expected.
Confirmed that _handle_invoke_result always yields at least one event, preventing an empty generator.

**Original Pull Request:** https://github.com/langgenius/dify/pull/13571 **State:** closed **Merged:** Yes --- Fix: Non-Streaming Models Do Not Return Results Properly in _handle_invoke_result Summary This PR fixes an issue where models that do not support streaming (stream=False) fail to return results properly. The _handle_invoke_result function was not yielding any events when it received an LLMResult, leading to an empty generator. As a result, the for event in generator: loop in _run() never executed, causing missing responses for non-streaming models. Issue: Non-streaming models (stream=False) return an LLMResult, but _handle_invoke_result does not yield anything. This causes _run() to receive an empty generator, resulting in no response being sent. Streaming models (stream=True) work fine, as their results are handled iteratively. Root Cause: The _handle_invoke_result method contained this condition: FIx https://github.com/langgenius/dify/issues/13569 if isinstance(invoke_result, LLMResult): return # ❌ This exits without yielding any event Since _handle_invoke_result did not yield an event for non-streaming models, the generator was empty. _run() expects to iterate over generator, but since it was empty, the loop never executed. Fix Implemented The fix ensures _handle_invoke_result always yields a ModelInvokeCompletedEvent when stream=False, preventing an empty generator. Updated _handle_invoke_result Implementation: def _handle_invoke_result(self, invoke_result: LLMResult | Generator) -> Generator[NodeEvent, None, None]: # Handle non-streaming models if isinstance(invoke_result, LLMResult): yield ModelInvokeCompletedEvent( text=invoke_result.message.content, usage=invoke_result.usage, finish_reason=None, # Adjust as needed ) return # Ensures at least one event is yielded Impact of the Fix ✅ Non-streaming models now return responses correctly. ✅ Streaming models (stream=True) remain unaffected. ✅ Prevents missing responses by ensuring _handle_invoke_result always yields an event. Testing & Validation Verified that non-streaming models (stream=False) now return valid responses. Ensured that streaming models (stream=True) function as expected. Confirmed that _handle_invoke_result always yields at least one event, preventing an empty generator.
yindo added the pull-request label 2026-02-21 20:42:36 -05:00
yindo closed this issue 2026-02-21 20:42:36 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#27996