Skip to content

Comments

Add codeExecution support to google providers and more safely handle provider and user tools#6354

Closed
Und3rf10w wants to merge 5 commits intovercel:mainfrom
Und3rf10w:fix/googleToolUsageWithProviderTools
Closed

Add codeExecution support to google providers and more safely handle provider and user tools#6354
Und3rf10w wants to merge 5 commits intovercel:mainfrom
Und3rf10w:fix/googleToolUsageWithProviderTools

Conversation

@Und3rf10w
Copy link
Contributor

@Und3rf10w Und3rf10w commented May 16, 2025

Background

The google and google-vertex providers would silently drop user defined tools when used in combination with useSearchGrounding. Using useSearchGrounding in combination with user defined tools is not supported. This change throws an error if that happens.

In addition, this change also adds support for the Code Execution feature in both the Google Generative AI and Vertex providers.

Summary

  • Throw an error when tools are used in conjunction with useSearchGrounding and useCodeExecution on the Google Generative AI and thus Vertex providers
  • Added logic to call the google defined codeExecution tool, and extract those as text parts.
  • Added documentation to both providers regarding the codeExecution feature.

Verification

I added examples for both the google and google-vertex providers and have tested the both of them.

I originally added tests for the code execution logic, but removed them in b75484b because apparently the test url doesn't support the 2.5 models.

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)

Future Work

I would more likely prefer that the executableCode part be treated as a tool-call, and the codeExecutionResultPartSchema be treated as the tool-result of that call, but i could not get that to function properly.

This is particularly difficult in a stream. The shape of both are the same regardless of whether it's being returned in a stream or in a generation.

Unit tests likely need to be added.

Related Issues

Fixes #6302
Fixes #6219

@Und3rf10w Und3rf10w marked this pull request as ready for review May 16, 2025 08:13
@lgrammel
Copy link
Collaborator

We have switched development to the v5 branch. Can you make the change against v5?

@Und3rf10w
Copy link
Contributor Author

We have switched development to the v5 branch. Can you make the change against v5?

@lgrammel, v5 doesn't seem to have the reasoning logic in it introduced in #6261. Do you want me to re-implement that in the updated PR as well?

@lgrammel
Copy link
Collaborator

@Und3rf10w i attempted a backport but ran into merge conflicts. if you could reimplement against v5 that would be excellent

@Und3rf10w
Copy link
Contributor Author

Looking at provider-defined-tool, would it make more sense to define the google provided tools as these?

If so, I just don't have a good understanding of how to convert the response to a tool result in the middle of the generation or stream.

@lgrammel
Copy link
Collaborator

code execution is more complex, i wanted to have a cleaner way in v5 but haven't yet gotten around to do it. you need to stream back tool results from the provider afaik

@Und3rf10w
Copy link
Contributor Author

Und3rf10w commented May 16, 2025

code execution is more complex, i wanted to have a cleaner way in v5 but haven't yet gotten around to do it. you need to stream back tool results from the provider afaik

Okay thanks. I'll opt for handling it as file and text parts for now. I'll get you that PR soonish

@Und3rf10w
Copy link
Contributor Author

Und3rf10w commented May 16, 2025

@lgrammel, I have opened #6365 in lieu of this PR. I'll add the docs shortly, but it's in a good state.

@Und3rf10w
Copy link
Contributor Author

Und3rf10w commented May 17, 2025

Closed in lieu of #6365

@Und3rf10w Und3rf10w closed this May 17, 2025
lgrammel pushed a commit that referenced this pull request Jul 30, 2025
## Background

This change was necessary to add support for the Code Execution feature
available in Google's Gemini models. This allows the model to generate
and execute Python code to perform calculations and solve problems,
enhancing its capabilities.

## Summary

- Added `codeExecution` tool: 
- A new tool, `google.tools.codeExecution()`, is now available. This
enables models that support it to run Python code.

## Verification

I manually verified the changes by setting up the new examples for
Google Vertex AI. I ran both the
`generate-text/google-vertex-code-execution.ts` and
`stream-text/google-vertex-code-execution.ts` examples against the
`gemini-2.5-pro model`.

In both cases, the model successfully used the code execution tool to
first calculate the 20th Fibonacci number and then find the nearest
palindrome. The console output correctly displayed the tool calls and
their corresponding results, followed by the final generated text,
confirming that the end-to-end flow is working as expected.

<img width="924" height="1013" alt="image"
src="https://github.com/user-attachments/assets/d70eea58-1fce-4bcf-873e-3b7f5981ac6c"
/>

## Related Issues

Closes #3205
Closes #6354
Closes #6365
@Und3rf10w Und3rf10w deleted the fix/googleToolUsageWithProviderTools branch August 12, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool calling not working with Gemini when useSearchGrounding is set to true Tool Calling with Search Grounding appears to not work (gemini-2.0-flash)

3 participants