-
Notifications
You must be signed in to change notification settings - Fork 115
fix(llm): handle subscription Responses streaming #1760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(llm): handle subscription Responses streaming #1760
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 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.