-
Notifications
You must be signed in to change notification settings - Fork 3.8k
test(provider/google): add thoughtSignature cross-namespace regression tests #12576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dancer
wants to merge
3
commits into
main
Choose a base branch
from
josh/thoughtsignature-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+684
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
examples/ai-functions/src/generate-text/gateway-google-gemini-3-multiturn.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import type { GoogleLanguageModelOptions } from '@ai-sdk/google'; | ||
| import { generateText, stepCountIs, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| const model = 'google/gemini-3-pro-preview'; | ||
| const firstUserMessage = | ||
| 'Use the lookup_definition tool first for "eventual consistency", then answer in one sentence. USE THE GOOGLE PROVIDER!!'; | ||
|
|
||
| run(async () => { | ||
| const providerOptions = { | ||
| gateway: { | ||
| only: ['google'], | ||
| }, | ||
| vertex: { | ||
| safetySettings: [ | ||
| { | ||
| category: 'HARM_CATEGORY_DANGEROUS_CONTENT', | ||
| threshold: 'BLOCK_ONLY_HIGH', | ||
| }, | ||
| { | ||
| category: 'HARM_CATEGORY_HATE_SPEECH', | ||
| threshold: 'BLOCK_ONLY_HIGH', | ||
| }, | ||
| ], | ||
| } satisfies GoogleLanguageModelOptions, | ||
| }; | ||
| const tools = { | ||
| lookup_definition: tool({ | ||
| description: 'Returns a short definition for a systems concept.', | ||
| inputSchema: z.object({ | ||
| term: z.string(), | ||
| }), | ||
| execute: async ({ term }) => ({ | ||
| term, | ||
| definition: | ||
| 'Eventual consistency means replicas may temporarily differ, but converge to the same value after propagation.', | ||
| }), | ||
| }), | ||
| }; | ||
|
|
||
| const firstTurn = await generateText({ | ||
| model, | ||
| prompt: firstUserMessage, | ||
| tools, | ||
| stopWhen: stepCountIs(10), | ||
| providerOptions, | ||
| }); | ||
|
|
||
| console.log('Turn 1:', firstTurn.text); | ||
| console.log( | ||
| 'Turn 1 provider metadata:', | ||
| JSON.stringify(firstTurn.providerMetadata, null, 2), | ||
| ); | ||
| console.log(); | ||
|
|
||
| const secondTurn = await generateText({ | ||
| model, | ||
| messages: [ | ||
| { role: 'user', content: firstUserMessage }, | ||
| ...firstTurn.response.messages, | ||
| { | ||
| role: 'user', | ||
| content: | ||
| 'Use the lookup_definition tool again, then give a concrete 2-line example involving an inventory update.', | ||
| }, | ||
| ], | ||
| tools, | ||
| stopWhen: stepCountIs(10), | ||
| providerOptions, | ||
| }); | ||
|
|
||
| console.log('Turn 2:', secondTurn.text); | ||
| console.log( | ||
| 'Turn 2 provider metadata:', | ||
| JSON.stringify(secondTurn.providerMetadata, null, 2), | ||
| ); | ||
| }); | ||
49 changes: 49 additions & 0 deletions
49
examples/ai-functions/src/generate-text/gateway-google-thinking-roundtrip.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { gateway, generateText, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| run(async () => { | ||
| const result = await generateText({ | ||
| model: gateway('google/gemini-3-flash-preview'), | ||
| tools: { | ||
| weather: tool({ | ||
| description: 'Get the current weather in a given location', | ||
| inputSchema: z.object({ | ||
| location: z | ||
| .string() | ||
| .describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| execute: async ({ location }) => ({ | ||
| location, | ||
| temperature: 72, | ||
| unit: 'fahrenheit', | ||
| }), | ||
| }), | ||
| }, | ||
| stopWhen: result => result.steps.length >= 3, | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
|
|
||
| console.log('Text:', result.text); | ||
| console.log('Steps:', result.steps.length); | ||
|
|
||
| for (const step of result.steps) { | ||
| console.log('---'); | ||
| console.log('Step finish reason:', step.finishReason); | ||
| for (const content of step.content) { | ||
| if (content.type === 'tool-call') { | ||
| console.log('Tool call:', content.toolName, content.input); | ||
| console.log( | ||
| 'Provider metadata:', | ||
| JSON.stringify(content.providerMetadata), | ||
| ); | ||
| } | ||
| if (content.type === 'reasoning') { | ||
| console.log( | ||
| 'Reasoning metadata:', | ||
| JSON.stringify(content.providerMetadata), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }); |
47 changes: 47 additions & 0 deletions
47
examples/ai-functions/src/generate-text/google-thinking-roundtrip.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { createGoogleGenerativeAI } from '@ai-sdk/google'; | ||
| import { generateText, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| run(async () => { | ||
| const google = createGoogleGenerativeAI(); | ||
| const model = google('gemini-3-flash-preview'); | ||
|
|
||
| const result = await generateText({ | ||
| model, | ||
| tools: { | ||
| weather: tool({ | ||
| description: 'Get the current weather in a given location', | ||
| inputSchema: z.object({ | ||
| location: z | ||
| .string() | ||
| .describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| execute: async ({ location }) => ({ | ||
| location, | ||
| temperature: 72, | ||
| unit: 'fahrenheit', | ||
| }), | ||
| }), | ||
| }, | ||
| stopWhen: result => result.steps.length >= 3, | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
|
|
||
| console.log('Text:', result.text); | ||
| console.log('Steps:', result.steps.length); | ||
|
|
||
| for (const step of result.steps) { | ||
| console.log('---'); | ||
| console.log('Step finish reason:', step.finishReason); | ||
| for (const content of step.content) { | ||
| if (content.type === 'tool-call') { | ||
| console.log('Tool call:', content.toolName, content.input); | ||
| console.log( | ||
| 'Provider metadata:', | ||
| JSON.stringify(content.providerMetadata), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }); |
89 changes: 89 additions & 0 deletions
89
examples/ai-functions/src/stream-text/gateway-google-gemini-3-multiturn.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import type { GoogleLanguageModelOptions } from '@ai-sdk/google'; | ||
| import { stepCountIs, streamText, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| const model = 'google/gemini-3-flash'; | ||
| const firstUserMessage = | ||
| 'Use the lookup_definition tool first for "eventual consistency", then answer in one sentence.'; | ||
|
|
||
| run(async () => { | ||
| const providerOptions = { | ||
| gateway: { | ||
| only: ['google'], | ||
| }, | ||
| vertex: { | ||
| safetySettings: [ | ||
| { | ||
| category: 'HARM_CATEGORY_DANGEROUS_CONTENT', | ||
| threshold: 'BLOCK_ONLY_HIGH', | ||
| }, | ||
| { | ||
| category: 'HARM_CATEGORY_HATE_SPEECH', | ||
| threshold: 'BLOCK_ONLY_HIGH', | ||
| }, | ||
| ], | ||
| } satisfies GoogleLanguageModelOptions, | ||
| }; | ||
| const tools = { | ||
| lookup_definition: tool({ | ||
| description: 'Returns a short definition for a systems concept.', | ||
| inputSchema: z.object({ | ||
| term: z.string(), | ||
| }), | ||
| execute: async ({ term }) => ({ | ||
| term, | ||
| definition: | ||
| 'Eventual consistency means replicas may temporarily differ, but converge to the same value after propagation.', | ||
| }), | ||
| }), | ||
| }; | ||
|
|
||
| const firstTurn = streamText({ | ||
| model, | ||
| prompt: firstUserMessage, | ||
| tools, | ||
| stopWhen: stepCountIs(10), | ||
| providerOptions, | ||
| }); | ||
|
|
||
| let firstTurnText = ''; | ||
| process.stdout.write('Turn 1: '); | ||
| for await (const textPart of firstTurn.textStream) { | ||
| firstTurnText += textPart; | ||
| process.stdout.write(textPart); | ||
| } | ||
| process.stdout.write('\n'); | ||
| console.log( | ||
| 'Turn 1 provider metadata:', | ||
| JSON.stringify(await firstTurn.providerMetadata, null, 2), | ||
| ); | ||
| console.log(); | ||
| const firstTurnResponse = await firstTurn.response; | ||
|
|
||
| const secondTurn = streamText({ | ||
| model, | ||
| messages: [ | ||
| { role: 'user', content: firstUserMessage }, | ||
| ...firstTurnResponse.messages, | ||
| { | ||
| role: 'user', | ||
| content: | ||
| 'Use the lookup_definition tool again, then give a concrete 2-line example involving an inventory update.', | ||
| }, | ||
| ], | ||
| tools, | ||
| stopWhen: stepCountIs(10), | ||
| providerOptions, | ||
| }); | ||
|
|
||
| process.stdout.write('Turn 2: '); | ||
| for await (const textPart of secondTurn.textStream) { | ||
| process.stdout.write(textPart); | ||
| } | ||
| process.stdout.write('\n'); | ||
| console.log( | ||
| 'Turn 2 provider metadata:', | ||
| JSON.stringify(await secondTurn.providerMetadata, null, 2), | ||
| ); | ||
| }); |
58 changes: 58 additions & 0 deletions
58
examples/ai-functions/src/stream-text/gateway-google-thinking-roundtrip.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { gateway, streamText, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| run(async () => { | ||
| const result = streamText({ | ||
| model: gateway('google/gemini-3-flash-preview'), | ||
| tools: { | ||
| weather: tool({ | ||
| description: 'Get the current weather in a given location', | ||
| inputSchema: z.object({ | ||
| location: z | ||
| .string() | ||
| .describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| execute: async ({ location }) => ({ | ||
| location, | ||
| temperature: 72, | ||
| unit: 'fahrenheit', | ||
| }), | ||
| }), | ||
| }, | ||
| stopWhen: result => result.steps.length >= 3, | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
|
|
||
| for await (const chunk of result.fullStream) { | ||
| switch (chunk.type) { | ||
| case 'text-delta': | ||
| process.stdout.write(chunk.text); | ||
| break; | ||
| case 'reasoning-delta': | ||
| break; | ||
| case 'tool-call': | ||
| console.log('Tool call:', chunk.toolName, chunk.input); | ||
| break; | ||
| case 'tool-result': | ||
| console.log('Tool result:', JSON.stringify(chunk.output)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log('Steps:', (await result.steps).length); | ||
|
|
||
| for (const step of await result.steps) { | ||
| console.log('---'); | ||
| console.log('Step finish reason:', step.finishReason); | ||
| for (const content of step.content) { | ||
| if (content.type === 'tool-call') { | ||
| console.log( | ||
| 'Provider metadata:', | ||
| JSON.stringify(content.providerMetadata), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }); |
62 changes: 62 additions & 0 deletions
62
examples/ai-functions/src/stream-text/google-thinking-roundtrip.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { createGoogleGenerativeAI } from '@ai-sdk/google'; | ||
| import { streamText, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| run(async () => { | ||
| const google = createGoogleGenerativeAI(); | ||
| const model = google('gemini-3-flash-preview'); | ||
|
|
||
| const result = streamText({ | ||
| model, | ||
| tools: { | ||
| weather: tool({ | ||
| description: 'Get the current weather in a given location', | ||
| inputSchema: z.object({ | ||
| location: z | ||
| .string() | ||
| .describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| execute: async ({ location }) => ({ | ||
| location, | ||
| temperature: 72, | ||
| unit: 'fahrenheit', | ||
| }), | ||
| }), | ||
| }, | ||
| stopWhen: result => result.steps.length >= 3, | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
|
|
||
| for await (const chunk of result.fullStream) { | ||
| switch (chunk.type) { | ||
| case 'text-delta': | ||
| process.stdout.write(chunk.text); | ||
| break; | ||
| case 'reasoning-delta': | ||
| break; | ||
| case 'tool-call': | ||
| console.log('Tool call:', chunk.toolName, chunk.input); | ||
| break; | ||
| case 'tool-result': | ||
| console.log('Tool result:', JSON.stringify(chunk.output)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log('Steps:', (await result.steps).length); | ||
|
|
||
| for (const step of await result.steps) { | ||
| console.log('---'); | ||
| console.log('Step finish reason:', step.finishReason); | ||
| for (const content of step.content) { | ||
| if (content.type === 'tool-call') { | ||
| console.log( | ||
| 'Provider metadata:', | ||
| JSON.stringify(content.providerMetadata), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the combination of passing
vertexproviderOptions to the google provider fails passing those options. when you run this example, you will notice that no safety ratings are set.however passing
googleproviderOptions to vertex provider will work, and the safety ratings will be observed.Solution:
the conversion of the key from
vertextogoogleneeds to happen when gateway falls back to google provider from vertex