refactor: extract streaming chunk processor to eliminate code duplication#782
Conversation
β¦tion - Add _process_stream_delta() helper method to consolidate duplicate logic - Refactor 4 duplicated streaming sections (sync/async, verbose/non-verbose) - Eliminates ~50 lines of duplicated code - Maintains 100% backward compatibility with no API changes - Addresses original issue #754 about streaming code duplication Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
WalkthroughA new private method, Changes
Poem
π Recent review detailsConfiguration used: CodeRabbit UI π Files selected for processing (1)
π§° Additional context usedπ§ Learnings (2)π Common learningssrc/praisonai-agents/praisonaiagents/llm/llm.py (4)β° Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
π Additional comments (5)
β¨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Summary of Changes
Hello @MervinPraison, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly improves the maintainability and readability of the LLM streaming logic by centralizing a previously duplicated code block. By extracting the common streaming delta processing into a dedicated helper method, I've reduced code redundancy and made future modifications or debugging of this core functionality much simpler, without impacting any external interfaces.
Highlights
- Code Refactoring: I've extracted the common logic for processing streaming delta chunks into a new private helper method,
_process_stream_delta. This consolidates the code responsible for accumulating response text and parsing tool calls during streaming. - Duplication Elimination: This pull request addresses issue #754 by removing approximately 50 lines of duplicated code across four different streaming sections within the
get_responseandget_response_asyncmethods. These sections previously contained identical logic for handling stream deltas in both verbose and non-verbose modes, for both synchronous and asynchronous calls. - Backward Compatibility: The changes are purely internal refactoring and maintain 100% backward compatibility with no changes to the public API.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with π and π on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. β©
PR Reviewer Guide πHere are some key observations to aid the review process:
|
|
@claude review this again, making sure nothing is breaking, no features removed, everything exactly the same in regards to the usage and the response. |
|
Claude encountered an error ββ View job Claude Code is reviewing the PR... Todo List
|
PR Code Suggestions β¨No code suggestions found for the PR. |
|
|
||
| return any(endpoint in base_url or endpoint in api_base for endpoint in ollama_endpoints) | ||
|
|
||
| def _process_stream_delta(self, delta, response_text: str, tool_calls: List[Dict], formatted_tools: Optional[List] = None) -> tuple: |
There was a problem hiding this comment.
Consider adding specific type hints for the delta parameter and the return tuple in the _process_stream_delta method. Using delta: Any and Tuple[str, List[Dict]] can improve code clarity and enable static type checking.
| def _process_stream_delta(self, delta, response_text: str, tool_calls: List[Dict], formatted_tools: Optional[List] = None) -> tuple: | |
| def _process_stream_delta(self, delta: Any, response_text: str, tool_calls: List[Dict], formatted_tools: Optional[List] = None) -> Tuple[str, List[Dict]]: |
| if formatted_tools and self._supports_streaming_tools() and hasattr(delta, 'tool_calls') and delta.tool_calls: | ||
| for tc in delta.tool_calls: | ||
| if tc.index >= len(tool_calls): | ||
| tool_calls.append({ | ||
| "id": tc.id, | ||
| "type": "function", | ||
| "function": {"name": "", "arguments": ""} | ||
| }) | ||
| if tc.function.name: | ||
| tool_calls[tc.index]["function"]["name"] = tc.function.name | ||
| if tc.function.arguments: | ||
| tool_calls[tc.index]["function"]["arguments"] += tc.function.arguments |
There was a problem hiding this comment.
|
Claude encountered an error ββ View job I'll analyze this and get back to you. |
|
Claude finished @MervinPraison's task ββ View job Code Review Complete βI've thoroughly reviewed this refactoring PR and can confirm that nothing is breaking, no features are removed, and everything remains exactly the same in terms of usage and response. Review Summaryβ Refactoring is Safe and Correct
β No Behavioral Changes
β No Public API Changes
β Edge Cases Handled Correctly
β Other Streaming Code Unaffected
ConclusionThis is an excellent refactoring that successfully eliminates ~50 lines of duplicated code while maintaining 100% backward compatibility. The code is now more maintainable without any risk of breaking existing functionality. |

User description
Addresses issue #754 by extracting the duplicated streaming chunk processing logic into a single helper method.
Changes
_process_stream_delta()helper method to consolidate duplicate logicGenerated with Claude Code
PR Type
Enhancement
Description
Extract duplicated streaming chunk processing logic into helper method
Refactor 4 streaming sections (sync/async, verbose/non-verbose modes)
Eliminate ~50 lines of duplicated code
Maintain 100% backward compatibility
Changes diagram
Changes walkthrough π
llm.py
Extract streaming chunk processor helper methodΒ Β Β Β Β Β Β Β Β Βsrc/praisonai-agents/praisonaiagents/llm/llm.py
_process_stream_delta()helper method to consolidate duplicatelogic
calls
verbose/non-verbose modes
Summary by CodeRabbit