Principle:Langgenius Dify Debug and Preview
| Knowledge Sources | |
|---|---|
| Domains | Real-Time Streaming Interactive Testing |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Interactive testing and validation of AI applications before deployment enables developers to observe real-time streaming responses with configurable parameters, iterating on prompts and settings without affecting production users.
Description
The debug and preview stage is where developers validate their application's behavior in a sandbox environment before publishing. This stage provides a live interaction interface that mirrors the end-user experience while exposing developer-facing controls for rapid iteration.
Core capabilities:
1. Server-Sent Events (SSE) Streaming
Dify uses SSE (Server-Sent Events) for real-time, token-by-token delivery of model responses. Instead of waiting for the complete response, the frontend receives incremental chunks as the model generates them. This provides:
- Low perceived latency -- Users see the first tokens within milliseconds of the model starting generation
- Progressive rendering -- The response builds up character by character, mimicking human typing
- Cancellation support -- The user can stop generation mid-stream by sending a stop request
2. Chat Mode Preview
For chat and advanced-chat applications, the debug panel provides a full conversation interface with:
- Persistent conversation context across multiple turns
- Conversation history retrieval for reviewing past messages
- Suggested question generation after each response
- Message replacement support for streaming corrections
3. Completion Mode Preview
For completion applications, the debug panel provides:
- Single-shot input/output testing
- "More Like This" alternative generation
- Variable injection through input forms
4. Conversation Management
The debug environment maintains conversation state, allowing developers to:
- Test multi-turn interactions with context retention
- Clear conversation history to start fresh
- Review token usage and response metadata
Usage
Use the debug and preview interface when:
- Testing prompt effectiveness with real model responses
- Validating conversation flow and context handling
- Comparing output quality across different model parameters
- Verifying feature toggles (citations, suggested questions, moderation) work correctly
- Debugging unexpected model behavior before deployment
Theoretical Basis
SSE streaming follows the Observer Pattern applied to HTTP connections. The server pushes events through a persistent connection, and the client reacts to each event as it arrives.
FUNCTION debug_chat(app_id, user_message, conversation_context):
connection = OPEN_SSE_CONNECTION(
endpoint = "/apps/{app_id}/completion-messages",
body = {
inputs: user_message,
response_mode: "streaming",
conversation_id: conversation_context.id
}
)
buffer = ""
FOR EACH event IN connection:
SWITCH event.type:
CASE "data":
buffer += event.content
RENDER_INCREMENTAL(buffer)
CALL onData(event.content)
CASE "message_replace":
buffer = event.content
RENDER_REPLACEMENT(buffer)
CALL onMessageReplace(event.content)
CASE "error":
DISPLAY_ERROR(event.message)
CALL onError(event)
CASE "done":
FINALIZE_RESPONSE(buffer)
CALL onCompleted(buffer)
RETURN buffer, conversation_context
Key design considerations:
- Idempotent conversation state -- Each debug session maintains its own conversation ID, isolated from other sessions and from production traffic.
- Graceful degradation -- If SSE streaming fails, the system can fall back to blocking mode (though this is not the default).
- Stop semantics -- Stopping a generation sends a separate POST request to halt the server-side generation process. Already-delivered tokens are preserved.