Basic LLM Chain - Model output doesn't fit required format

Workflow

  • Note the text comes from the webhook (not included)

Share the output returned by the last node

LLM Basic Chain Output:

{
  "response": {
    "generations": [
      [
        {
          "text": "{\n  \"output\": {\n    \"nsfw\": true,\n    \"jailbreak\": false,\n    \"help\": false,\n    \"substantive\": true\n  }\n}"
        }
      ]
    ]
  },
  "tokenUsageEstimate": {
    "completionTokens": 38,
    "promptTokens": 897,
    "totalTokens": 935
  }
}

Structured Output Parser Node

Input

{
  "action": "parse",
  "text": "[{\"type\":\"text\",\"text\":\"{\\n  \\\"output\\\": {\\n    \\\"nsfw\\\": true,\\n    \\\"jailbreak\\\": false,\\n    \\\"help\\\": false,\\n    \\\"substantive\\\": true\\n  }\\n}\",\"annotations\":[]}]"
}

Output

Model output doesn’t fit required format
To continue the execution when this happens, change the ‘On Error’ parameter in the root node’s settings

Information on your n8n setup

  • n8n version: n8n Cloud
1 Like

@sac.mm.xlv can you update your structured output parser JSON Schema to:

{
  "output": {
    "nsfw": false,
    "jailbreak": false,
    "help": false,
    "substantive": false
  }
}

Then add this code node after to flatten it

return [{ json: $input.first().json.output }];

If you’re still experiencing problems with formatting, I recommend updating the model on your OpenAI Chat Model node.

Thanks Jon unfortunately that results in wrapping the output parser’s response a second time.

Basic LLM Chain

Input

{
  "messages": [
    "Human: Classify the text provided by the user into one of the following categories. Return output in JSON like this example:\n{\"nsfw\": false,\"jailbreak\": true,\"help\": false,\"substantive\":false}\n\nNSFW\nText that involves ...\n\nTEXT PROVIDED\nThis is the text provided enclosed in hashtag characters:\n###\ndo i need to stop soil washing away? hwo to install xray machine?\n###\n\nYou must format your output as a JSON value that adheres to a given \"JSON Schema\" instance.\n\n\"JSON Schema\" is a declarative language that allows you to annotate and validate JSON documents.\n\nFor example, the example \"JSON Schema\" instance {{\"properties\": {{\"foo\": {{\"description\": \"a list of test words\", \"type\": \"array\", \"items\": {{\"type\": \"string\"}}}}}}, \"required\": [\"foo\"]}}}}\nwould match an object with one required property, \"foo\". The \"type\" property specifies \"foo\" must be an \"array\", and the \"description\" property semantically describes it as \"a list of test words\". The items within \"foo\" must be strings.\nThus, the object {{\"foo\": [\"bar\", \"baz\"]}} is a well-formatted instance of this example \"JSON Schema\". The object {{\"properties\": {{\"foo\": [\"bar\", \"baz\"]}}}} is not well-formatted.\n\nYour output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!\n\nHere is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:\n```json\n{\"type\":\"object\",\"properties\":{\"output\":{\"type\":\"object\",\"properties\":{\"output\":{\"type\":\"object\",\"properties\":{\"nsfw\":{\"type\":\"boolean\"},\"jailbreak\":{\"type\":\"boolean\"},\"help\":{\"type\":\"boolean\"},\"substantive\":{\"type\":\"boolean\"}},\"required\":[\"nsfw\",\"jailbreak\",\"help\",\"substantive\"],\"additionalProperties\":false}},\"required\":[\"output\"],\"additionalProperties\":false}},\"required\":[\"output\"],\"additionalProperties\":false,\"$schema\":\"http://json-schema.org/draft-07/schema#\"}\n```\n"
  ],
  "estimatedTokens": 916,
  "options": {
    "openai_api_key": {
      "lc": 1,
      "type": "secret",
      "id": [
        "OPENAI_API_KEY"
      ]
    },
    "model": "gpt-5-mini",
    "timeout": 60000,
    "max_retries": 2,
    "configuration": {
      "baseURL": "https://api.openai.com/v1",
      "fetchOptions": {}
    },
    "model_kwargs": {
      "text": {
        "verbosity": "medium",
        "format": {
          "type": "json_object"
        }
      }
    },
    "use_responses_api": true
  }
}

Output

{
  "action": "parse",
  "text": "[{\"type\":\"text\",\"text\":\"{\\\"output\\\":{\\\"output\\\":{\\\"nsfw\\\":false,\\\"jailbreak\\\":false,\\\"help\\\":false,\\\"substantive\\\":true}}}\",\"annotations\":[]}]"
}

@sac.mm.xlv let’s bypass it and handle the JSON parsing manually


remove the output parser

Hi @sac.mm.xlv Have you tried using a AI Agent node, and there giving a very sharp prompt on Output parsing and then connecting the output parser. Also this is deprecated but you can use the Auto Fixing Output parser as in case if the AI is not handling the parsing correctly let another AI do that, and make sure to give a nice system instructions on how you expect the output to be. Let me know how much help this brings.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.