fix(llm): handle subscription Responses streaming#1760
fix(llm): handle subscription Responses streaming#1760enyst wants to merge 1 commit intoOpenHands:feat/openai-subscription-authfrom
Conversation
Subscription auth requires Responses streaming; LiteLLM returns a streaming iterator instead of a ResponsesAPIResponse. Drain the iterator and use the completed response, and log once when subscription mode is active.
all-hands-bot
left a comment
There was a problem hiding this comment.
This PR successfully implements subscription mode streaming support for the Responses API. However, there are several issues that should be addressed, particularly around error handling, type validation, and exception usage. See inline comments for details.
| assert isinstance(completed_resp, ResponsesAPIResponse), ( | ||
| f"Expected ResponsesAPIResponse, got {type(completed_resp)}" | ||
| ) |
There was a problem hiding this comment.
🔴 Critical: Using assert for runtime type validation is problematic. Assertions can be disabled with Python's -O (optimize) flag, which would skip this check in production and could lead to silent failures.
Replace with an explicit check and proper exception:
| assert isinstance(completed_resp, ResponsesAPIResponse), ( | |
| f"Expected ResponsesAPIResponse, got {type(completed_resp)}" | |
| ) | |
| if not isinstance(completed_resp, ResponsesAPIResponse): | |
| raise TypeError( | |
| f"Expected ResponsesAPIResponse, got {type(completed_resp)}" | |
| ) |
| if not isinstance(ret, SyncResponsesAPIStreamingIterator): | ||
| raise AssertionError( | ||
| f"Expected Responses stream iterator, got {type(ret)}" | ||
| ) |
There was a problem hiding this comment.
🟠 Important: Raising AssertionError for validation failures is unconventional. AssertionError should typically be reserved for programmer errors and invariants, not runtime type validation. Consider using TypeError or a custom exception like LLMResponseError instead.
| if not isinstance(ret, SyncResponsesAPIStreamingIterator): | |
| raise AssertionError( | |
| f"Expected Responses stream iterator, got {type(ret)}" | |
| ) | |
| if not isinstance(ret, SyncResponsesAPIStreamingIterator): | |
| raise TypeError( | |
| f"Expected Responses stream iterator, got {type(ret)}" | |
| ) |
| # Drain stream until a completed response is captured. | ||
| for _ in ret: | ||
| pass |
There was a problem hiding this comment.
🟠 Important: The stream draining loop has no timeout or error handling. If the stream:
- Hangs or yields indefinitely
- Raises an exception during iteration
- Takes an unusually long time
This could cause the entire request to hang or fail unexpectedly. Consider adding a timeout mechanism or wrapping in a try-except to provide better error messages if streaming fails.
| raise AssertionError( | ||
| f"Expected ResponsesAPIResponse, got {type(ret)}" | ||
| ) |
There was a problem hiding this comment.
🟠 Important: Same issue as above - using AssertionError for runtime validation. This should be a TypeError or similar exception.
| raise AssertionError( | |
| f"Expected ResponsesAPIResponse, got {type(ret)}" | |
| ) | |
| raise TypeError( | |
| f"Expected ResponsesAPIResponse, got {type(ret)}" | |
| ) |
| from litellm.responses.streaming_iterator import ( | ||
| SyncResponsesAPIStreamingIterator, | ||
| ) |
There was a problem hiding this comment.
🟡 Suggestion: This import is performed inside the execution path. If the import fails (e.g., due to litellm version mismatch or API changes), the error message might not be immediately clear to developers.
Consider either:
- Importing at module level if this is a stable API
- Wrapping in try-except with a more descriptive error:
try:
from litellm.responses.streaming_iterator import (
SyncResponsesAPIStreamingIterator,
)
except ImportError as e:
raise ImportError(
f"Failed to import SyncResponsesAPIStreamingIterator. "
f"This may indicate an incompatible litellm version: {e}"
) from e| "Streaming callbacks are not supported for Responses API yet" | ||
| ) | ||
|
|
||
| streaming_requested = bool(call_kwargs.get("stream", False) or self.stream) |
There was a problem hiding this comment.
🟢 Nit: Consider adding parentheses for clarity, since the or operation happens before the bool() conversion:
| streaming_requested = bool(call_kwargs.get("stream", False) or self.stream) | |
| streaming_requested = bool(call_kwargs.get("stream", False)) or self.stream |
Actually, self.stream is already a bool, so this could be simplified to:
streaming_requested = call_kwargs.get("stream", False) or self.stream
Follow-up to #1682.