# Google Search Tool and Custom Function Declaration

**URL:** <https://discuss.ai.google.dev/t/google-search-tool-and-custom-function-declaration/60671>\
**Category:** Gemini API\
**Tags:** gemini-flash, train-function\
**Created:** [January 14, 2025, 2:37pm UTC](https://discuss.ai.google.dev/t/google-search-tool-and-custom-function-declaration/60671 "2025-01-14T14:37:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philomath](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/philomath/32/31662_2.png) [@Philomath](https://discuss.ai.google.dev/u/Philomath)\
**Post date:** [January 14, 2025, 2:37pm UTC](https://discuss.ai.google.dev/t/google-search-tool-and-custom-function-declaration/60671/1 "2025-01-14T14:37:33Z")

</div>

Can anyone help me to use a custom function and the google search tool with gemini-2.0-flash-exp in node? I am always getting the error “Requst contains an invalid argument” Am using the npm package Using one of these tools by itself works fine. Here;s my code (edited)

```auto
class UnifiedGeminiAPI {
    constructor(options = {}) {
        const apiKey = process.env.GEMINI_API_KEY;
        if (!apiKey) {
            throw new Error("GEMINI_API_KEY not found in environment variables.");
        }

        this.genAI = new GoogleGenerativeAI(apiKey);
        this.model = this.genAI.getGenerativeModel({ model: "gemini-2.0-flash-exp" });
        this.debug = options.debug || false;
        this.defaultSystemPrompt = options.systemPrompt || `You are Professor Regenbogen, an expert LGBTQ+ journalist and content writer for Pride.Direct.
            Your expertise is in creating engaging, well-researched articles about LGBTQ+ topics for a German-speaking audience.`;
        
        // Define function declarations for blog publishing
        this.functionDeclarations = [{
            name: "publish_blog_post",
            description: "Publishes a blog post to Shopify. Only use when content is ready and user has confirmed publication.",
            parameters: {
                type: "object",
                properties: {
                    title: {
                        type: "string",
                        description: "The title of the blog post"
                    },
                    author: {
                        type: "string",
                        description: "The author of the blog post"
                    },
                    content: {
                        type: "string",
                        description: "The HTML content of the blog post"
                    },
                    tags: {
                        type: "array",
                        items: { type: "string" },
                        description: "Array of tags for the blog post"
                    },
                    published: {
                        type: "boolean",
                        description: "Whether to publish immediately"
                    },
                    userConfirmed: {
                        type: "boolean",
                        description: "Whether user has confirmed publication"
                    }
                },
                required: ["title", "author", "content", "userConfirmed"]
            }
        }];
    }

    log(...args) {
        if (this.debug) {
            console.log('[DEBUG]', ...args);
        }
    }

    async ask(text, options = {}) {
        try {
            // Build the complete request payload
            const requestPayload = {
                contents: [{ role: 'user', parts: [{ text: `${options.systemPrompt || this.defaultSystemPrompt}\n\n${text}` }] }],
                tools: [
                    { google_search: {} }, // Search tool
                    { function_declarations: this.functionDeclarations } // Reuse the class property
                ],
                generationConfig: {
                    maxOutputTokens: 2048,
                },
                safetySettings: [
                    {
                        category: "HARM_CATEGORY_HARASSMENT",
                        threshold: "BLOCK_NONE"
                    },
                    {
                        category: "HARM_CATEGORY_HATE_SPEECH",
                        threshold: "BLOCK_NONE"
                    },
                    {
                        category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                        threshold: "BLOCK_NONE"
                    },
                    {
                        category: "HARM_CATEGORY_DANGEROUS_CONTENT",
                        threshold: "BLOCK_NONE"
                    }
                ]
            };

            // Log the complete request payload
            this.log('Sending request to Gemini:');
            this.log(JSON.stringify(requestPayload, null, 2));
            const prompt = `${options.systemPrompt || this.defaultSystemPrompt}\n\n${text}`;
            
            const result = await this.model.generateContent(requestPayload);

            if (!result?.response?.candidates?.[0]?.content?.parts) {
                throw new Error('Invalid response format from Gemini');
            }

            // Get the response content
            const responsePart = result.response.candidates[0].content.parts[0];
            
            // Check if we have a function call
            if (responsePart.functionCall) {
                this.log('Function call detected:', responsePart.functionCall);
                
                const { name, args } = responsePart.functionCall;
                
                if (name === 'publish_blog_post') {
                    if (!args.userConfirmed) {
                        return responsePart.text + '\n\nWould you like to publish this article? Please confirm.';
                    }
                    
                    this.log('Publishing blog post...');
                    const result = await createBlogArticle(args);
                    return responsePart.text + `\n\nBlog post published successfully with ID: ${result.id}`;
                }
            }

            // Get any search citations if available
            let response = responsePart.text || '';
            if (result.response.candidates[0].citations) {
                response += '\n\nSources:\n';
                result.response.candidates[0].citations.forEach((citation, index) => {
                    response += `[${index + 1}] ${citation.url}\n`;
                });
            }

            return response;

        } catch (error) {
            console.error('Error querying Gemini:', error);
            throw error;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![GUNAND\_MAYANGLAMBAM](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/gunand_mayanglambam/32/4964_2.png) [@GUNAND\_MAYANGLAMBAM](https://discuss.ai.google.dev/u/GUNAND_MAYANGLAMBAM)\
**Post date:** [January 14, 2025, 2:49pm UTC](https://discuss.ai.google.dev/t/google-search-tool-and-custom-function-declaration/60671/2 "2025-01-14T14:49:56Z")

</div>

Hi @Philomath, Welcome to the forum.

Currently, multi-tool use is supported only through the Multimodal Live API.

We have a [notebook](https://github.com/google-gemini/cookbook/blob/main/gemini-2/live_api_tool_use.ipynb) that explains how to use tools with the Multimodal Live API. Please take a look.

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/system/32/1_2.png) [@system](https://discuss.ai.google.dev/u/system)\
**Post date:** [November 5, 2026, 6:19pm UTC](https://discuss.ai.google.dev/t/google-search-tool-and-custom-function-declaration/60671/5 "2026-11-05T18:19:23Z")

</div>

Closed on November 5, 2026, 6:19pm UTC

This thread was automatically closed 60 days after the last reply. If you are running into an issue, please create a new post.
