[PR #23649] fix: sync missing conversation variables for existing conversations #30337

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

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

State: closed
Merged: Yes


Summary

This PR fixes an issue where existing conversations would encounter a "conversation variable not exists error" when a chatflow is republished with new conversation variables.

Problem

When users:

  1. Publish a chatflow
  2. Start conversations with it
  3. Add new conversation variables to the chatflow
  4. Republish the chatflow
  5. Try to continue existing conversations

They would face an error because the ne conversation variables don't exist in the database for the old conversations.

Solution

The fix ensures backward compatibility by:

  • Detecting missing variables: When loading conversation variables from the database, check if any new variables exist in the workflow that aren't in the database
  • Creating missing variables: Automatically create missing variables with their default values from the workflow definition
  • Maintaining existing values: Preserve all existing conversation variable values while adding the new ones

Implementation Details

Refactored Code Structure

The conversation variable initialization logic has been refactored into separate, focused methods for better readability and maintainability:

  • _initialize_conversation_variables(): Main orchestrator method
  • _load_existing_conversation_variables(): Loads variables from database
  • _create_all_conversation_variables(): Creates all variables for new conversations
  • _sync_missing_conversation_variables(): Syncs missing variables from workflow

Testing

Added comprehensive unit tests covering three scenarios:

  1. Missing variables are added when detected
  2. All variables are created when none exist
  3. No changes are made when all variables already exist

Changes

  • Modified api/core/app/apps/advanced_chat/app_runner.py to detect and sync missing conversation variables
  • Added api/tests/unit_tests/core/app/apps/advanced_chat/test_app_runner_conversation_variables.py with comprehensive test coverage

Fixes #19893
close https://github.com/langgenius/dify/issues/23432

**Original Pull Request:** https://github.com/langgenius/dify/pull/23649 **State:** closed **Merged:** Yes --- ## Summary This PR fixes an issue where existing conversations would encounter a "conversation variable not exists error" when a chatflow is republished with new conversation variables. ## Problem When users: 1. Publish a chatflow 2. Start conversations with it 3. Add new conversation variables to the chatflow 4. Republish the chatflow 5. Try to continue existing conversations They would face an error because the ne conversation variables don't exist in the database for the old conversations. ## Solution The fix ensures backward compatibility by: - **Detecting missing variables**: When loading conversation variables from the database, check if any new variables exist in the workflow that aren't in the database - **Creating missing variables**: Automatically create missing variables with their default values from the workflow definition - **Maintaining existing values**: Preserve all existing conversation variable values while adding the new ones ## Implementation Details ### Refactored Code Structure The conversation variable initialization logic has been refactored into separate, focused methods for better readability and maintainability: - `_initialize_conversation_variables()`: Main orchestrator method - `_load_existing_conversation_variables()`: Loads variables from database - `_create_all_conversation_variables()`: Creates all variables for new conversations - `_sync_missing_conversation_variables()`: Syncs missing variables from workflow ### Testing Added comprehensive unit tests covering three scenarios: 1. Missing variables are added when detected 2. All variables are created when none exist 3. No changes are made when all variables already exist ## Changes - Modified `api/core/app/apps/advanced_chat/app_runner.py` to detect and sync missing conversation variables - Added `api/tests/unit_tests/core/app/apps/advanced_chat/test_app_runner_conversation_variables.py` with comprehensive test coverage Fixes #19893 close https://github.com/langgenius/dify/issues/23432
yindo added the pull-request label 2026-02-21 20:47:18 -05:00
yindo closed this issue 2026-02-21 20:47:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30337