How to send null binary data if no file data is available?

In this flow, if I receive a media URL, I download the image from the URL and send it to the AI model as data.

If there is no media, the other branch kicks in. The data is non existent.

How do I tell the model to use data if it exists and ignore if it does not exist?

إعجابَين (2)

It is not possible. But there are 2 ways to go around it

If false branch has no data, AI node may fail. You need 2 different messages. It will look something like this

If you do not like duplicating AI nodes, you can use a code node to build messages dynamically.

Welcome @Yash_Ganthe to our community! I’m Jay and I am a n8n verified creator.

The code node approach is the cleaner path here. In the false branch, use a Code node to strip binary data before merging:

return $input.all().map(item => ({
  json: item.json,
  binary: {}
}));

Then merge both branches and pass the combined output into a single AI node - the model will just receive an empty binary object on the no-media path, which it handles fine without erroring out.

أجريت التغييرات كما هو موضح أعلاه. تم تكوين عقدة الدمج (Merge) لإضافة المدخلات (Append). لا توجد خيارات لاختيار أحد المدخلات إذا كان الآخر مفقوداً.

تعطي عقدة الدمج:

[ 
{
"text": "Test15", 
"media_url": "" 
}
]

تبدو القيم الثنائية فارغة.

تعطي عقدة LLM الخطأ التالي:

The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined

تم تكوين العقدة كما يلي:

المشكلة هي أن تمرير binary: {} يترك مفتاح binary على العنصر، وتحاول عقدة LLM قراءة حقل data منه - وهو غير معرّف. الحل هو إزالة مفتاح binary بالكامل عندما لا يكون هناك ملف:

return $input.all().map(item => {
  const result = { json: item.json };
  if (item.binary && Object.keys(item.binary).length > 0) {
    result.binary = item.binary;
  }
  return result;
});

بهذه الطريقة، العناصر التي لا تحتوي على ملف لن تحتوي على خاصية binary على الإطلاق، لذا لن تحاول عقدة LLM القراءة منها.

يعطي LLM الآن خطأً يقول:

This operation expects the node’s input data to contain a binary file ‘data’, but none was found [item 0]

