zod validation errors serialize function call arguments

#  Description

When using the Google Generative AI provider with tools that have Zod schema validation errors, the AI SDK incorrectly serializes functionCall.args as a JSON string instead of an object in subsequent requests after a validation error occurs. This causes Google's API to reject the request with a protobuf validation error.

#  Environment

  - AI SDK Version: 5.0.4
  - Provider: @ai-sdk/google 2.0.2
  - Zod Version: 3.25.76

#  Reproduction Steps

  1. Create a tool with a Zod schema
  2. Call the tool with invalid parameters that trigger Zod validation to fail
  3. AI SDK attempts to send the conversation history including the validation error back to Google
  4. Request fails with protobuf validation error

#  Minimal Reproduction Code

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

  const model = google('gemini-2.5-flash');

  // Tool with .refine() validation
  const validateMagicWord = tool({
    description: 'Validates a magic word',
    parameters: z.object({
      magic: z.string()
        .refine((val) => val === 'abracadabra', 'Must be the magic word') // we use refine to reliably reproduce zod validation error
        .describe('The magic word'),
    }),
    execute: async ({ magic }) => {
      return `Success! You said: ${magic}`;
    },
  });

  // This will fail when the model retries after validation error
  const result = await generateText({
    model,
    system: 'Try the tool. If validation fails, read the error and retry.',
    messages: [
      { role: 'user', content: 'Call validateMagicWord with magic="wrong"' }
    ],
    tools: { validateMagicWord },
  });
```

#  Expected Behavior

The AI SDK should maintain functionCall.args as an object throughout the conversation history:

```
  {
    "contents": [
      {
        "role": "model",
        "parts": [{
          "functionCall": {
            "name": "validateMagicWord",
            "args": { "magic": "wrong" }  // Should remain as object
          }
        }]
      }
    ]
  }
```

#  Actual Behavior

After any Zod validation error, the AI SDK incorrectly converts functionCall.args to a JSON string in the conversation history:

```
  {
    "contents": [
      {
        "role": "model",
        "parts": [{
          "functionCall": {
            "name": "validateMagicWord",
            "args": "{\"magic\":\"wrong\"}"  // Incorrectly serialized as string
          }
        }]
      }
    ]
  }
```

#  Error Message

As a result, Google API returns this error message:

>  AI_APICallError: Invalid value at 'contents[1].parts[0].function_call.args' (type.googleapis.com/google.protobuf.Struct), "{"magic":"wrong"}"

# Root Cause Analysis

Through testing, we've confirmed:

  1. Working scenario (no Zod validation errors):
    - Tool calls work correctly
    - args is sent as an object
    - Google API accepts the request
  2. Failing scenario (any Zod validation error):
    - Initial tool call works (args sent as object)
    - Zod validation fails (whether from type mismatch, refine(), or any other validation rule)
    - AI SDK adds validation error to conversation
    - On retry, AI SDK incorrectly serializes previous functionCall.args as JSON string
    - Google API rejects with protobuf validation error

# Impact

This bug prevents effective use of Zod schema validation when using the Google Generative AI provider, as any validation error makes subsequent API calls fail. This significantly limits the ability to implement validation logic that the LLM can learn from and retry.

# Workaround

Currently, the only workaround is to avoid Zod schema validation entirely and implement validation logic in the tool's execute function instead, throwing errors there rather than using schema validation.

### AI SDK Version

_No response_

Labels: bug, ai/provider, provider/google
State: closed