[JS] Gemini models flagged `constrained: 'no-tools'`, forcing simulated structured output when tools are present
Describe the bug
Every Gemini model in js/plugins/google-genai (both googleai and vertexai variants) declares supports.constrained: 'no-tools' in its ModelInfo. The middleware in js/ai/src/model.ts interprets this as "native constrained generation is supported, but not when tools are present" and routes any combined tools + output: { schema } call through simulateConstrainedGeneration instead of the native path.
The simulator serializes the JSON schema as text, injects it into the user message ("Output should be in JSON format and conform to the following schema: ..."), and strips constrained, format, schema, and contentType from the request. Gemini never receives responseJsonSchema / responseMimeType: application/json for any tool-using call.
Result:
- Higher prompt-token cost (schema serialized into every request).
- Lower reliability than native structured output (model can produce malformed JSON or echo schema metadata; e.g., a stray
"$schema": "http://json-schema.org/draft-07/schema#"field leaking into streamed chunks). - Slower streaming (chunks are parser-recovered partials of text-mode JSON, not API-native incremental structured output).
The Gemini API has supported combined tools + responseSchema / responseJsonSchema since Gemini 2.0. The 'no-tools' flag predates that support and was never relaxed when newer models were registered.
To reproduce
import { googleAI } from '@genkit-ai/google-genai';
import { genkit, z } from 'genkit';
const ai = genkit({
plugins: [googleAI()],
model: googleAI.model('gemini-flash-latest'),
});
const Schema = z.object({
title: z.string(),
items: z.array(z.string()),
});
const myTool = ai.defineTool(
{ name: 'getThings', description: 'returns things', inputSchema: z.object({}), outputSchema: z.array(z.string()) },
async () => ['a', 'b', 'c'],
);
const { stream, response } = ai.generateStream({
prompt: 'Call getThings, then return a titled list.',
tools: [myTool],
output: { schema: Schema },
});
for await (const chunk of stream) {
console.log(JSON.stringify(chunk.output));
}
Observed: each streamed chunk's accumulated JSON includes a stray "$schema" field echoed from the prompt-injected schema. With native constrained generation, this field would not appear because Gemini constrains output via the API rather than via a prompted instruction.
Expected behavior
For Gemini 2.0+ models, tools + output: { schema } should route through the native path: generationConfig.responseJsonSchema = request.output.schema and responseMimeType = 'application/json' set on the Gemini API call, with no schema injection into the prompt.
Root cause
Both provider files default every model to constrained: 'no-tools':
- js/plugins/google-genai/src/googleai/gemini.ts:427
- js/plugins/google-genai/src/vertexai/gemini.ts:415
The middleware decision in js/ai/src/model.ts:368-378 then forces the simulator whenever tools are present.
Proposed fix
Change constrained: 'no-tools' to constrained: 'all' for the default Gemini metadata in both providers (the commonRef helper used by every entry in KNOWN_GEMINI_MODELS). The ai.ModelInfo['supports'].constrained enum already includes 'all' (model-types.ts:92), which means "constrained generation in all cases, including with tools" — exactly the behavior modern Gemini models actually have.
This is safe to do unconditionally now: every model in KNOWN_GEMINI_MODELS is Gemini 2.5 or newer (gemini-flash-latest, gemini-pro-latest, gemini-2.5-pro, gemini-2.5-flash, gemini-2.5-flash-lite, gemini-3-flash-preview, gemini-3.1-*, gemini-3.5-flash), and all Gemini 1.x models are deprecated. There are no live model entries that would regress.
Not a breaking change for any current model.
Runtime
- OS: macOS 15.6
- Genkit: 1.35.0 (also reproduced against current
main) - Node.js: v25.0.0
Additional context
If a future Gemini model lacks native tools+constrained support, it can opt out by declaring constrained: 'no-tools' explicitly in its model-specific info. The default in commonRef should reflect what the current model family actually supports.
Opened #5393 with the fix. End-to-end verified against gemini-flash-latest in an Angular SSR reproduction: before the fix, every streamed structured chunk included a stray "$schema":"http://json-schema.org/draft-07/schema#" field echoed from the prompt-injected schema; after the fix, chunks contain only the declared schema fields, and tool calls still resolve correctly.
Im facing the issue about json response constraint with the new gemini-3.1-flash-lite. But with gemini-3.1-flash-lite-preview version is still working. This is very annoying since the preview model will be no longer supported.