-
Notifications
You must be signed in to change notification settings - Fork 49.7k
fix(SerpApi Tool Node): Fix missing query parameter error #21596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(SerpApi Tool Node): Fix missing query parameter error #21596
Conversation
### 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
|
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: Regarding new nodes: 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: Thank you again for contributing to n8n. |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
Problem
The SerpAPI tool fails with error
Missing query 'q' parameterbecause it passes the entire n8n input object to the SerpAPIinvoke()method instead of extracting the query stringRoot Cause
In the
execute()method, the code passes the complete input data object (containingjson,pairedItem, etc.) directly totool.invoke(), but SerpAPI expects a simple string parameter for the search querySummary
Implementation (
ToolSerpApi.node.ts)inputorchatInputfields before invoking the tooltool.invoke()instead of entire objectinputnorchatInputis providedTests (
ToolSerpApi.node.test.ts)inputfield matching actual implementationtool.invoke()is called with extracted string, not objectchatInputfallback wheninputis not providedRelated Linear tickets, Github issues, and Community forum posts
Fixes #21471
Review / Merge checklist
release/backport(if the PR is an urgent fix that needs to be backported)Note
Extracts query from
inputorchatInput, validates it, and passes the string to SerpAPI; updates tests with fallbacks and error cases.json.inputorjson.chatInput; validate non-empty string and throwNodeOperationErrorif missing.SerpAPI.invokeinstead of the whole item.inputand assertinvokereceives the string.chatInput.Written by Cursor Bugbot for commit 7549d77. This will update automatically on new commits. Configure here.