spring-projects / spring-projects/spring-ai
Support the Gemini code execution tool in `GoogleGenAiChatModel`
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
Summary
Add support for Gemini's built-in code execution tool to GoogleGenAiChatModel, letting the model generate and run Python code in a server-side sandbox and use the result in its response.
I'm opening this issue to align on direction before submitting a PR — see the Contribution section below.
Motivation
This adds the missing member of an already-supported capability rather than introducing a new concept: GoogleGenAiChatModel already exposes Gemini's Google Search built-in tool via googleSearchRetrieval(true), and code execution is a sibling in the same family. There is currently no way to enable it, and the code execution response parts are not surfaced.
Code execution is valuable wherever the model needs precise computation instead of estimation — arithmetic, data processing, or algorithmic tasks — where a plain LLM response is error-prone.
It is a standard, documented Gemini capability, and the Google Java SDK (com.google.genai) already provides the required types (ToolCodeExecution, ExecutableCode, CodeExecutionResult), so no new dependency is needed:
- Gemini API documentation: https://ai.google.dev/gemini-api/docs/code-execution
- Provided by Google's official SDKs for the Gemini API — the Java SDK types listed above are already on the classpath
The capability is supported on both the Gemini Developer API and Vertex AI.
Proposed API
For consistency, mirror the existing googleSearchRetrieval option:
ChatResponse response = chatModel.call(
new Prompt(
"What is the sum of the first 50 prime numbers?",
GoogleGenAiChatOptions.builder()
.model("gemini-2.5-flash")
.codeExecution(true)
.build()));
On the request side this adds Tool.builder().codeExecution(...), exactly as googleSearchRetrieval adds Tool.builder().googleSearch(...).
Response handling
Gemini returns the generated code and its result as response parts (executableCode, codeExecutionResult). These are surfaced on the response message metadata, consistent with how part-level information is exposed today:
executableCode— the generated codeexecutableCodeLanguage— e.g.PYTHONcodeExecutionResult— the execution outputcodeExecutionOutcome— e.g.OUTCOME_OK
Because a code execution response is returned as multiple parts, each part maps to a separate Generation (the code and result generations carry the metadata above; the final answer text is carried by a separate generation). This is consistent with the existing handling of multi-part responses.
Design considerations
- API shape: a boolean option on
GoogleGenAiChatOptions, matchinggoogleSearchRetrieval. Open to a different shape if you prefer. - Portability: code execution also exists for other providers, but is exposed differently — for example, the Anthropic integration adds it as a supporting tool for Skills rather than as a standalone option, so there is currently no provider-independent code-execution capability. Because providers model it differently, this follows the existing provider-specific built-in-tool pattern (matching
googleSearchRetrieval); happy to align with a portable abstraction later if that is preferred. - Combining with client-side function calling: this may be provider-constrained, since Gemini may not support both in a single request for all models.
Contribution
I'm actively working on this, following the existing googleSearchRetrieval pattern, with unit tests, an integration test, and reference documentation. I'd be glad to open a PR once the direction above is confirmed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with GoogleGenAiChatModel and GoogleGenAiChatOptions, using the existing googleSearchRetrieval option as the reference entry point. Inspect the current multi-part response handling and the com.google.genai ToolCodeExecution, ExecutableCode, and CodeExecutionResult types, then run the unit and integration tests mentioned in the issue. Done means code execution can be enabled, response parts expose the listed metadata, and reference documentation is updated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- google-cloud, java
- Domain
- ai, api, backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100