ai icon indicating copy to clipboard operation
ai copied to clipboard

Bug: Vertex AI strips tool parameters schema to empty properties

Open louis030195 opened this issue 6 months ago • 6 comments

Bug Report: Vertex AI function calling parameters schema stripped to empty properties

Description

When using tool() with custom parameters (either via jsonSchema() or dynamically-generated Zod schemas) with the Google Vertex AI provider, the tool parameters schema is incorrectly serialized. All property definitions are stripped out, leaving only an empty properties: {} object in the request sent to Vertex AI.

This causes all custom tools to fail with the error:

Unable to submit request because `<tool_name>` functionDeclaration parameters schema should be of type OBJECT

Environment

Reproduction

Minimal Test Case

import { tool, streamText, jsonSchema } from 'ai';
import { vertex } from '@ai-sdk/google-vertex';

// Define a simple tool with parameters
const testTool = tool({
  description: 'Update a workflow step',
  parameters: jsonSchema({
    type: 'object',
    properties: {
      step_id: {
        type: 'string',
        description: 'Step ID',
      },
      new_name: {
        type: 'string',
        description: 'New step name',
      },
    },
    required: ['step_id', 'new_name'],
  }, { validate: false }),
  execute: async (params) => {
    return { success: true };
  },
});

// Try to use the tool
const model = vertex('gemini-2.0-flash-exp');

const result = await streamText({
  model,
  messages: [{ role: 'user', content: 'Test' }],
  tools: { testTool },
});

// This fails with:
// Unable to submit request because `testTool` functionDeclaration
// parameters schema should be of type OBJECT

Also Fails with Zod

The same issue occurs when using Zod schemas:

import { z } from 'zod';

const testTool = tool({
  description: 'Update a workflow step',
  parameters: z.object({
    step_id: z.string().describe('Step ID'),
    new_name: z.string().describe('New step name'),
  }),
  execute: async (params) => {
    return { success: true };
  },
});

// Same error occurs

Expected Behavior

The request body sent to Vertex AI should contain the complete schema:

{
  "functionDeclarations": [
    {
      "name": "testTool",
      "description": "Update a workflow step",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "step_id": {
            "type": "STRING",
            "description": "Step ID"
          },
          "new_name": {
            "type": "STRING",
            "description": "New step name"
          }
        },
        "required": ["step_id", "new_name"]
      }
    }
  ]
}

Actual Behavior

The request body sent to Vertex AI has empty properties:

{
  "functionDeclarations": [
    {
      "name": "testTool",
      "description": "Update a workflow step",
      "parameters": {
        "properties": {}
      }
    }
  ]
}

The type, required, and all property definitions are missing.

Evidence

I captured the actual request body from the error object:

for await (const chunk of result.fullStream) {
  if (chunk.type === 'error') {
    const error = chunk.error;
    console.log('Request body:',
      JSON.stringify(error.requestBodyValues.tools, null, 2)
    );
  }
}

Output shows empty properties for all custom tools.

Working Comparison

MCP tools created via experimental_createMCPClient().tools() work correctly with the same Vertex AI provider, suggesting the issue is specific to the schema serialization path used by tool().

Root Cause

The issue appears to be in how the Vertex AI provider serializes tool schemas. The schema is correctly constructed in memory (verified via logging), but gets stripped during serialization to the Vertex AI API format.

This may be related to issue #6572 which mentions "parameters.properties field missing in request body when using Vertex AI".

Impact

This bug makes it impossible to use custom tools with the Vertex AI provider unless they have no parameters. Only MCP-provided tools work correctly.

Workaround

Currently, the only workaround is to use experimental_createMCPClient() for all tools, which is not practical for custom application-specific tools.

Additional Context

  • The schema cleaning/conversion logic works correctly (tested extensively)
  • Both jsonSchema() and Zod approaches fail identically
  • The error occurs during the HTTP request to Vertex AI
  • MCP tools using the same provider work fine
  • Error response from Vertex AI: INVALID_ARGUMENT with message about schema should be type OBJECT

Request

Please fix the schema serialization in the Vertex AI provider to preserve all schema properties when building function declarations for the Vertex AI API.

louis030195 avatar Oct 23 '25 19:10 louis030195

Update: Confirmed this bug exists in the latest versions:

The issue persists in v5 of the AI SDK.

louis030195 avatar Oct 23 '25 19:10 louis030195

I've identified and fixed the bug!

Root cause: The Google provider was not calling asSchema().jsonSchema to extract the JSON Schema from tool.inputSchema, which is a FlexibleSchema type. This resulted in passing a Schema object (with a jsonSchema getter) instead of a plain JSONSchema7 object.

Pull request: #9762

The fix properly extracts the JSON schema using await asSchema(tool.inputSchema).jsonSchema, matching the pattern used in other parts of the codebase.

louis030195 avatar Oct 23 '25 20:10 louis030195

Hey @gr2m, @nicoalbanese, @lgrammel , could you please take a look at the PR I submitted? Link: https://github.com/vercel/ai/pull/9763

sujal12344 avatar Oct 23 '25 21:10 sujal12344

@sujal12344 why did you submit the same pr as #9762 ?

lgrammel avatar Oct 24 '25 07:10 lgrammel

@louis030195 the fix cannot work imo. was this done with ai or did you do it yourself?

lgrammel avatar Oct 24 '25 07:10 lgrammel

@lgrammel i use my branch in my project and it solved my problem

louis030195 avatar Oct 25 '25 18:10 louis030195