Tool calling broken with Google Gemini in ai@4.0.34 onwards

### Description

Using ai@4.0.34, calling a tool using streamText (and probably other functions as well) with an argument schema featuring either an optional array or an optional enum (other types may also trigger the error, these are the two I've pinned down) will make Google return some variation of this error:

```
Invalid value at 'tools.function_declarations[2].parameters.properties[0].value.any_of[0].items.properties[3].value.any_of[1].type' (type.googleapis.com/google.ai.generativelanguage.v1beta.Type), "null"
```

A schema that triggers the error:

```typescript
const optionalArraySchema = z.object({
  items: z.array(z.string()).optional(),
})
```

A schema that works:

```typescript
const requiredArraySchema = z.object({
  items: z.array(z.string()),
})
```

Such a tool only needs to be present in the LLM call, no matter what the user message is, to trigger the error. Note that I do not use .nullable() in my schemas as that is explicitly not supported by Google Gemini.

ai@4.0.33 works, this broke in 4.0.34. The version of @ai-sdk/google does not seem to matter, the newest works well with 4.0.33.

### Code example

```typescript
import { google } from '@ai-sdk/google'
import { streamText, tool } from 'ai'
import { z } from 'zod'

// Case 1: Simple types (works)
const simpleSchema = z.object({
  text: z.string(),
  number: z.number(),
})

// Case 2: Optional fields in object (works)
const optionalObjectSchema = z.object({
  required: z.string(),
  optional: z.string().optional(),
})

// Case 3: Required array (works)
const requiredArraySchema = z.object({
  items: z.array(z.string()),
})

// Case 4: Optional array (fails)
const optionalArraySchema = z.object({
  items: z.array(z.string()).optional(),
})

// Case 5: Required enum (works)
const requiredEnumSchema = z.object({
  type: z.enum(['a', 'b', 'c']),
})

// Case 6: Optional enum (fails)
const optionalEnumSchema = z.object({
  type: z.enum(['a', 'b', 'c']).optional(),
})

export async function testLLMHandler() {
  const result = streamText({
    model: google('gemini-2.0-flash-exp', {
      structuredOutputs: true,
    }),
    system: 'You are a helpful assistant.',
    messages: [{ role: 'user', content: 'Hello!' }],
    tools: {
      // Works
      simple_tool: tool({
        description: 'A tool with simple types',
        parameters: simpleSchema,
        execute: async (args) => {
          return { success: true }
        },
      }),
      // Works
      optional_object_tool: tool({
        description: 'A tool with optional fields in an object',
        parameters: optionalObjectSchema,
        execute: async (args) => {
          return { success: true }
        },
      }),
      // Works
      required_array_tool: tool({
        description: 'A tool with a required array field',
        parameters: requiredArraySchema,
        execute: async (args) => {
          return { success: true }
        },
      }),
      // Fails
      optional_array_tool: tool({
        description: 'A tool with an optional array field',
        parameters: optionalArraySchema,
        execute: async (args) => {
          return { success: true }
        },
      }),
      // Works
      required_enum_tool: tool({
        description: 'A tool with a required enum field',
        parameters: requiredEnumSchema,
        execute: async (args) => {
          return { success: true }
        },
      }),
      // Fails
      optional_enum_tool: tool({
        description: 'A tool with an optional enum field',
        parameters: optionalEnumSchema,
        execute: async (args) => {
          return { success: true }
        },
      }),
    },
  })

  return result.toDataStreamResponse({
    getErrorMessage: (error) => {
      console.log('Error:', error)
      return error instanceof Error ? error.message : 'Unknown error'
    },
  })
}

```

### AI provider

@ai-sdk/google@1.0.16

### Additional context

_No response_

Labels: bug
State: closed