{
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        -144
      ],
      "id": "a6ebddff-4394-489e-8160-abb0752ffa2f",
      "name": "عند النقر على "تنفيذ سير العمل""
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "6e738c76-bc7c-4390-9297-d0457d04865c",
              "name": "media_link",
              "value": "",
              "type": "string"
            },
            {
              "id": "48967218-28a4-42e4-af42-8f4bc17156ef",
              "name": "text",
              "value": "",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        -144
      ],
      "id": "f74adb71-9667-4ccf-8f18-c3561130fc5c",
      "name": "تحرير الحقول"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "1c910c16-090b-42d2-8b9f-0394f4941b45",
              "leftValue": "={{ $json.media_link }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        448,
        -224
      ],
      "id": "19307e16-3604-4d28-91f1-7763e4db2d50",
      "name": "إذا"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        896,
        -144
      ],
      "id": "71f4761d-28df-4e33-aa30-96aee80afda5",
      "name": "دمج"
    },
    {
      "parameters": {
        "url": "={{ $json.media_link }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        672,
        -272
      ],
      "id": "1f4bbc9d-fda2-4f29-bda2-8d8a9d914659",
      "name": "طلب HTTP"
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {
          "includeBinaries": true
        }
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1120,
        -144
      ],
      "id": "f50de3b8-9fd8-432b-84ca-e0f86b0a6f5f",
      "name": "تجميع"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('Edit Fields').item.json.text }}",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1344,
        -144
      ],
      "id": "1857f2fb-a6a5-41d9-aa37-1fcdb1ca8128",
      "name": "وكيل ذكي"
    }
  ],
  "connections": {
    "عند النقر على \"تنفيذ سير العمل\"": {
      "main": [
        [
          {
            "node": "تحرير الحقول",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "تحرير الحقول": {
      "main": [
        [
          {
            "node": "إذا",
            "type": "main",
            "index": 0
          },
          {
            "node": "دمج",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "إذا": {
      "main": [
        [
          {
            "node": "طلب HTTP",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "دمج",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "دمج": {
      "main": [
        [
          {
            "node": "تجميع",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "طلب HTTP": {
      "main": [
        [
          {
            "node": "دمج",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "تجميع": {
      "main": [
        [
          {
            "node": "وكيل ذكي",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "instanceId": "735886904af210643f438394a538e64374f0cb4ab13fd94d97005987482d652a"
  }
}

@Yash_Ganthe جرب سير العمل هذا، إنه يعمل بنسبة 100%.

لا يزال نفس الخطأ. لاحظ أن البيانات يجب أن تكون ثنائية. وليست مصفوفة.

تتوقع هذه العملية أن تحتوي بيانات إدخال العقدة على ملف ثنائي ‘data’، لكن لم يتم العثور على أي منها [item 0]

@Yash_Ganthe الخطأ يأتي من أن العقدة لا تزال تتوقع خاصية ثنائية تسمى data على العنصر، حتى عندما تكون على الفرع الخاطئ. أضف عقدة Code على المسار الخاطئ (قبل أن تصل إلى نموذج الذكاء الاصطناعي) مع هذا:

for (const item of $input.all()) {
  item.binary = {};
}
return $input.all();

هذا يعيّن بشكل صريح binary إلى كائن فارغ، مما يخبر العقدة التالية بأنه لا توجد بيانات ثنائية متاحة بدلاً من تركها غير معرّفة. الفرق هو: undefined يحفز خطأ «لم يتم العثور على binary»، بينما {} الفارغ يمر بنعومة.

The binary is expected to have a data property which is expected to be empty in the False branch.

item.binary = { data:{} };

Added this. Still the same error.

The item has no binary field ‘data’ [item 0]

Check that the parameter where you specified the input binary field name is correct, and that it matches a field in the binary input.

I get this error when I make the binary empty.

All I need is a way of making the input to the model go as empty file if there is no image sent in the input.

يرجى استخدام JSON أدناه، سيعمل بشكل صحيح. ملاحظة سريعة أنني مُنشئ n8n وقد بنيت العديد من سير العمل مثل هذا بالفعل، لذا يمكنك تطبيق هذا النهج بأمان.

تحتاج إلى استخدام AI Agent مع عقدة OpenAI Chat Model لكي تعمل هذه الميزة بشكل صحيح (إرسال الملف عند توفره، واعتباره كلا ملف عند فقده). لقد اختبرت هذا الإعداد وعمل بشكل تام في سير عملي.

The Aggregate node removes the binary data that has come from the Download node. Due to this, the Agent node does not receive the binary data.

If I remove the Aggregate node and connect the Merge to the Agent, the JSON data reaches the Agent twice as 2 items. Is there a way to retain the binary data?

النسخة المختصرة: لا تحاول جعل عقدة واحدة تأخذ الصورة “بشكل اختياري” — هذا هو الجزء الذي يسبب لك المشاكل. بما أن لديك بالفعل فرعين، احتفظ بهما منفصلين طوال الطريق حتى الموديل:

• إذا كنت تستخدم عقدة ذكاء اصطناعي/موديل مدمجة: ضع عقدة موديل على كل فرع — فرع الوسائط مكوّن مع إدخال الصورة الثنائية، والفرع بدون وسائط يقبل النصوص فقط — وادمج بعد الموديل إذا احتجت لإعادة دمج النتائج.
• إذا كنت تستدعي الموديل عبر عقدة HTTP Request: ابنِ جسم الطلب لكل فرع — فرع الوسائط يتضمن جزء الصورة (base64)، والفرع بدون وسائط يرسل جسم نصي فقط. (يمكنك فعل ذلك في عقدة واحدة باستخدام تعبير يضيف كتلة الصورة فقط عندما يكون الملف الثنائي موجوداً، لكن عقدتين أسهل في القراءة.)
الشيء الذي يجب تجنبه هو دمج الفرعين قبل الموديل — سيرمي خطأ على عناصر الملف الثنائي الفارغة، وMerge يميل إلى حذف الملف الثنائي على أي حال. أهلاً وسهلاً بك في المجتمع :waving_hand:

@Yash_Ganthe آسف، نسيت تفعيل وضع Include Binary، لقد أصلحته الآن.

يمكنك التحقق من ملف JSON الخاص بي مجددًا. يسعدني مساعدتك! أتمنى أن يكون هذا هو الحل لمشكلتك.

I did it in a simpler way. The image received from Whatsapp is successfully uploaded to Google drive. However, the Agent is not able to access it. I have kept the drive images in a public folder and have tried accessing them successfully from other browsers.