UNPKG

@ai-sdk/google

Version:

The **[Google Generative AI provider](https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Google Generative AI](https://ai.google/discover/generativeai/)

1 lines 160 kB
{"version":3,"sources":["../../src/google-generative-ai-language-model.ts","../../src/convert-google-generative-ai-usage.ts","../../src/convert-json-schema-to-openapi-schema.ts","../../src/convert-to-google-generative-ai-messages.ts","../../src/get-model-path.ts","../../src/google-error.ts","../../src/google-generative-ai-options.ts","../../src/google-prepare-tools.ts","../../src/google-json-accumulator.ts","../../src/map-google-generative-ai-finish-reason.ts","../../src/tool/code-execution.ts","../../src/tool/enterprise-web-search.ts","../../src/tool/file-search.ts","../../src/tool/google-maps.ts","../../src/tool/google-search.ts","../../src/tool/url-context.ts","../../src/tool/vertex-rag-store.ts","../../src/google-tools.ts"],"sourcesContent":["import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Content,\n LanguageModelV3FinishReason,\n LanguageModelV3GenerateResult,\n LanguageModelV3Source,\n LanguageModelV3StreamPart,\n LanguageModelV3StreamResult,\n JSONObject,\n SharedV3ProviderMetadata,\n SharedV3Warning,\n} from '@ai-sdk/provider';\nimport {\n combineHeaders,\n createEventSourceResponseHandler,\n createJsonResponseHandler,\n generateId,\n lazySchema,\n parseProviderOptions,\n postJsonToApi,\n resolve,\n zodSchema,\n type FetchFunction,\n type InferSchema,\n type ParseResult,\n type Resolvable,\n} from '@ai-sdk/provider-utils';\nimport { z } from 'zod/v4';\nimport {\n convertGoogleGenerativeAIUsage,\n type GoogleGenerativeAIUsageMetadata,\n} from './convert-google-generative-ai-usage';\nimport { convertJSONSchemaToOpenAPISchema } from './convert-json-schema-to-openapi-schema';\nimport { convertToGoogleGenerativeAIMessages } from './convert-to-google-generative-ai-messages';\nimport { getModelPath } from './get-model-path';\nimport { googleFailedResponseHandler } from './google-error';\nimport {\n googleLanguageModelOptions,\n type GoogleGenerativeAIModelId,\n} from './google-generative-ai-options';\nimport type {\n GoogleGenerativeAIContentPart,\n GoogleGenerativeAIProviderMetadata,\n} from './google-generative-ai-prompt';\nimport { prepareTools } from './google-prepare-tools';\nimport {\n GoogleJSONAccumulator,\n type PartialArg,\n} from './google-json-accumulator';\nimport { mapGoogleGenerativeAIFinishReason } from './map-google-generative-ai-finish-reason';\n\ntype GoogleGenerativeAIConfig = {\n provider: string;\n baseURL: string;\n headers: Resolvable<Record<string, string | undefined>>;\n fetch?: FetchFunction;\n generateId: () => string;\n\n /**\n * The supported URLs for the model.\n */\n supportedUrls?: () => LanguageModelV3['supportedUrls'];\n};\n\nexport class GoogleGenerativeAILanguageModel implements LanguageModelV3 {\n readonly specificationVersion = 'v3';\n\n readonly modelId: GoogleGenerativeAIModelId;\n\n private readonly config: GoogleGenerativeAIConfig;\n private readonly generateId: () => string;\n\n constructor(\n modelId: GoogleGenerativeAIModelId,\n config: GoogleGenerativeAIConfig,\n ) {\n this.modelId = modelId;\n this.config = config;\n this.generateId = config.generateId ?? generateId;\n }\n\n get provider(): string {\n return this.config.provider;\n }\n\n get supportedUrls() {\n return this.config.supportedUrls?.() ?? {};\n }\n\n private async getArgs(\n {\n prompt,\n maxOutputTokens,\n temperature,\n topP,\n topK,\n frequencyPenalty,\n presencePenalty,\n stopSequences,\n responseFormat,\n seed,\n tools,\n toolChoice,\n providerOptions,\n }: LanguageModelV3CallOptions,\n { isStreaming = false }: { isStreaming?: boolean } = {},\n ) {\n const warnings: SharedV3Warning[] = [];\n\n const providerOptionsName = this.config.provider.includes('vertex')\n ? 'vertex'\n : 'google';\n let googleOptions = await parseProviderOptions({\n provider: providerOptionsName,\n providerOptions,\n schema: googleLanguageModelOptions,\n });\n\n if (googleOptions == null && providerOptionsName !== 'google') {\n googleOptions = await parseProviderOptions({\n provider: 'google',\n providerOptions,\n schema: googleLanguageModelOptions,\n });\n }\n\n // Add warning if Vertex rag tools are used with a non-Vertex Google provider\n const isVertexProvider = this.config.provider.startsWith('google.vertex.');\n\n if (\n tools?.some(\n tool =>\n tool.type === 'provider' && tool.id === 'google.vertex_rag_store',\n ) &&\n !isVertexProvider\n ) {\n warnings.push({\n type: 'other',\n message:\n \"The 'vertex_rag_store' tool is only supported with the Google Vertex provider \" +\n 'and might not be supported or could behave unexpectedly with the current Google provider ' +\n `(${this.config.provider}).`,\n });\n }\n\n if (googleOptions?.streamFunctionCallArguments && !isVertexProvider) {\n warnings.push({\n type: 'other',\n message:\n \"'streamFunctionCallArguments' is only supported on the Vertex AI API \" +\n 'and will be ignored with the current Google provider ' +\n `(${this.config.provider}). See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling#streaming-fc`,\n });\n }\n\n if (googleOptions?.serviceTier && isVertexProvider) {\n warnings.push({\n type: 'other',\n message:\n \"'serviceTier' is a Gemini API option and is not supported on Vertex AI. \" +\n \"Use 'sharedRequestType' (and optionally 'requestType') instead. See \" +\n 'https://docs.cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo',\n });\n }\n if (\n (googleOptions?.sharedRequestType || googleOptions?.requestType) &&\n !isVertexProvider\n ) {\n warnings.push({\n type: 'other',\n message:\n \"'sharedRequestType' and 'requestType' are Vertex AI options and \" +\n `are ignored with the current Google provider (${this.config.provider}).`,\n });\n }\n\n const vertexPaygoHeaders: Record<string, string> | undefined =\n isVertexProvider &&\n (googleOptions?.sharedRequestType || googleOptions?.requestType)\n ? {\n ...(googleOptions.sharedRequestType && {\n 'X-Vertex-AI-LLM-Shared-Request-Type':\n googleOptions.sharedRequestType,\n }),\n ...(googleOptions.requestType && {\n 'X-Vertex-AI-LLM-Request-Type': googleOptions.requestType,\n }),\n }\n : undefined;\n const bodyServiceTier = isVertexProvider\n ? undefined\n : googleOptions?.serviceTier;\n\n const isGemmaModel = this.modelId.toLowerCase().startsWith('gemma-');\n const supportsFunctionResponseParts = this.modelId.startsWith('gemini-3');\n\n const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(\n prompt,\n {\n isGemmaModel,\n providerOptionsName,\n supportsFunctionResponseParts,\n },\n );\n\n const {\n tools: googleTools,\n toolConfig: googleToolConfig,\n toolWarnings,\n } = prepareTools({\n tools,\n toolChoice,\n modelId: this.modelId,\n isVertexProvider,\n });\n\n const streamFunctionCallArguments =\n isStreaming && isVertexProvider\n ? (googleOptions?.streamFunctionCallArguments ?? false)\n : undefined;\n\n const toolConfig =\n googleToolConfig ||\n streamFunctionCallArguments ||\n googleOptions?.retrievalConfig\n ? {\n ...googleToolConfig,\n ...(streamFunctionCallArguments && {\n functionCallingConfig: {\n ...googleToolConfig?.functionCallingConfig,\n streamFunctionCallArguments: true as const,\n },\n }),\n ...(googleOptions?.retrievalConfig && {\n retrievalConfig: googleOptions.retrievalConfig,\n }),\n }\n : undefined;\n\n return {\n args: {\n generationConfig: {\n // standardized settings:\n maxOutputTokens,\n temperature,\n topK,\n topP,\n frequencyPenalty,\n presencePenalty,\n stopSequences,\n seed,\n\n // response format:\n responseMimeType:\n responseFormat?.type === 'json' ? 'application/json' : undefined,\n responseSchema:\n responseFormat?.type === 'json' &&\n responseFormat.schema != null &&\n // Google GenAI does not support all OpenAPI Schema features,\n // so this is needed as an escape hatch:\n // TODO convert into provider option\n (googleOptions?.structuredOutputs ?? true)\n ? convertJSONSchemaToOpenAPISchema(responseFormat.schema)\n : undefined,\n ...(googleOptions?.audioTimestamp && {\n audioTimestamp: googleOptions.audioTimestamp,\n }),\n\n // provider options:\n responseModalities: googleOptions?.responseModalities,\n thinkingConfig: googleOptions?.thinkingConfig,\n ...(googleOptions?.mediaResolution && {\n mediaResolution: googleOptions.mediaResolution,\n }),\n ...(googleOptions?.imageConfig && {\n imageConfig: googleOptions.imageConfig,\n }),\n },\n contents,\n systemInstruction: isGemmaModel ? undefined : systemInstruction,\n safetySettings: googleOptions?.safetySettings,\n tools: googleTools,\n toolConfig,\n cachedContent: googleOptions?.cachedContent,\n labels: googleOptions?.labels,\n serviceTier: bodyServiceTier,\n },\n warnings: [...warnings, ...toolWarnings],\n providerOptionsName,\n extraHeaders: vertexPaygoHeaders,\n };\n }\n\n async doGenerate(\n options: LanguageModelV3CallOptions,\n ): Promise<LanguageModelV3GenerateResult> {\n const { args, warnings, providerOptionsName, extraHeaders } =\n await this.getArgs(options);\n\n const mergedHeaders = combineHeaders(\n await resolve(this.config.headers),\n options.headers,\n extraHeaders,\n );\n\n const {\n responseHeaders,\n value: response,\n rawValue: rawResponse,\n } = await postJsonToApi({\n url: `${this.config.baseURL}/${getModelPath(\n this.modelId,\n )}:generateContent`,\n headers: mergedHeaders,\n body: args,\n failedResponseHandler: googleFailedResponseHandler,\n successfulResponseHandler: createJsonResponseHandler(responseSchema),\n abortSignal: options.abortSignal,\n fetch: this.config.fetch,\n });\n\n const candidate = response.candidates[0];\n const content: Array<LanguageModelV3Content> = [];\n\n // map ordered parts to content:\n const parts = candidate.content?.parts ?? [];\n\n const usageMetadata = response.usageMetadata;\n\n // Associates a code execution result with its preceding call.\n let lastCodeExecutionToolCallId: string | undefined;\n // Associates a server-side tool response with its preceding call (tool combination).\n let lastServerToolCallId: string | undefined;\n\n // Build content array from all parts\n for (const part of parts) {\n if ('executableCode' in part && part.executableCode?.code) {\n const toolCallId = this.config.generateId();\n lastCodeExecutionToolCallId = toolCallId;\n\n content.push({\n type: 'tool-call',\n toolCallId,\n toolName: 'code_execution',\n input: JSON.stringify(part.executableCode),\n providerExecuted: true,\n });\n } else if ('codeExecutionResult' in part && part.codeExecutionResult) {\n content.push({\n type: 'tool-result',\n // Assumes a result directly follows its corresponding call part.\n toolCallId: lastCodeExecutionToolCallId!,\n toolName: 'code_execution',\n result: {\n outcome: part.codeExecutionResult.outcome,\n output: part.codeExecutionResult.output ?? '',\n },\n });\n // Clear the ID after use to avoid accidental reuse.\n lastCodeExecutionToolCallId = undefined;\n } else if ('text' in part && part.text != null) {\n const thoughtSignatureMetadata = part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n },\n }\n : undefined;\n\n if (part.text.length === 0) {\n if (thoughtSignatureMetadata != null && content.length > 0) {\n const lastContent = content[content.length - 1];\n lastContent.providerMetadata = thoughtSignatureMetadata;\n }\n } else {\n content.push({\n type: part.thought === true ? 'reasoning' : 'text',\n text: part.text,\n providerMetadata: thoughtSignatureMetadata,\n });\n }\n } else if ('functionCall' in part && part.functionCall.name != null) {\n content.push({\n type: 'tool-call' as const,\n toolCallId: part.functionCall.id ?? this.config.generateId(),\n toolName: part.functionCall.name,\n input: JSON.stringify(part.functionCall.args ?? {}),\n providerMetadata: part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n },\n }\n : undefined,\n });\n } else if ('inlineData' in part) {\n const hasThought = part.thought === true;\n const hasThoughtSignature = !!part.thoughtSignature;\n content.push({\n type: 'file' as const,\n data: part.inlineData.data,\n mediaType: part.inlineData.mimeType,\n providerMetadata:\n hasThought || hasThoughtSignature\n ? {\n [providerOptionsName]: {\n ...(hasThought ? { thought: true } : {}),\n ...(hasThoughtSignature\n ? { thoughtSignature: part.thoughtSignature }\n : {}),\n },\n }\n : undefined,\n });\n } else if ('toolCall' in part && part.toolCall) {\n const toolCallId = part.toolCall.id ?? this.config.generateId();\n lastServerToolCallId = toolCallId;\n content.push({\n type: 'tool-call',\n toolCallId,\n toolName: `server:${part.toolCall.toolType}`,\n input: JSON.stringify(part.toolCall.args ?? {}),\n providerExecuted: true,\n dynamic: true,\n providerMetadata: part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n serverToolCallId: toolCallId,\n serverToolType: part.toolCall.toolType,\n },\n }\n : {\n [providerOptionsName]: {\n serverToolCallId: toolCallId,\n serverToolType: part.toolCall.toolType,\n },\n },\n });\n } else if ('toolResponse' in part && part.toolResponse) {\n const responseToolCallId =\n lastServerToolCallId ??\n part.toolResponse.id ??\n this.config.generateId();\n content.push({\n type: 'tool-result',\n toolCallId: responseToolCallId,\n toolName: `server:${part.toolResponse.toolType}`,\n result: (part.toolResponse.response ?? {}) as JSONObject,\n providerMetadata: part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n serverToolCallId: responseToolCallId,\n serverToolType: part.toolResponse.toolType,\n },\n }\n : {\n [providerOptionsName]: {\n serverToolCallId: responseToolCallId,\n serverToolType: part.toolResponse.toolType,\n },\n },\n });\n lastServerToolCallId = undefined;\n }\n }\n\n const sources =\n extractSources({\n groundingMetadata: candidate.groundingMetadata,\n generateId: this.config.generateId,\n }) ?? [];\n for (const source of sources) {\n content.push(source);\n }\n\n return {\n content,\n finishReason: {\n unified: mapGoogleGenerativeAIFinishReason({\n finishReason: candidate.finishReason,\n // Only count client-executed tool calls for finish reason determination.\n hasToolCalls: content.some(\n part => part.type === 'tool-call' && !part.providerExecuted,\n ),\n }),\n raw: candidate.finishReason ?? undefined,\n },\n usage: convertGoogleGenerativeAIUsage(usageMetadata),\n warnings,\n providerMetadata: {\n [providerOptionsName]: {\n promptFeedback: response.promptFeedback ?? null,\n groundingMetadata: candidate.groundingMetadata ?? null,\n urlContextMetadata: candidate.urlContextMetadata ?? null,\n safetyRatings: candidate.safetyRatings ?? null,\n usageMetadata: usageMetadata ?? null,\n finishMessage: candidate.finishMessage ?? null,\n serviceTier: usageMetadata?.serviceTier ?? null,\n } satisfies GoogleGenerativeAIProviderMetadata,\n },\n request: { body: args },\n response: {\n // TODO timestamp, model id, id\n headers: responseHeaders,\n body: rawResponse,\n },\n };\n }\n\n async doStream(\n options: LanguageModelV3CallOptions,\n ): Promise<LanguageModelV3StreamResult> {\n const { args, warnings, providerOptionsName, extraHeaders } =\n await this.getArgs(options, { isStreaming: true });\n\n const headers = combineHeaders(\n await resolve(this.config.headers),\n options.headers,\n extraHeaders,\n );\n\n const { responseHeaders, value: response } = await postJsonToApi({\n url: `${this.config.baseURL}/${getModelPath(\n this.modelId,\n )}:streamGenerateContent?alt=sse`,\n headers,\n body: args,\n failedResponseHandler: googleFailedResponseHandler,\n successfulResponseHandler: createEventSourceResponseHandler(chunkSchema),\n abortSignal: options.abortSignal,\n fetch: this.config.fetch,\n });\n\n let finishReason: LanguageModelV3FinishReason = {\n unified: 'other',\n raw: undefined,\n };\n let usage: GoogleGenerativeAIUsageMetadata | undefined = undefined;\n let providerMetadata: SharedV3ProviderMetadata | undefined = undefined;\n let lastGroundingMetadata: GroundingMetadataSchema | null = null;\n let lastUrlContextMetadata: UrlContextMetadataSchema | null = null;\n\n const generateId = this.config.generateId;\n let hasToolCalls = false;\n\n // Track active blocks to group consecutive parts of same type\n let currentTextBlockId: string | null = null;\n let currentReasoningBlockId: string | null = null;\n let blockCounter = 0;\n\n // Track emitted sources to prevent duplicates\n const emittedSourceUrls = new Set<string>();\n // Associates a code execution result with its preceding call.\n let lastCodeExecutionToolCallId: string | undefined;\n // Associates a server-side tool response with its preceding call (tool combination).\n let lastServerToolCallId: string | undefined;\n\n const activeStreamingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n accumulator: GoogleJSONAccumulator;\n providerMetadata?: SharedV3ProviderMetadata;\n }> = [];\n\n const finishActiveStreamingToolCall = (\n controller: TransformStreamDefaultController<LanguageModelV3StreamPart>,\n ) => {\n const active = activeStreamingToolCalls.pop();\n if (active == null) {\n return;\n }\n\n const { finalJSON, closingDelta } = active.accumulator.finalize();\n\n if (closingDelta.length > 0) {\n controller.enqueue({\n type: 'tool-input-delta',\n id: active.toolCallId,\n delta: closingDelta,\n providerMetadata: active.providerMetadata,\n });\n }\n\n controller.enqueue({\n type: 'tool-input-end',\n id: active.toolCallId,\n providerMetadata: active.providerMetadata,\n });\n\n controller.enqueue({\n type: 'tool-call',\n toolCallId: active.toolCallId,\n toolName: active.toolName,\n input: finalJSON,\n providerMetadata: active.providerMetadata,\n });\n\n hasToolCalls = true;\n };\n\n return {\n stream: response.pipeThrough(\n new TransformStream<\n ParseResult<ChunkSchema>,\n LanguageModelV3StreamPart\n >({\n start(controller) {\n controller.enqueue({ type: 'stream-start', warnings });\n },\n\n transform(chunk, controller) {\n if (options.includeRawChunks) {\n controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });\n }\n\n if (!chunk.success) {\n controller.enqueue({ type: 'error', error: chunk.error });\n return;\n }\n\n const value = chunk.value;\n\n const usageMetadata = value.usageMetadata;\n\n if (usageMetadata != null) {\n usage = usageMetadata;\n }\n\n const candidate = value.candidates?.[0];\n\n // sometimes the API returns an empty candidates array\n if (candidate == null) {\n return;\n }\n\n const content = candidate.content;\n\n if (candidate.groundingMetadata != null) {\n lastGroundingMetadata = candidate.groundingMetadata;\n }\n if (candidate.urlContextMetadata != null) {\n lastUrlContextMetadata = candidate.urlContextMetadata;\n }\n\n const sources = extractSources({\n groundingMetadata: candidate.groundingMetadata,\n generateId,\n });\n if (sources != null) {\n for (const source of sources) {\n if (\n source.sourceType === 'url' &&\n !emittedSourceUrls.has(source.url)\n ) {\n emittedSourceUrls.add(source.url);\n controller.enqueue(source);\n }\n }\n }\n\n // Process tool call's parts before determining finishReason to ensure hasToolCalls is properly set\n if (content != null) {\n // Process all parts in a single loop to preserve original order\n const parts = content.parts ?? [];\n for (const part of parts) {\n if ('executableCode' in part && part.executableCode?.code) {\n const toolCallId = generateId();\n lastCodeExecutionToolCallId = toolCallId;\n\n controller.enqueue({\n type: 'tool-call',\n toolCallId,\n toolName: 'code_execution',\n input: JSON.stringify(part.executableCode),\n providerExecuted: true,\n });\n } else if (\n 'codeExecutionResult' in part &&\n part.codeExecutionResult\n ) {\n // Assumes a result directly follows its corresponding call part.\n const toolCallId = lastCodeExecutionToolCallId;\n\n if (toolCallId) {\n controller.enqueue({\n type: 'tool-result',\n toolCallId,\n toolName: 'code_execution',\n result: {\n outcome: part.codeExecutionResult.outcome,\n output: part.codeExecutionResult.output ?? '',\n },\n });\n // Clear the ID after use.\n lastCodeExecutionToolCallId = undefined;\n }\n } else if ('text' in part && part.text != null) {\n const thoughtSignatureMetadata = part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n },\n }\n : undefined;\n\n if (part.text.length === 0) {\n if (\n thoughtSignatureMetadata != null &&\n currentTextBlockId !== null\n ) {\n controller.enqueue({\n type: 'text-delta',\n id: currentTextBlockId,\n delta: '',\n providerMetadata: thoughtSignatureMetadata,\n });\n }\n } else if (part.thought === true) {\n // End any active text block before starting reasoning\n if (currentTextBlockId !== null) {\n controller.enqueue({\n type: 'text-end',\n id: currentTextBlockId,\n });\n currentTextBlockId = null;\n }\n\n // Start new reasoning block if not already active\n if (currentReasoningBlockId === null) {\n currentReasoningBlockId = String(blockCounter++);\n controller.enqueue({\n type: 'reasoning-start',\n id: currentReasoningBlockId,\n providerMetadata: thoughtSignatureMetadata,\n });\n }\n\n controller.enqueue({\n type: 'reasoning-delta',\n id: currentReasoningBlockId,\n delta: part.text,\n providerMetadata: thoughtSignatureMetadata,\n });\n } else {\n if (currentReasoningBlockId !== null) {\n controller.enqueue({\n type: 'reasoning-end',\n id: currentReasoningBlockId,\n });\n currentReasoningBlockId = null;\n }\n\n if (currentTextBlockId === null) {\n currentTextBlockId = String(blockCounter++);\n controller.enqueue({\n type: 'text-start',\n id: currentTextBlockId,\n providerMetadata: thoughtSignatureMetadata,\n });\n }\n\n controller.enqueue({\n type: 'text-delta',\n id: currentTextBlockId,\n delta: part.text,\n providerMetadata: thoughtSignatureMetadata,\n });\n }\n } else if ('inlineData' in part) {\n // End any active text or reasoning block before starting file output.\n // Relevant for multimodal output models.\n if (currentTextBlockId !== null) {\n controller.enqueue({\n type: 'text-end',\n id: currentTextBlockId,\n });\n currentTextBlockId = null;\n }\n if (currentReasoningBlockId !== null) {\n controller.enqueue({\n type: 'reasoning-end',\n id: currentReasoningBlockId,\n });\n currentReasoningBlockId = null;\n }\n\n const hasThought = part.thought === true;\n const hasThoughtSignature = !!part.thoughtSignature;\n const fileMeta =\n hasThought || hasThoughtSignature\n ? {\n [providerOptionsName]: {\n ...(hasThought ? { thought: true } : {}),\n ...(hasThoughtSignature\n ? { thoughtSignature: part.thoughtSignature }\n : {}),\n },\n }\n : undefined;\n controller.enqueue({\n type: 'file',\n mediaType: part.inlineData.mimeType,\n data: part.inlineData.data,\n providerMetadata: fileMeta,\n });\n } else if ('toolCall' in part && part.toolCall) {\n const toolCallId = part.toolCall.id ?? generateId();\n lastServerToolCallId = toolCallId;\n const serverMeta = {\n [providerOptionsName]: {\n ...(part.thoughtSignature\n ? { thoughtSignature: part.thoughtSignature }\n : {}),\n serverToolCallId: toolCallId,\n serverToolType: part.toolCall.toolType,\n },\n };\n\n controller.enqueue({\n type: 'tool-call',\n toolCallId,\n toolName: `server:${part.toolCall.toolType}`,\n input: JSON.stringify(part.toolCall.args ?? {}),\n providerExecuted: true,\n dynamic: true,\n providerMetadata: serverMeta,\n });\n } else if ('toolResponse' in part && part.toolResponse) {\n const responseToolCallId =\n lastServerToolCallId ??\n part.toolResponse.id ??\n generateId();\n const serverMeta = {\n [providerOptionsName]: {\n ...(part.thoughtSignature\n ? { thoughtSignature: part.thoughtSignature }\n : {}),\n serverToolCallId: responseToolCallId,\n serverToolType: part.toolResponse.toolType,\n },\n };\n\n controller.enqueue({\n type: 'tool-result',\n toolCallId: responseToolCallId,\n toolName: `server:${part.toolResponse.toolType}`,\n result: (part.toolResponse.response ?? {}) as JSONObject,\n providerMetadata: serverMeta,\n });\n lastServerToolCallId = undefined;\n }\n }\n\n // Handle streaming and complete function calls\n for (const part of parts) {\n if (!('functionCall' in part)) continue;\n\n const providerMeta = part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n },\n }\n : undefined;\n\n const isStreamingChunk =\n part.functionCall.partialArgs != null ||\n (part.functionCall.name != null &&\n part.functionCall.willContinue === true);\n const isTerminalChunk =\n part.functionCall.name == null &&\n part.functionCall.args == null &&\n part.functionCall.partialArgs == null &&\n part.functionCall.willContinue == null;\n const isCompleteCall =\n part.functionCall.name != null &&\n part.functionCall.args != null &&\n part.functionCall.partialArgs == null;\n // Single-chunk no-args call: `{ name: 'X' }` with no `args`,\n // `partialArgs`, or `willContinue`. Carries `thoughtSignature`.\n const isNoArgsCompleteCall =\n part.functionCall.name != null &&\n part.functionCall.args == null &&\n part.functionCall.partialArgs == null &&\n part.functionCall.willContinue !== true;\n\n if (isStreamingChunk) {\n if (part.functionCall.name != null) {\n const toolCallId = part.functionCall.id ?? generateId();\n const accumulator = new GoogleJSONAccumulator();\n activeStreamingToolCalls.push({\n toolCallId,\n toolName: part.functionCall.name,\n accumulator,\n providerMetadata: providerMeta,\n });\n\n controller.enqueue({\n type: 'tool-input-start',\n id: toolCallId,\n toolName: part.functionCall.name,\n providerMetadata: providerMeta,\n });\n\n if (part.functionCall.partialArgs != null) {\n const partialArgs = part.functionCall\n .partialArgs as PartialArg[];\n const { textDelta } =\n accumulator.processPartialArgs(partialArgs);\n if (textDelta.length > 0) {\n controller.enqueue({\n type: 'tool-input-delta',\n id: toolCallId,\n delta: textDelta,\n providerMetadata: providerMeta,\n });\n }\n if (\n part.functionCall.willContinue !== true &&\n partialArgs.every(arg => arg.willContinue !== true)\n ) {\n finishActiveStreamingToolCall(controller);\n }\n }\n } else if (\n part.functionCall.partialArgs != null &&\n activeStreamingToolCalls.length > 0\n ) {\n const active =\n activeStreamingToolCalls[\n activeStreamingToolCalls.length - 1\n ];\n const partialArgs = part.functionCall\n .partialArgs as PartialArg[];\n const { textDelta } =\n active.accumulator.processPartialArgs(partialArgs);\n if (textDelta.length > 0) {\n controller.enqueue({\n type: 'tool-input-delta',\n id: active.toolCallId,\n delta: textDelta,\n providerMetadata: providerMeta,\n });\n }\n if (\n part.functionCall.willContinue !== true &&\n partialArgs.every(arg => arg.willContinue !== true)\n ) {\n finishActiveStreamingToolCall(controller);\n }\n }\n } else if (\n isTerminalChunk &&\n activeStreamingToolCalls.length > 0\n ) {\n finishActiveStreamingToolCall(controller);\n } else if (isCompleteCall) {\n const toolCallId = part.functionCall.id ?? generateId();\n const toolName = part.functionCall.name!;\n const args =\n typeof part.functionCall.args === 'string'\n ? part.functionCall.args\n : JSON.stringify(part.functionCall.args ?? {});\n\n controller.enqueue({\n type: 'tool-input-start',\n id: toolCallId,\n toolName,\n providerMetadata: providerMeta,\n });\n\n controller.enqueue({\n type: 'tool-input-delta',\n id: toolCallId,\n delta: args,\n providerMetadata: providerMeta,\n });\n\n controller.enqueue({\n type: 'tool-input-end',\n id: toolCallId,\n providerMetadata: providerMeta,\n });\n\n controller.enqueue({\n type: 'tool-call',\n toolCallId,\n toolName,\n input: args,\n providerMetadata: providerMeta,\n });\n\n hasToolCalls = true;\n } else if (isNoArgsCompleteCall) {\n const toolCallId = part.functionCall.id ?? generateId();\n const toolName = part.functionCall.name!;\n\n controller.enqueue({\n type: 'tool-input-start',\n id: toolCallId,\n toolName,\n providerMetadata: providerMeta,\n });\n\n controller.enqueue({\n type: 'tool-input-end',\n id: toolCallId,\n providerMetadata: providerMeta,\n });\n\n controller.enqueue({\n type: 'tool-call',\n toolCallId,\n toolName,\n input: '{}',\n providerMetadata: providerMeta,\n });\n\n hasToolCalls = true;\n }\n }\n }\n\n if (candidate.finishReason != null) {\n finishReason = {\n unified: mapGoogleGenerativeAIFinishReason({\n finishReason: candidate.finishReason,\n hasToolCalls,\n }),\n raw: candidate.finishReason,\n };\n\n providerMetadata = {\n [providerOptionsName]: {\n promptFeedback: value.promptFeedback ?? null,\n groundingMetadata: lastGroundingMetadata,\n urlContextMetadata: lastUrlContextMetadata,\n safetyRatings: candidate.safetyRatings ?? null,\n usageMetadata: usageMetadata ?? null,\n finishMessage: candidate.finishMessage ?? null,\n serviceTier: usage?.serviceTier ?? null,\n } satisfies GoogleGenerativeAIProviderMetadata,\n };\n }\n },\n\n flush(controller) {\n if (currentTextBlockId !== null) {\n controller.enqueue({\n type: 'text-end',\n id: currentTextBlockId,\n });\n }\n if (currentReasoningBlockId !== null) {\n controller.enqueue({\n type: 'reasoning-end',\n id: currentReasoningBlockId,\n });\n }\n\n controller.enqueue({\n type: 'finish',\n finishReason,\n usage: convertGoogleGenerativeAIUsage(usage),\n providerMetadata,\n });\n },\n }),\n ),\n response: { headers: responseHeaders },\n request: { body: args },\n };\n }\n}\n\nfunction getToolCallsFromParts({\n parts,\n generateId,\n providerOptionsName,\n}: {\n parts: ContentSchema['parts'];\n generateId: () => string;\n providerOptionsName: string;\n}) {\n const functionCallParts = parts?.filter(\n part => 'functionCall' in part,\n ) as Array<\n GoogleGenerativeAIContentPart & {\n functionCall: { name: string; args: unknown };\n thoughtSignature?: string | null;\n }\n >;\n\n return functionCallParts == null || functionCallParts.length === 0\n ? undefined\n : functionCallParts.map(part => ({\n type: 'tool-call' as const,\n toolCallId: generateId(),\n toolName: part.functionCall.name,\n args: JSON.stringify(part.functionCall.args),\n providerMetadata: part.thoughtSignature\n ? {\n [providerOptionsName]: {\n thoughtSignature: part.thoughtSignature,\n },\n }\n : undefined,\n }));\n}\n\nfunction extractSources({\n groundingMetadata,\n generateId,\n}: {\n groundingMetadata: GroundingMetadataSchema | undefined | null;\n generateId: () => string;\n}): undefined | LanguageModelV3Source[] {\n if (!groundingMetadata?.groundingChunks) {\n return undefined;\n }\n\n const sources: LanguageModelV3Source[] = [];\n\n for (const chunk of groundingMetadata.groundingChunks) {\n if (chunk.web != null) {\n // Handle web chunks as URL sources\n sources.push({\n type: 'source',\n sourceType: 'url',\n id: generateId(),\n url: chunk.web.uri,\n title: chunk.web.title ?? undefined,\n });\n } else if (chunk.image != null) {\n // Handle image chunks as image sources\n sources.push({\n type: 'source',\n sourceType: 'url',\n id: generateId(),\n // Google requires attribution to the source URI, not the actual image URI.\n // TODO: add another type in v7 to allow both the image and source URL to be included separately\n url: chunk.image.sourceUri,\n title: chunk.image.title ?? undefined,\n });\n } else if (chunk.retrievedContext != null) {\n // Handle retrievedContext chunks from RAG operations\n const uri = chunk.retrievedContext.uri;\n const fileSearchStore = chunk.retrievedContext.fileSearchStore;\n\n if (uri && (uri.startsWith('http://') || uri.startsWith('https://'))) {\n // Old format: Google Search with HTTP/HTTPS URL\n sources.push({\n type: 'source',\n sourceType: 'url',\n id: generateId(),\n url: uri,\n title: chunk.retrievedContext.title ?? undefined,\n });\n } else if (uri) {\n // Old format: Document with file path (gs://, etc.)\n const title = chunk.retrievedContext.title ?? 'Unknown Document';\n let mediaType = 'application/octet-stream';\n let filename: string | undefined = undefined;\n\n if (uri.endsWith('.pdf')) {\n mediaType = 'application/pdf';\n filename = uri.split('/').pop();\n } else if (uri.endsWith('.txt')) {\n mediaType = 'text/plain';\n filename = uri.split('/').pop();\n } else if (uri.endsWith('.docx')) {\n mediaType =\n 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';\n filename = uri.split('/').pop();\n } else if (uri.endsWith('.doc')) {\n mediaType = 'application/msword';\n filename = uri.split('/').pop();\n } else if (uri.match(/\\.(md|markdown)$/)) {\n mediaType = 'text/markdown';\n filename = uri.split('/').pop();\n } else {\n filename = uri.split('/').pop();\n }\n\n sources.push({\n type: 'source',\n sourceType: 'document',\n id: generateId(),\n mediaType,\n title,\n filename,\n });\n } else if (fileSearchStore) {\n // New format: File Search with fileSearchStore (no uri)\n const title = chunk.retrievedContext.title ?? 'Unknown Document';\n sources.push({\n type: 'source',\n sourceType: 'document',\n id: generateId(),\n mediaType: 'application/octet-stream',\n title,\n filename: fileSearchStore.split('/').pop(),\n });\n }\n } else if (chunk.maps != null) {\n if (chunk.maps.uri) {\n sources.push({\n type: 'source',\n sourceType: 'url',\n id: generateId(),\n url: chunk.maps.uri,\n title: chunk.maps.title ?? undefined,\n });\n }\n }\n }\n\n return sources.length > 0 ? sources : undefined;\n}\n\nexport const getGroundingMetadataSchema = () =>\n z.object({\n webSearchQueries: z.array(z.string()).nullish(),\n imageSearchQueries: z.array(z.string()).nullish(),\n retrievalQueries: z.array(z.string()).nullish(),\n searchEntryPoint: z.object({ renderedContent: z.string() }).nullish(),\n groundingChunks: z\n .array(\n z.object({\n web: z\n .object({ uri: z.string(), title: z.string().nullish() })\n .nullish(),\n image: z\n .object({\n sourceUri: z.string(),\n imageUri: z.string(),\n title: z.string().nullish(),\n domain: z.string().nullish(),\n })\n .nullish(),\n retrievedContext: z\n .object({\n uri: z.string().nullish(),\n title: z.string().nullish(),\n text: z.string().nullish(),\n fileSearchStore: z.string().nullish(),\n })\n .nullish(),\n maps: z\n .object({\n uri: z.string().nullish(),\n title: z.string().nullish(),\n text: z.string().nullish(),\n placeId: z.string().nullish(),\n })\n .nullish(),\n }),\n )\n .nullish(),\n groundingSupports: z\n .array(\n z.object({\n segment: z\n .object({\n startIndex: z.number().nullish(),\n endIndex: z.number().nullish(),\n text: z.string().nullish(),\n })\n .nullish(),\n segment_text: z.string().nullish(),\n groundingChunkIndices: z.array(z.number()).nullish(),\n supportChunkIndices: z.array(z.number()).nullish(),\n confidenceScores: z.array(z.number()).nullish(),\n confidenceScore: z.array(z.number()).nullish(),\n }),\n )\n .nullish(),\n retrievalMetadata: z\n .union([\n z.object({\n webDynamicRetrievalScore: z.number(),\n }),\n z.object({}),\n ])\n .nullish(),\n });\n\nconst partialArgSchema = z.object({\n jsonPath: z.string(),\n stringValue: z.string().nullish(),\n numberValue: z.number().nullish(),\n boolValue: z.boolean().nullish(),\n nullValue: z.unknown().nullish(),\n willContinue: z.boolean().nullish(),\n});\n\nconst getContentSchema = () =>\n z.object({\n parts: z\n .array(\n z.union([\n // note: order matters since text can be fully empty\n z.object({\n functionCall: z.object({\n id: z.string().nullish(),\n name: z.string().nullish(),\n args: z.unknown().nullish(),\n partialArgs: z.array(partialArgSchema).nullish(),\n willContinue: z.boolean().nullish(),\n }),\n thoughtSignature: z.string().nullish(),\n }),\n z.object({\n inlineData: z.object({\n mimeType: z.string(),\n data: z.string(),\n }),\n thought: z.boolean().nullish(),\n thoughtSignature: z.string().nullish(),\n }),\n z.object({\n toolCall: z.object({\n toolType: z.string(),\n args: z.unknown().nullish(),\n id: z.string(),\n }),\n thoughtSignature: z.string().nullish(),\n }),\n z.object({\n toolResponse: z.object({\n toolType: z.string(),\n response: z.unknown().nullish(),\n id: z.string(),\n }),\n thoughtSignature: z.string().nullish(),\n }),\n z.object({\n executableCode: z\n .object({\n language: z.string(),\n code: z.string(),\n })\n .nullish(),\n codeExecutionResult: z\n .object({\n outcome: z.string(),\n output: z.string().nullish(),\n })\n .nullish(),\n text: z.string().nullish(),\n thought: z.boolean().nullish(),\n thoughtSignature: z.string().nullish(),\n }),\n ]),\n )\n .nullish(),\n });\n\n// https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-filters\nconst getSafetyRatingSchema = () =>\n z.object({\n category: z.string().nullish(),\n probability: z.string().nullish(),\n probabilityScore: z.number().nullish(),\n severity: z.string().nullish(),\n severityScore: z.number().nullish(),\n blocked: z.boolean().nullish(),\n });\n\nconst tokenDetailsSchema = z\n .array(\n z.object({\n modality: z.string(),\n tokenCount: z.number(),\n }),\n )\n .nullish();\n\nconst usageSchema = z.object({\n cachedContentTokenCount: z.number().nullish(),\n thoughtsTokenCount: z.number().nul