Skip to content

Conversation

@actuallyzefe
Copy link

@actuallyzefe actuallyzefe commented Nov 5, 2025

Problem

The SerpAPI tool fails with error Missing query 'q' parameter because it passes the entire n8n input object to the SerpAPI invoke() method instead of extracting the query string

Root Cause

In the execute() method, the code passes the complete input data object (containing json, pairedItem, etc.) directly to tool.invoke(), but SerpAPI expects a simple string parameter for the search query

Summary

Implementation (ToolSerpApi.node.ts)

  • Extract query string from input or chatInput fields before invoking the tool
  • Pass extracted string directly to tool.invoke() instead of entire object
  • Add proper error handling when neither input nor chatInput is provided
  • Add logging for missing query string errors

Tests (ToolSerpApi.node.test.ts)

  • Update all test cases to use input field matching actual implementation
  • Verify tool.invoke() is called with extracted string, not object
  • Add test for chatInput fallback when input is not provided
  • Add test for error handling when neither field is present
  • Verify error logging is called with correct message

Related Linear tickets, Github issues, and Community forum posts

Fixes #21471

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

Note

Extracts query from input or chatInput, validates it, and passes the string to SerpAPI; updates tests with fallbacks and error cases.

  • ToolSerpApi.node.ts
    • Extract query from json.input or json.chatInput; validate non-empty string and throw NodeOperationError if missing.
    • Pass extracted string to SerpAPI.invoke instead of the whole item.
  • ToolSerpApi.node.test.ts
    • Update tests to use input and assert invoke receives the string.
    • Add fallback test for chatInput.
    • Add error tests for missing or invalid query types.

Written by Cursor Bugbot for commit 7549d77. This will update automatically on new commits. Configure here.

### Problem
The SerpAPI tool fails with error `Missing query 'q' parameter` because it passes the entire n8n input object to the SerpAPI `invoke()` method instead of extracting the query string

### Root Cause
In the `execute()` method, the code passes the complete input data object (containing `json`, `pairedItem`, etc.) directly to `tool.invoke()`, but SerpAPI expects a simple string parameter for the search query

### Changes Made

#### Implementation (`ToolSerpApi.node.ts`)
- Extract query string from `input` or `chatInput` fields before invoking the tool
- Pass extracted string directly to `tool.invoke()` instead of entire object
- Add proper error handling when neither `input` nor `chatInput` is provided
- Add logging for missing query string errors

#### Tests (`ToolSerpApi.node.test.ts`)
- Update all test cases to use `input` field matching actual implementation
- Verify `tool.invoke()` is called with extracted string, not object
- Add test for `chatInput` fallback when `input` is not provided
- Add test for error handling when neither field is present
- Verify error logging is called with correct message
@actuallyzefe actuallyzefe changed the title ## Fix SerpAPI tool query parameter handling Fix SerpAPI tool query parameter handling Nov 5, 2025
@CLAassistant
Copy link

CLAassistant commented Nov 5, 2025

CLA assistant check
All committers have signed the CLA.

@n8n-assistant n8n-assistant bot added community Authored by a community member in linear Issue or PR has been created in Linear for internal review labels Nov 5, 2025
@n8n-assistant
Copy link

n8n-assistant bot commented Nov 5, 2025

Hey @actuallyzefe,

Thank you for your contribution. We appreciate the time and effort you’ve taken to submit this pull request.

Before we can proceed, please ensure the following:
• Tests are included for any new functionality, logic changes or bug fixes.
• The PR aligns with our contribution guidelines.

Regarding new nodes:
We no longer accept new nodes directly into the core codebase. Instead, we encourage contributors to follow our Community Node Submission Guide to publish nodes independently.

If your node integrates with an AI service that you own or represent, please email [email protected] and we will be happy to discuss the best approach.

About review timelines:
This PR has been added to our internal tracker as "GHC-5374". While we plan to review it, we are currently unable to provide an exact timeframe. Our goal is to begin reviews within a month, but this may change depending on team priorities. We will reach out when the review begins.

Thank you again for contributing to n8n.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

const query = inputData[itemIndex];
const result = await tool.invoke(query);
const item = inputData[itemIndex].json;
const queryString = (item.input ?? item.chatInput) as string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Nullish coalescing misses empty string fallback bug

The nullish coalescing operator (??) only checks for null/undefined, not other falsy values. If item.input is an empty string ("") and item.chatInput contains a valid query, the code will not fall back to chatInput. Instead, queryString will be "" and the subsequent validation will throw an error, even though a valid query exists in chatInput. This should use the logical OR operator (||) instead to properly handle empty strings as missing input.

Fix in Cursor Fix in Web

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="packages/@n8n/nodes-langchain/nodes/tools/ToolSerpApi/ToolSerpApi.node.ts">

<violation number="1" location="packages/@n8n/nodes-langchain/nodes/tools/ToolSerpApi/ToolSerpApi.node.ts:138">
Rule violated: **Prefer Typeguards over Type casting**

Replace the `as string` assertion with an explicit string check so the query is narrowed without violating the Prefer Typeguards over Type casting rule.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

const query = inputData[itemIndex];
const result = await tool.invoke(query);
const item = inputData[itemIndex].json;
const queryString = (item.input ?? item.chatInput) as string;
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rule violated: Prefer Typeguards over Type casting

Replace the as string assertion with an explicit string check so the query is narrowed without violating the Prefer Typeguards over Type casting rule.

Prompt for AI agents
Address the following comment on packages/@n8n/nodes-langchain/nodes/tools/ToolSerpApi/ToolSerpApi.node.ts at line 138:

<comment>Replace the `as string` assertion with an explicit string check so the query is narrowed without violating the Prefer Typeguards over Type casting rule.</comment>

<file context>
@@ -133,8 +134,16 @@ export class ToolSerpApi implements INodeType {
-			const query = inputData[itemIndex];
-			const result = await tool.invoke(query);
+			const item = inputData[itemIndex].json;
+			const queryString = (item.input ?? item.chatInput) as string;
+
+			if (!queryString) {
</file context>

✅ Addressed in 4a476e1

### Changes Made
- Updated the `execute()` method to validate the query string, ensuring it is a non-empty string.
- Added tests to cover various invalid input types, confirming that appropriate errors are thrown for empty strings, numbers, objects, arrays, null, and undefined inputs.
@actuallyzefe actuallyzefe changed the title Fix SerpAPI tool query parameter handling fix(SerpApi Tool Node): Fix missing query parameter error Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Authored by a community member in linear Issue or PR has been created in Linear for internal review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI Agent version 3 fails to use SerpAPI

2 participants