Skip to content

Feat/ai samples function calling - #1075

Closed
sedanah-m wants to merge 1 commit into
feat/ai-samplesfrom
feat/ai-samples-function-calling
Closed

Feat/ai samples function calling#1075
sedanah-m wants to merge 1 commit into
feat/ai-samplesfrom
feat/ai-samples-function-calling

Conversation

@sedanah-m

Copy link
Copy Markdown
Contributor

No description provided.

@sedanah-m
sedanah-m changed the base branch from master to feat/ai-samples July 29, 2026 22:16
@wiz-9635d3485b

wiz-9635d3485b Bot commented Jul 29, 2026

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities -
Data Finding Sensitive Data -
Secret Finding Secrets -
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings -
Software Management Finding Software Management Findings -
Total -

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try the Wiz Code extension for VS Code, JetBrains, or Visual Studio.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces implementation examples for Firebase AI features, specifically adding function-calling capabilities and structured output generation (JSON and Enum schemas) with corresponding UI views. The review feedback highlights two critical issues in the function-calling service: first, the tools parameter in getAiModel must be passed as an array rather than a single object to avoid compilation or runtime errors; second, when the model returns multiple function calls, all responses must be collected and sent back together to prevent the Gemini API from throwing a runtime error.

Comment on lines +65 to +69
const model = getAiModel('gemini-3.6-flash', {
model: "gemini-3.6-flash",
// Provide the function declaration to the model.
tools: fetchWeatherTool
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The tools configuration parameter expects an array of tools (e.g., [fetchWeatherTool]) rather than a single tool object. Passing a single object will cause a TypeScript compilation error or runtime failure. Additionally, the model property is redundant here since it is already specified as the first argument to getAiModel.

const model = getAiModel('gemini-3.6-flash', {
    // Provide the function declaration to the model.
    tools: [fetchWeatherTool]
});

Comment on lines +79 to +108
const functionCalls = result.response.functionCalls() ?? [];
let functionCall;
let functionResult;
// When the model responds with one or more function calls, invoke the function(s).
if (functionCalls.length > 0) {
for (const call of functionCalls) {
if (call.name === "fetchWeather") {
// Forward the structured input data prepared by the model
// to the hypothetical external API.
functionResult = await fetchWeather(call.args as { location: { city: string; state: string }; date: string });
functionCall = call;
}
}
}

/**
* Step 5: Send the response from the function back to the model
* so that the model can use it to generate its final response.
*/

if (functionCall && functionResult) {
result = await chat.sendMessage([
{
functionResponse: {
name: functionCall.name, // "fetchWeather"
response: functionResult,
},
},
]);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

When the model returns multiple function calls in a single turn, the Gemini API requires that you provide a response for each of the requested function calls in the next message. If you only send a single response back (the last one that was overwritten in the loop), the API will throw a runtime error. Collecting all responses into an array and sending them together resolves this issue and ensures robust multi-turn function calling.

const functionCalls = result.response.functionCalls() ?? [];
const functionResponses = [];

// When the model responds with one or more function calls, invoke the function(s).
for (const call of functionCalls) {
    if (call.name === "fetchWeather") {
        // Forward the structured input data prepared by the model
        // to the hypothetical external API.
        const functionResult = await fetchWeather(call.args as { location: { city: string; state: string }; date: string });
        functionResponses.push({
            functionResponse: {
                name: call.name,
                response: functionResult,
            },
        });
    }
}

/**
 * Step 5: Send the response from the function back to the model
 * so that the model can use it to generate its final response.
 */
if (functionResponses.length > 0) {
    result = await chat.sendMessage(functionResponses);
}

@sedanah-m
sedanah-m force-pushed the feat/ai-samples-function-calling branch from 5444eb7 to e79f5e6 Compare July 29, 2026 22:39
@sedanah-m
sedanah-m force-pushed the feat/ai-samples-function-calling branch from ac1a63b to f2d1a6c Compare July 29, 2026 22:50
@sedanah-m sedanah-m closed this Jul 29, 2026
@sedanah-m

Copy link
Copy Markdown
Contributor Author

Branch got messy; creating a new one for a cleaner start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant