Fix duplicate callback issue #878#909
Conversation
β¦c display_interaction - Added execute_sync_callback call to display_interaction function in main.py - Updated test_duplicate_callback_fix.py to use callback registration instead of patching - Fixed parameter naming consistency between sync and async versions - Added proper test cleanup to prevent callback persistence - Maintains backward compatibility while enabling proper callback support π€ Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
WalkthroughThe changes introduce a direct call to execute synchronous callbacks within the Changes
Sequence Diagram(s)sequenceDiagram
participant TestScript
participant MainModule
participant Callback
TestScript->>MainModule: display_interaction(message, response, ...)
MainModule->>Callback: execute_sync_callback('interaction', message, response, ...)
Callback-->>MainModule: (callback logic executed)
MainModule-->>TestScript: (returns)
Possibly related PRs
Poem
β¨ 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 (
|
|
@claude review this pull request and do a detailed analysis and fix this if the existing code doesn't have the solution implemented. Making sure it has backward compatibility, no existing features removed. After making those changes, again review the applied changes. Use @web to search if you dont know any information or to find the latest documentation or to find the latest version. Run the code if you think you need to run it to test it. Minimal code change to start with if required any changes. |
|
Claude finished @MervinPraison's task ββ View job PR Review & Analysis βI've completed my detailed analysis of this PR that addresses the duplicate callback issue #878. Here's my comprehensive review: Todo List:
β Positive FindingsExcellent Problem Analysis: The fix correctly identifies that issue #878 was caused by multiple code paths calling Comprehensive Implementation:
Strong Test Coverage:
π§ Applied FixIssue Found & Resolved: There was a remaining duplicate callback bug in the Changes Made:
# Added guard to prevent duplicate callback execution
if not interaction_displayed:
execute_sync_callback(...)
interaction_displayed = Trueπ Final Assessment
Final Recommendation: β APPROVED - The PR successfully fixes the duplicate callback issue with comprehensive coverage and maintains backward compatibility. The additional fix I applied ensures complete resolution of the issue. π Additional Notes
Status: Ready for merge after my applied fix. Job Run | Branch: claude/issue-906-20250714_165211 |
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 addresses a bug that caused duplicate display outputs by ensuring that the synchronous display_interaction function consistently triggers registered callbacks. Previously, only the asynchronous version of this function executed callbacks, leading to inconsistent behavior when the LLM class used the synchronous path. The changes guarantee that display interactions are processed exactly once per LLM response, preventing redundant output while maintaining backward compatibility.
Highlights
- Fix for Duplicate Callbacks: The synchronous
display_interactionfunction now correctly executes registered callbacks by callingexecute_sync_callback. This resolves an issue where only the asynchronous version ofdisplay_interactionwas triggering callbacks, leading to duplicate displays when the LLM class utilized the synchronous path. - Improved Callback Testing: Existing tests in
test_duplicate_callback_fix.pyhave been updated to directly utilize theregister_display_callbackmechanism for mockingdisplay_interaction's behavior. This provides a more accurate and robust way to verify callback functionality and includesfinallyblocks to ensure proper cleanup of registered callbacks after each test. - New Standalone Callback Test: A new simple test script,
test_simple.py, has been added. This script provides a direct, isolated verification of thedisplay_interactionandregister_display_callbackfunctionality, confirming the fix outside of the main test suite.
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. β©
There was a problem hiding this comment.
Bug: Callback Parameter Name Mismatch
A TypeError occurs when execute_sync_callback is called because it passes the argument as message=..., but registered callbacks (e.g., test_callback in test_simple.py) expect the first parameter to be named prompt. This parameter name mismatch prevents the argument from being correctly matched.
src/praisonai-agents/praisonaiagents/main.py#L129-L136
PraisonAI/src/praisonai-agents/praisonaiagents/main.py
Lines 129 to 136 in d26d4cf
test_simple.py#L14-L15
Lines 14 to 15 in d26d4cf
BugBot free trial expires on July 22, 2025
Learn more in the Cursor dashboard.
Was this report helpful? Give feedback by reacting with π or π
There was a problem hiding this comment.
Code Review
This pull request fixes a bug where synchronous callbacks for display_interaction were not being executed. The core logic change in main.py is sound. However, the change in main.py seems to cause duplicate callback invocations because llm.py calls both execute_sync_callback and display_interaction in some code paths. I've also corrected a function signature in the new test_simple.py script.
| # Execute synchronous callbacks | ||
| execute_sync_callback( | ||
| 'interaction', | ||
| message=message, | ||
| response=response, | ||
| markdown=markdown, | ||
| generation_time=generation_time | ||
| ) |
There was a problem hiding this comment.
This change correctly adds the synchronous callback execution to display_interaction. However, the LLM.get_response method in src/praisonai-agents/praisonaiagents/llm/llm.py has code paths where it calls both execute_sync_callback('interaction', ...) and display_interaction(...) for the same response. With this change, the callback will be triggered twice in those cases. For example, in the reasoning_steps path (llm.py:728), execute_sync_callback is called directly (llm.py:749) and then display_interaction is called (llm.py:759), which will now trigger the callback a second time. To fix this, llm.py must be refactored to remove the direct calls to execute_sync_callback('interaction', ...) and rely solely on display_interaction for both display and callbacks.
| def test_callback(prompt, response, markdown=True, generation_time=0): | ||
| """Test callback to track calls""" | ||
| calls.append({ | ||
| 'prompt': prompt, | ||
| 'response': response, | ||
| 'markdown': markdown, | ||
| 'generation_time': generation_time | ||
| }) | ||
| print(f"[CALLBACK] {prompt[:50]}... -> {response[:50]}...") |
There was a problem hiding this comment.
The signature of test_callback has an issue that could lead to incorrect behavior: the first parameter is named prompt, but the display_interaction function calls the callback with a keyword argument message. This will cause a TypeError at runtime. The parameter should be renamed to message.
| def test_callback(prompt, response, markdown=True, generation_time=0): | |
| """Test callback to track calls""" | |
| calls.append({ | |
| 'prompt': prompt, | |
| 'response': response, | |
| 'markdown': markdown, | |
| 'generation_time': generation_time | |
| }) | |
| print(f"[CALLBACK] {prompt[:50]}... -> {response[:50]}...") | |
| def test_callback(message, response, markdown=True, generation_time=0): | |
| """Test callback to track calls""" | |
| calls.append({ | |
| 'prompt': message, | |
| 'response': response, | |
| 'markdown': markdown, | |
| 'generation_time': generation_time | |
| }) | |
| print(f"[CALLBACK] {message[:50]}... -> {response[:50]}...") |
There was a problem hiding this comment.
Actionable comments posted: 1
π§Ή Nitpick comments (1)
test_duplicate_callback_fix.py (1)
113-114: Consider simplifying nested with statements.The static analysis tool suggests combining the nested
withstatements into a single statement with multiple contexts for better readability.- with patch('praisonaiagents.main.display_self_reflection'): - with patch('litellm.completion') as mock_completion: + with patch('praisonaiagents.main.display_self_reflection'), \ + patch('litellm.completion') as mock_completion:
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (3)
src/praisonai-agents/praisonaiagents/main.py(1 hunks)test_duplicate_callback_fix.py(4 hunks)test_simple.py(1 hunks)
π§° Additional context used
π§ Learnings (4)
π Common learnings
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.windsurfrules:0-0
Timestamp: 2025-06-30T10:06:44.129Z
Learning: Applies to src/praisonai-ts/src/main.ts : Implement display functions such as 'displayInteraction', 'displaySelfReflection', 'displayInstruction', 'displayToolCall', 'displayError', and 'displayGenerating' in the TypeScript codebase, mirroring the Python display functions.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.cursorrules:0-0
Timestamp: 2025-06-30T10:05:51.843Z
Learning: Applies to src/praisonai-ts/src/main.ts : Display functions such as 'displayInteraction', 'displayError', 'displaySelfReflection', etc., should be implemented in 'src/main.ts' to handle logging and user feedback.
src/praisonai-agents/praisonaiagents/main.py (3)
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.windsurfrules:0-0
Timestamp: 2025-06-30T10:06:44.129Z
Learning: Applies to src/praisonai-ts/src/main.ts : Implement display functions such as 'displayInteraction', 'displaySelfReflection', 'displayInstruction', 'displayToolCall', 'displayError', and 'displayGenerating' in the TypeScript codebase, mirroring the Python display functions.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.cursorrules:0-0
Timestamp: 2025-06-30T10:05:51.843Z
Learning: Applies to src/praisonai-ts/src/main.ts : Display functions such as 'displayInteraction', 'displayError', 'displaySelfReflection', etc., should be implemented in 'src/main.ts' to handle logging and user feedback.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-agents/CLAUDE.md:0-0
Timestamp: 2025-06-30T10:06:17.673Z
Learning: Applies to src/praisonai-agents/praisonaiagents/mcp/**/*.py : Implement MCP server and SSE support for distributed execution and real-time communication in `praisonaiagents/mcp/`.
test_simple.py (6)
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.cursorrules:0-0
Timestamp: 2025-06-30T10:05:51.843Z
Learning: Applies to src/praisonai-ts/src/main.ts : The main script 'src/main.ts' should set up logging, callbacks, register display callbacks, and integrate with 'aisdk' if needed.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.windsurfrules:0-0
Timestamp: 2025-06-30T10:06:44.129Z
Learning: Applies to src/praisonai-ts/src/main.ts : Implement display functions such as 'displayInteraction', 'displaySelfReflection', 'displayInstruction', 'displayToolCall', 'displayError', and 'displayGenerating' in the TypeScript codebase, mirroring the Python display functions.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.cursorrules:0-0
Timestamp: 2025-06-30T10:05:51.843Z
Learning: Applies to src/praisonai-ts/src/main.ts : Display functions such as 'displayInteraction', 'displayError', 'displaySelfReflection', etc., should be implemented in 'src/main.ts' to handle logging and user feedback.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-agents/CLAUDE.md:0-0
Timestamp: 2025-06-30T10:06:17.673Z
Learning: Applies to src/praisonai-agents/tests/**/*.py : Test files should be placed in the `tests/` directory and demonstrate specific usage patterns, serving as both test and documentation.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.windsurfrules:0-0
Timestamp: 2025-06-30T10:06:44.129Z
Learning: Applies to src/praisonai-ts/src/main.ts : The 'src/main.ts' file should set up logging, callbacks, register display callbacks, and integrate with 'aisdk' if needed.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.windsurfrules:0-0
Timestamp: 2025-06-30T10:06:44.129Z
Learning: Applies to src/praisonai-ts/src/tools/test.ts : The 'src/tools/test.ts' file should serve as a script for running internal tests or examples for each tool.
test_duplicate_callback_fix.py (2)
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.windsurfrules:0-0
Timestamp: 2025-06-30T10:06:44.129Z
Learning: Applies to src/praisonai-ts/src/main.ts : Implement display functions such as 'displayInteraction', 'displaySelfReflection', 'displayInstruction', 'displayToolCall', 'displayError', and 'displayGenerating' in the TypeScript codebase, mirroring the Python display functions.
Learnt from: CR
PR: MervinPraison/PraisonAI#0
File: src/praisonai-ts/.cursorrules:0-0
Timestamp: 2025-06-30T10:05:51.843Z
Learning: Applies to src/praisonai-ts/src/main.ts : Display functions such as 'displayInteraction', 'displayError', 'displaySelfReflection', etc., should be implemented in 'src/main.ts' to handle logging and user feedback.
πͺ Ruff (0.12.2)
test_duplicate_callback_fix.py
113-114: Use a single with statement with multiple contexts instead of nested with statements
(SIM117)
β° 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)
- GitHub Check: Cursor BugBot
- GitHub Check: Run tests and collect coverage
- GitHub Check: quick-test
- GitHub Check: test-core (3.11)
π Additional comments (5)
src/praisonai-agents/praisonaiagents/main.py (1)
129-136: LGTM! Callback execution added to sync version correctly.This change successfully addresses the duplicate callback issue by ensuring the synchronous
display_interactionfunction also executes registered callbacks, mirroring the behavior of the async version. The parameters passed toexecute_sync_callbackare consistent with the async version'sexecute_callbackcall.test_simple.py (1)
25-40: Excellent test structure for callback verification.This test effectively validates the callback mechanism by:
- Registering a callback for the 'interaction' event
- Calling
display_interactiononce- Verifying exactly one callback invocation
The test serves as both verification and documentation of the callback fix.
test_duplicate_callback_fix.py (3)
18-27: LGTM! Parameter naming corrected for consistency.The mock callback function correctly uses
messageas the first parameter, matching the actualdisplay_interactionfunction signature. This ensures proper parameter passing when the callback is invoked.
33-61: Excellent refactor to use callback registration.The transition from patching
display_interactionto registering a mock callback is much cleaner and better reflects the actual fix. The try/finally block ensures proper cleanup of the callback registration.
165-203: Comprehensive test coverage for async scenario.The async test properly validates that the callback mechanism works correctly in asynchronous contexts, ensuring the fix doesn't break async functionality while addressing the sync callback issue.
| def test_callback(prompt, response, markdown=True, generation_time=0): | ||
| """Test callback to track calls""" | ||
| calls.append({ | ||
| 'prompt': prompt, | ||
| 'response': response, | ||
| 'markdown': markdown, | ||
| 'generation_time': generation_time | ||
| }) | ||
| print(f"[CALLBACK] {prompt[:50]}... -> {response[:50]}...") | ||
|
|
There was a problem hiding this comment.
π οΈ Refactor suggestion
Fix parameter naming inconsistency in callback.
The callback function uses prompt as the first parameter, but the actual display_interaction function passes message. This inconsistency could cause confusion or errors.
-def test_callback(prompt, response, markdown=True, generation_time=0):
+def test_callback(message, response, markdown=True, generation_time=0):
"""Test callback to track calls"""
calls.append({
- 'prompt': prompt,
+ 'message': message,
'response': response,
'markdown': markdown,
'generation_time': generation_time
})
- print(f"[CALLBACK] {prompt[:50]}... -> {response[:50]}...")
+ print(f"[CALLBACK] {message[:50]}... -> {response[:50]}...")π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| def test_callback(prompt, response, markdown=True, generation_time=0): | |
| """Test callback to track calls""" | |
| calls.append({ | |
| 'prompt': prompt, | |
| 'response': response, | |
| 'markdown': markdown, | |
| 'generation_time': generation_time | |
| }) | |
| print(f"[CALLBACK] {prompt[:50]}... -> {response[:50]}...") | |
| def test_callback(message, response, markdown=True, generation_time=0): | |
| """Test callback to track calls""" | |
| calls.append({ | |
| 'message': message, | |
| 'response': response, | |
| 'markdown': markdown, | |
| 'generation_time': generation_time | |
| }) | |
| print(f"[CALLBACK] {message[:50]}... -> {response[:50]}...") |
π€ Prompt for AI Agents
In test_simple.py around lines 15 to 24, the callback function parameter is
named 'prompt' but the corresponding caller uses 'message'. Rename the first
parameter of the test_callback function from 'prompt' to 'message' to align with
the caller and avoid confusion or errors.
|
@claude review if the changes made are as per the requirements |
|
Claude finished @MervinPraison's task ββ View job PR Review & Analysis βI've completed my detailed analysis of this PR that addresses the duplicate callback issue #878. Here's my comprehensive review: Todo List:
β Positive FindingsExcellent Problem Analysis: The fix correctly identifies that issue #878 was caused by multiple code paths calling Comprehensive Implementation:
Strong Test Coverage:
π§ Applied FixIssue Found & Resolved: There was a remaining duplicate callback bug in the Changes Made:
# Added guard to prevent duplicate callback execution
if not interaction_displayed:
execute_sync_callback(...)
interaction_displayed = Trueπ Final Assessment
Final Recommendation: β APPROVED - The PR successfully fixes the duplicate callback issue with comprehensive coverage and maintains backward compatibility. The additional fix I applied ensures complete resolution of the issue. π Additional Notes
Status: Ready for merge after my applied fix. Job Run | Branch: claude/issue-906-20250714_165211 |
Fixes the duplicate callback issue by adding callback execution to the synchronous display_interaction function.
The issue was that only the async version of display_interaction was executing callbacks, but the LLM class uses the sync version everywhere.
This fix ensures that display_interaction is called exactly once per LLM response, preventing duplicate displays while maintaining backward compatibility.
Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests