Skip to content

Commit 993f997

Browse files
google-genai-botcopybara-github
authored andcommitted
Add new GeminiCodeExecutor to code_executors/ folder.
PiperOrigin-RevId: 757895291
1 parent 5462862 commit 993f997

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎src/google/adk/code_executors/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
from .base_code_executor import BaseCodeExecutor
1818
from .code_executor_context import CodeExecutorContext
19+
from .gemini_code_executor import GeminiCodeExecutor
1920
from .unsafe_local_code_executor import UnsafeLocalCodeExecutor
2021

2122
logger = logging.getLogger(__name__)
2223

2324
__all__ = [
2425
'BaseCodeExecutor',
2526
'CodeExecutorContext',
27+
'GeminiCodeExecutor',
2628
'UnsafeLocalCodeExecutor',
2729
]
2830

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.genai import types
16+
from pydantic import Field
17+
from typing_extensions import override
18+
19+
from ..agents.invocation_context import InvocationContext
20+
from ..models import LlmRequest
21+
from .base_code_executor import BaseCodeExecutor
22+
from .code_execution_utils import CodeExecutionInput
23+
from .code_execution_utils import CodeExecutionResult
24+
25+
26+
class GeminiCodeExecutor(BaseCodeExecutor):
27+
"""A code executor for Gemini 2.0+ models to exeute code."""
28+
29+
@override
30+
def execute_code(
31+
self,
32+
invocation_context: InvocationContext,
33+
code_execution_input: CodeExecutionInput,
34+
) -> CodeExecutionResult:
35+
pass
36+
37+
def process_llm_request(self, llm_request: LlmRequest) -> None:
38+
"""Pre-process the LLM request for Gemini 2.0+ models to use the code execution tool."""
39+
if llm_request.model and llm_request.model.startswith("gemini-2"):
40+
llm_request.config = llm_request.config or types.GenerateContentConfig()
41+
llm_request.config.tools = llm_request.config.tools or []
42+
llm_request.config.tools.append(
43+
types.Tool(code_execution=types.ToolCodeExecution())
44+
)
45+
return
46+
raise ValueError(
47+
"Gemini code execution tool is not supported for model"
48+
f" {llm_request.model}"
49+
)

‎src/google/adk/flows/llm_flows/_code_execution.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import os
2323
import re
2424
from typing import AsyncGenerator
25-
from typing import Generator
2625
from typing import Optional
2726
from typing import TYPE_CHECKING
2827

@@ -36,6 +35,7 @@
3635
from ...code_executors.code_execution_utils import CodeExecutionUtils
3736
from ...code_executors.code_execution_utils import File
3837
from ...code_executors.code_executor_context import CodeExecutorContext
38+
from ...code_executors.gemini_code_executor import GeminiCodeExecutor
3939
from ...events.event import Event
4040
from ...events.event_actions import EventActions
4141
from ...models.llm_response import LlmResponse
@@ -174,6 +174,11 @@ async def _run_pre_processor(
174174

175175
if not code_executor or not isinstance(code_executor, BaseCodeExecutor):
176176
return
177+
178+
if isinstance(code_executor, GeminiCodeExecutor):
179+
code_executor.process_llm_request(llm_request)
180+
return
181+
177182
if not code_executor.optimize_data_file:
178183
return
179184

@@ -262,6 +267,9 @@ async def _run_post_processor(
262267
if not llm_response or not llm_response.content:
263268
return
264269

270+
if isinstance(code_executor, GeminiCodeExecutor):
271+
return
272+
265273
code_executor_context = CodeExecutorContext(invocation_context.session.state)
266274
# Skip if the error count exceeds the max retry attempts.
267275
if (

0 commit comments

Comments
 (0)