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 Likes

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.

Made the changes as shown above. The Merge is configured to Append the Inputs. There is no option to choose one input if the other is missing.

The Merge node gives out:

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

The binary values appears empty.

The LLM node gives the error as:

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

The node is configured as:

The issue is that passing binary: {} still leaves the binary key on the item, and the LLM node tries to read the data field from it - which is undefined. The fix is to remove the binary key entirely when there’s no file:

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;
});

This way, items without a file won’t have a binary property at all, so the LLM node won’t attempt to read from it.

The LLM now gives an error saying:

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

@Yash_Ganthe Let try with this workflow, It work 100%.

Still the same error. Note that data is expected to be binary. Not an array.

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

@Yash_Ganthe the error is coming from the node still expecting a binary property called data on the item, even when you’re on the false branch. Add a Code node on the false path (before it hits the AI model) with this:

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

This explicitly sets binary to an empty object, which tells the downstream node there is no binary data available rather than leaving it undefined. The difference is: undefined triggers the “binary not found” error, while an empty {} passes through cleanly.

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.

Please use the JSON below, it will work correctly. Just a quick note that I am an n8n Creator and have built many workflows like this already, so you can safely apply this approach.

You need to use an AI Agent together with the OpenAI Chat Model node for this feature to behave correctly (send file when available, and treat it as no file when it is missing). I have tested this setup and it works completely fine in my workflow.

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?

Short version: don’t try to make one node “optionally” take the image — that’s the bit fighting you. Since you’ve already got the two branches, keep them separate all the way to the model:

• If you’re using a built-in AI/model node: put a model node on each branch — the media branch configured with the binary image input, the no-media branch text-only — and Merge after the model if you need to recombine.
• If you’re calling the model via the HTTP Request node: build the request body per branch — the media branch includes the image part (base64), the no-media branch sends a text-only body. (You can do it in one node with an expression that only adds the image block when binary exists, but two nodes is easier to read.)
The thing to avoid is merging the two branches before the model — it’ll throw on the empty-binary items, and Merge tends to drop the binary anyway. Welcome to the community :waving_hand:

@Yash_Ganthe Sorry I forget to turn on Include Binary mode, I have fixed it.

You can check again my JSON. Glad to help you! Hope it is the solution for your issue.