ai icon indicating copy to clipboard operation
ai copied to clipboard

`@ai-sdk/google{-vertex}`: ignores `strict:true` on tools

Open heiwen opened this issue 9 months ago • 6 comments

Description

In the AI SDK Google and Google Vertex providers, tool strictness is not reflected in Gemini function-calling mode.

From an end-user perspective, setting strict: true on tools should enable validated/strict function calling behavior, but today the provider sends ANY (or AUTO/NONE) only.

Minimal Reproduction

import { generateText, tool } from 'ai';
import { google } from '@ai-sdk/google';
import { z } from 'zod';

await generateText({
  model: google('gemini-3-flash-preview'),
  toolChoice: 'required',
  tools: {
    createMeeting: tool({
      description: 'Create a meeting',
      inputSchema: z.object({
        title: z.string(),
        startTime: z.string(),
        attendees: z.array(z.string()),
      }),
      strict: true, // ignored
    }),
  },
  prompt: 'Schedule a product review tomorrow with [email protected]',
});

Expected
When tool strictness is requested (strict: true), Gemini should use validated function calling mode (VALIDATED).

https://ai.google.dev/gemini-api/docs/function-calling#function_calling_modes

Actual
Google provider currently maps function calling config modes to AUTO | NONE | ANY only, so validated mode is not used.

Impact

  • Less predictable schema-conformant tool arguments
  • Extra app-side validation/retry logic
  • Inconsistent strict behavior across providers

Request
Please add support in the Google/Gemini provider to use VALIDATED mode when strict tool behavior is requested (e.g. strict: true), and keep existing behavior for non-strict tools.

AI SDK Version

No response

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

heiwen avatar Feb 23 '26 05:02 heiwen

I'd like to work on this.

Looking at packages/google/src/google-prepare-tools.ts, the toolConfig.functionCallingConfig.mode type is currently 'AUTO' | 'NONE' | 'ANY' — missing 'VALIDATED'.

One design question: Gemini's VALIDATED mode is a global functionCallingConfig setting, while strict in the AI SDK is per-tool. If some tools have strict: true and others don't, we need a policy:

  1. If any tool has strict: true, use VALIDATED mode — safest, matches the intent of requesting strict validation
  2. Only use VALIDATED when all tools have strict: true — more conservative

I'd lean toward option 1, since a user setting strict: true on any tool clearly wants schema validation enforced.

The implementation would:

  • Add 'VALIDATED' to the mode union type
  • Check if any tool in the tools array has strict: true
  • If so, set mode to 'VALIDATED' instead of the default for that toolChoice type
  • This applies to auto, required, and tool choice types

Would appreciate guidance on the preferred approach before submitting a PR.

echoVic avatar Feb 25 '26 07:02 echoVic

Personally, I agree with option 1: "If any tool has strict: true, use VALIDATED mode — safest, matches the intent of requesting strict validation".

heiwen avatar Feb 25 '26 08:02 heiwen

@echoVic feel free to submit a PR with this approach and we'll take a look if it works

aayush-kapoor avatar Feb 26 '26 20:02 aayush-kapoor

Looks good to me. @aayush-kapoor what do you think?

heiwen avatar Mar 03 '26 08:03 heiwen

:rocket: Published in:

Package Version
@ai-sdk/google 4.0.0-beta.1
@ai-sdk/google-vertex 5.0.0-beta.1

ghost avatar Mar 05 '26 05:03 ghost

Identify issue type

Identified as Bug
Confidence: High

Rationale

This is a concrete failure of existing tool strictness behavior: the providers did not honor strict: true when configuring Gemini function calling.

Evidence

  • Setting strict: true on a tool is ignored by the Google and Google Vertex providers.
  • The providers only mapped Gemini function-calling modes to AUTO, NONE, or ANY, omitting VALIDATED.
  • A maintainer invited a PR for mapping strict tools to VALIDATED, and the change was later published in beta package versions.

lgrammel avatar Jul 22 '26 11:07 lgrammel