Skip to content

can't receive streaming response using api to run a workflow #19465

Description

@terrysun1216

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.31

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

when runs a workflow through api, responses are always in blocking mode.
here are re requests:
# Input parameters
inputs = {
"query": "被解雇了怎么办",
"history_output": ""
}

# Request headers
headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

# Request payload
payload = {
    "inputs": inputs,
    "response_mode": "streaming",
    "user": "abc-123"
}

Log file path (in the same directory as this script)

log_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "query.log")

try:
    # Make the API request with streaming response
    with requests.post(api_url, headers=headers, json=payload, stream=True) as response:
        response.raise_for_status()  # Raise an exception for HTTP errors
        
        with open(log_file_path, "w", encoding="utf-8") as log_file:
            # Log request information
            log_file.write(f"REQUEST URL: {api_url}\n")
            log_file.write(f"REQUEST HEADERS: {json.dumps(headers, ensure_ascii=False)}\n")
            log_file.write(f"REQUEST PAYLOAD: {json.dumps(payload, ensure_ascii=False)}\n\n")
            log_file.write("RESPONSE:\n")
            
            # Process and log streaming response
            for line in response.iter_lines():
                if line:
                    try:
                        # Decode the line from bytes to string using UTF-8
                        decoded_line = line.decode('utf-8')
                        
                        # Parse the line if it's JSON data (for better display of Chinese characters)
                        if decoded_line.startswith('data: '):
                            try:
                                json_part = decoded_line[6:]  # Remove "data: " prefix
                                json_obj = json.loads(json_part)
                                # Use ensure_ascii=False to properly show Chinese characters
                                pretty_json = json.dumps(json_obj, ensure_ascii=False, indent=2)
                                formatted_line = f"data: {pretty_json}"
                            except json.JSONDecodeError:
                                formatted_line = decoded_line
                        else:
                            formatted_line = decoded_line
                            
                        # Write to log file
                        log_file.write(f"{formatted_line}\n")
                        log_file.flush()  # Ensure data is written immediately
                        
                        # Print to console (to correctly handle Chinese characters in terminal)
                        print(formatted_line)
                    except UnicodeDecodeError:
                        # If UTF-8 decoding fails, try with error handling
                        decoded_line = line.decode('utf-8', errors='replace')
                        log_file.write(f"[DECODE ERROR] {decoded_line}\n")
                        log_file.flush()
                        print(f"[DECODE ERROR] {decoded_line}")
                    
    print(f"API response has been logged to {log_file_path}")

✔️ Expected Behavior

should work like chat flow, message returns chunk by chunk. I have searched the issue history and find 2 similar questions on feb 12 and feb 13. but none of the 2 threads provide exact why and solutions.

❌ Actual Behavior

however, the response always in one block:
"outputs": {
"text": "\n\n\n\n<file_content>\n根据您提供的情况,您被解雇了,如果您认为该解雇行为属于违法解除劳动合同,您可以依据相关法律维护自己的合法权益。以下是对您情况的法律分析及建议:\n\n1. 被解雇是否违法? \n 如果您是因为参加工会活动或者工会工作人员因履行工会职责而被解除劳动合同,这属于法律明确禁止的行为。根据《中华人民共和国工会法》第五十二条、第五十三条的规定,劳动行政部门可以责令用人单位恢复您的工作,并补发被解除劳动合同期间应得的报酬,或者责令用人单位赔偿您本人年收入的二倍。\n\n - 相关法律依据: \n 《中华人民共和国工会法》第五十二条、第五十三条\n\n2. 如果解雇行为不属于上述情形,例如您因为违反公司规章制度而被解除劳动合同,则需要具体分析是否符合《劳动合同法》第三十九条、第四十条规定的合法解除条件。如果公司未按照法定程序解除劳动合同,您也可以主张赔偿。\n\n3. 建议采取以下步骤: \n - 第一步:收集证据 \n 收集与被解雇相关的所有材料,包括: \n - 劳动合同 \n - 解除劳动合同的通知或书面材料 \n - 工资条、银行流水(证明工资收入) \n - 工会活动相关记录(如参加工会会议、活动的照片、通知等) \n - 公司规章制度(如有) \n - 与公司管理人员的沟通记录(如微信、邮件等) \n\n - 第二步:向劳动行政部门投诉或申请劳动仲裁 \n 如果您认为公司违法解除劳动合同,可以向当地人力资源和社会保障局投诉,要求其依法处理。您也可以向劳动仲裁机构申请仲裁,要求恢复工作、补发工资或获得赔偿。\n\n - 第三步:法律诉讼(如仲裁未果) \n 若对仲裁结果不满意,您可以在法定期限内向人民法院提起诉讼。\n\n4. 风险提示 \n - 如果您未保留被解雇的相关证据,可能会影响维权效果。因此,建议尽快收集和保存相关材料。 \n - 若公司以“经济性裁员”或“优化人员结构”为由解除劳动合同,也需判断是否符合《劳动合同法》第四十一条的规定。\n\n<span style="color: red;">综上所述,如果您是因为工会活动或履行工会职责被解雇,属于法律明确禁止的行为,您有权要求恢复工作或获得赔偿。建议您尽快收集相关证据,并向劳动行政部门投诉或申请仲裁,以维护您的合法权益。\n</file_content>",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions