Fix output of AI-Agent- or Langchain-Node

Describe the problem/error/question

I’m currently running frequently into the issue that the output of any AI-Agent- or Langchain-Node doesn’t match my requirements and lead to an error (sometimes within the Agent-Node, sometimes in one of the next nodes).

What is the error message (if any)?

{
“errorMessage”: “Model output doesn’t fit required format”,
“errorDescription”: “To continue the execution when this happens, change the ‘On Error’ parameter in the root node’s settings”,
“errorDetails”: {},
“n8nDetails”: {
“time”: “27.9.2025, 14:21:27”,
“n8nVersion”: “1.112.5 (Self Hosted)”,
“binaryDataMode”: “default”
}
}

Please share your workflow

Share the output returned by the last node

[
{
“actionableFeedback”: “”,
“justification”: “”,
“error”: “Model output doesn’t fit required format”
}
]

Information on your n8n setup

  • n8n version: 1.112.5
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Linux VM via Proxmox

AI models’ output isn’t always robust – or isn’t close to what you needed in the first place. AI Models usually respond with plain text rather than in a formatted or structured format. Although the response you want to receive from an agent can be customized in a system message, the knowledge base differs from one model to another. So with a smarter model you may achieve your goal – and have a robust response.

But I suggest formatting the response of the AI by attaching a code node and formatting the output after it leaves the agent node altogether. Because I don’t trust the AI having a similar output to what I need all the time.

Thanks for the explanation! That makes sense, formatting the output separately after the agent node. Could you maybe share a concrete example of how you ensure the output from an AI agent node is actually transformed into the desired format? I’m especially interested in how you practically implement this with a code node and what steps you take to reliably structure the result.
That would really help me understand it better. Thanks in advance!

1 Like

Well there are many ways to structure the output of the agent afterwards. N8N has a built-in AI node specifically for this type of cases. '‘Information Extractor’’ lets you extract certain parts of a text message and form a JSON response and it’s AI based but very robust – fits your case (refer to image for reference).

However, you can still simplify the system message given to the agent and attach a code node afterwards with this code (example of a code piece – haven’t tested it yet);

def parse_agent_response(response):
    lines = response.split('\n')
    segments = []
    
    current_segment = {}
    for line in lines:
        if line.startswith('Segment:'):
            if current_segment:
                segments.append(current_segment)
            current_segment = {'segment': line[8:].strip()}
        elif line.startswith('Begründung:'):
            current_segment['justification'] = line[11:].strip()
    
    if current_segment:
        segments.append(current_segment)
    
    return {"codedSegments": segments}

Here is the node by itself (change the descriptions to fit your case):

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