spring-projects / spring-projects/spring-ai

Support the Gemini code execution tool in `GoogleGenAiChatModel`

Open
#6,471 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
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:

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 code
  • executableCodeLanguage — e.g. PYTHON
  • codeExecutionResult — the execution output
  • codeExecutionOutcome — 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, matching googleSearchRetrieval. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.