Define "variable required inputs" in json schema with IF function?

Hello dear Forum !

I am quite new to n8n and impressed of its possibilities.

I have a question regarding a code-tool-node and hope you can help me.

I define a sample javascript code to either add 2 or 3 numbers, dependent on an input variable (Berechnungsart). This is just for test purpose and has no real sense.

What i want to achieve is, to define the json input schema in that way, that dependent on the choosen calculation type (add_2_numbers or add_3_numbers) the required input values adapt to the choosen calculation type.

In this example:

Berechnungsart = add_2_numbers : a,b must be required.
Berechnungsart = add_3_numbers, a,b,c must be required.

I try to define the schema in this way without success (using if);

{
  "type": "object",
  "properties": {
    "berechnungsart": {
      "type": "string",
      "enum": ["add_2_numbers", "add_3_numbers"],
      "description": "Definiert, ob zwei oder drei Zahlen addiert werden sollen."
    },
    "a": {
      "type": "number",
      "description": "Die erste Zahl, die addiert werden soll."
    },
    "b": {
      "type": "number",
      "description": "Die zweite Zahl, die addiert werden soll."
    },
    "c": {
      "type": "number",
      "description": "Die dritte Zahl, die addiert werden soll."
    }
  },
  "required": ["berechnungsart"],
  "allOf": [
    {
      "if": {
        "properties": { "berechnungsart": { "const": "add_2_numbers" } }
      },
      "then": {
        "required": ["a", "b"]
      }
    },
    {
      "if": {
        "properties": { "berechnungsart": { "const": "add_3_numbers" } }
      },
      "then": {
        "required": ["a", "b", "c"]
      }
    }
  ]
}

this gives me the error:

Error in sub-node ‘OpenAI Chat Model‘

Invalid schema for function ‘Addierer’: schema must be a JSON Schema of ‘type: “object”’, got ‘type: “None”’.
Open node

How can i make certain input variables “required” denpendend on other inputvariables with using the json schema definition ?

Information on your n8n setup

  • **n8n version:1.88.0
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): cloud
  • **Operating system: WIN 10

Hey!

Looks like these are not supported by OpenAI, the docs only mention anyOf https://platform.openai.com/docs/guides/structured-outputs#for-anyof-the-nested-schemas-must-each-be-a-valid-json-schema-per-this-subset

Maybe you can have two separate tools with each definition and instruct your Agent to call the correct tool depending on the input?

1 Like

Hey c6k3, thanks for your reply - I really appreciate it!

Yes - splitting into several tools is one solution. I was just trying to combine some “functions” into a single tool.

I am not a programmer, just a mechanical engineer, so I am doing more trial and error to get to my solution :wink:

What I realised - even though this is not a hard coded solution - if I describe the required variables in the description of the json schema - this more or less works.

1 Like