AI agent outputs in the wrong format when using a tool

Hey guys,

I am trying to build an agent that search in my website for information before replying a chat. The issue I am having is that the agent is not using the correct JSON format for the tool, and I can’t figure out why.

I need the agent to output this:

{
  "term": "multi coin combo",
  "mode": ["feature_requests", "documentation"]
}

But instead is using the tool with this:

{
  "query": "{\"term\": \"multi coin combo\", \"mode\": [\"feature_requests\", \"documentation\"]}"
}

I noticed this looking through the action logs, which was automatically appended to my original query:

Your 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!

Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:

{"type":"object","properties":{"output":{"type":"object","properties":{"query":{"type":"string"},"mode":{"type":"array","items":{"type":"string"}}},"additionalProperties":false}},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}

I think this is what interfering with my desired output, but I can’t figure out where is this text coming from.

I would like to share the workflow but not sure how to do it?

Any help is appreciated.

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

In the top right of your workflow, hit the 3 dots then download. Then copy paste the contents into a preformatted text box : </>

Thanks! Attached the workflow.

Try this node for the output parser:

You see this added to the prompt because you probably enabled the ‘Require specific output format’ toggle in the agent node settings and attached a structured parser with example JSON.
image
What this does is generate the actual JSON schema and pass it at the end of the prompt…

If you’ve done that the easiest thing you can try is to be more specific in your prompt and tell it things like:
Output <this information> into <this attribute>.
Also in the example you can do something like this
{"summary":"summary of the email goes here"}

That said relying on the AI to do it properly is risky and not guaranteed to work.
To improve chances of success you can:

  1. Add an autofixing parser
  2. Enable Retry on Fail
  3. Enable branch off on error and try to process the string with another AI node.

I have experienced the same problem a lot and the best result that i got were by having 1 AI generate the text without configuring specific format, and a second one that just formats it to json. This way each agent has 1 responsibility and is more likely to succeed. (You can add retries and error branch too)

Thanks for looking into it guys.

@daniel_lamphere with your suggestion I am getting a new error:

This is because is not what the subworkflow input expects, it’s still using the wrong format. The output parser is for the response of the model, not for the call of the tool, is that correct?

@Ventsislav_Minev

Thanks for the tips. I don’t have enabled “Required specific output” in the agent. Also I get what you mean by separating the steps but this I can’t separate because it’s between the agent and the tool call, which doesn’t accept any intermediaries.

I see. With tools it’s much more difficult.
Yes the output parser is for the output of the AI Agent node, not for tool calls.

As for the error you are getting, this often happens when the AI generates characters that break the json syntax. Very often it wraps code with json
or markdown

You can add instructions to the prompt to not do that but it still happens.
I haven’t found a way to prevent it completely so I send myself a message and fix it manually :expressionless:

For the life of me I can’t figure this out, I’ve spent the entire day trying to make this work with no luck. Anyone here have tips on how to make it work? Is the issue that I am trying to pass two keys to the tool instead of the default “query” the model always use?

I’ve got a similar issue.
The tool is a separate workflow and its JSON output is correct.
Is it possible to circumvent the 2nd AI model round, after the output has already been created correctly, and just return that?

Hey guys, I used this forum here to try to overcome the formatting issue as well, but the structured output solution did not work for me that well too.

What I did at the end was use the information extractor node. It needs some tweaks to get to the point you need, but the solution worked really well for me.