AI Agent Fails with "Received tool input did not match expected schema" on Complex Tool Call

  • Topic Title: AI Agent Fails with "Received tool input did not match expected schema" on Complex Tool Call
  • Your n8n Version: 1.103.2 (Cloud)
  • The Error: Received tool input did not match expected schema
  • Summary of the Problem:

“The AI Agent fails to call a Code node tool that uses multiple parameters (including some of type JSON). The error occurs even when the prompt provides a direct command with hardcoded data that perfectly matches the tool’s schema. Simple, no-argument tools work, but the agent fails when it needs to construct the arguments for a more complex tool.”

{
“nodes”: [
{
“parameters”: {
“chatModel”: “openai:gpt-4o”,
“prompt”: {
“__rl”: true,
“value”: “Your only task is to call the format_email_html tool. You must use these exact, hardcoded values for the parameters:\n- weatherData: { "conditions": "test weather", "temperature": 75 }\n- todaysEvents: [{ "name": "Test a meeting" }]\n- upcomingEvents: \n- recommendation: "This is a test of the formatting tool."”,
“mode”: “define”
}
},
“name”: “AI Agent”,
“type”: “@n8n/n8n-nodes-langchain.agent”,
“typeVersion”: 2.1,
“position”: [
1120,
360
],
“credentials”: {
“openAiApi”: {
“id”: “YOUR_CREDENTIAL_ID”,
“name”: “Your OpenAI Credential”
}
}
},
{
“parameters”: {
“toolName”: “format_email_html”,
“toolDescription”: “Formats the final email. It requires four parameters: weatherData (object), todaysEvents (array), upcomingEvents (array), and recommendation (string).”,
“specifySchema”: true,
“schemaType”: “fixed”,
“parameters”: {
“parameters”: [
{
“name”: “weatherData”,
“type”: “json”,
“required”: true
},
{
“name”: “todaysEvents”,
“type”: “json”,
“required”: true
},
{
“name”: “upcomingEvents”,
“type”: “json”,
“required”: true
},
{
“name”: “recommendation”,
“type”: “string”,
“required”: true
}
]
},
“code”: “const weatherDataInput = weatherData || {};\nconst todaysEventsInput = todaysEvents || ;\nconst upcomingEventsInput = upcomingEvents || ;\nconst recommendationInput = recommendation || ‘No specific recommendation provided.’;\n\nconst htmlEmailBody = \n<html>\n <body>\n <h1>Test Report</h1>\n <h2>Weather</h2>\n <p>${weatherDataInput.conditions || 'N/A'}</p>\n <h2>Today's Events</h2>\n <p>${todaysEventsInput.length > 0 ? todaysEventsInput[0].name : 'N/A'}</p>\n <h2>Recommendation</h2>\n <p>${recommendationInput}</p>\n </body>\n</html>\n;\n\nreturn { string: htmlEmailBody };”
},
“name”: “format_email_html”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
900,
360
]
}
],
“connections”: {
“format_email_html”: {
“main”: [
[
{
“node”: “AI Agent”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

Hey @xtreme137 hope all is well. Welcome.
What do we have to do with the provided workflow to make it fail?

Thanks for responding. This is my first time building anything and I worked with Gemini Pro until it declared it couldn’t help any more. Simply plan: daily trigger, grab today calendar events, the next five days calendar events, the weather of my town, the airquality index (Utah has inverstion), and then output a nice email. Here is the AI Prompt simplied to not do everthing: Your only task is to call the format_email_html tool. You must use these exact, hardcoded values for the parameters:

  • weatherData: { “conditions”: “test weather”, “temperature”: 75 }
  • todaysEvents: [{ “name”: “Test a meeting” }]
  • upcomingEvents:
  • recommendation: “This is a test of the formatting tool.”.

Here is the email formatting that fails to match teh schema: // These variables are now passed directly as named parameters from the agent.
const weatherDataInput = weatherData || {};
const todaysEventsInput = todaysEvents || ;
const upcomingEventsInput = upcomingEvents || ;
const recommendationInput = recommendation || ‘No specific recommendation provided.’;

// Function to format event details
const formatEvent = (event) => {
const name = event.name || ‘N/A’;
const date = event.date || ‘N/A’;
const timeStart = event.timeStart || ‘’;
const timeEnd = event.timeEnd ? to ${event.timeEnd} : ‘’;
const platform = event.platform ? (${event.platform}) : ‘’;
return <li><strong>${name}</strong><br>When: ${date} ${timeStart}${timeEnd}${platform}</li>;
};

// Build the HTML for today’s events
let todaysEventsHtml = ‘’;
if (todaysEventsInput.length > 0) {
todaysEventsHtml = todaysEventsInput.map(formatEvent).join(‘’);
} else {
todaysEventsHtml = ‘

  • No events scheduled for today.
  • ’;
    }

    // Build the HTML for upcoming events
    let upcomingEventsHtml = ‘’;
    if (upcomingEventsInput.length > 0) {
    upcomingEventsHtml = upcomingEventsInput.map(formatEvent).join(‘’);
    } else {
    upcomingEventsHtml = ‘

  • No upcoming events in the next 5 days.
  • ’;
    }

    // Construct the full HTML string
    const htmlEmailBody = `

    body { font-family: Arial, sans-serif; line-height: 1.6; color: #333333; margin: 0; padding: 0; background-color: #f4f4f4; } .email-container { max-width: 600px; margin: 20px auto; padding: 20px; border: 1px solid #dddddd; border-radius: 8px; background-color: #ffffff; } h2 { color: #2c3e50; margin-top: 0; } h3 { color: #34495e; border-bottom: 1px solid #eeeeee; padding-bottom: 10px; } ul { list-style-type: none; padding: 0; } ul li { background-color: #f8f9fa; margin-bottom: 10px; padding: 10px; border-left: 3px solid #3498db; } .recommendation { background-color: #ecf0f1; padding: 15px; border-left: 5px solid #1abc9c; margin-top: 20px; border-radius: 4px; } .footer { margin-top: 30px; font-size: 0.9em; text-align: center; color: #7f8c8d; }

    Your Daily Update

        <h3>Weather & Air Quality</h3>
        <ul>
            <li><strong>Weather:</strong> ${weatherDataInput.conditions || 'N/A'} at ${weatherDataInput.temperature || 'N/A'}&deg;F (feels like ${weatherDataInput.feelsLike || 'N/A'}&deg;F)</li>
            <li><strong>Air Quality:</strong> ${(weatherDataInput.airQuality && weatherDataInput.airQuality.status) || 'N/A'} (AQI: ${(weatherDataInput.airQuality && weatherDataInput.airQuality.ozone) || 'N/A'})</li>
        </ul>
        
        <h3>Today's Events</h3>
        <ul>${todaysEventsHtml}</ul>
        
        <h3>Upcoming Events</h3>
        <ul>${upcomingEventsHtml}</ul>
        
        <div class="recommendation">
            <h3>💡 Recommendation</h3>
            <p>${recommendationInput}</p>
        </div>
        
        <div class="footer">
            <p>Have a great day!</p>
        </div>
    </div>
    
    `;

    // Return the final HTML in the correct object format for the agent
    return { string: htmlEmailBody };

    Let me know if you need anything else!

    Thanks.

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