From 524334d29fbc3222a11e2e2f1a4d344f23b91388 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Mon, 8 Jun 2026 12:40:08 +0800 Subject: [PATCH] fix: skip code regex without delimiters --- .../adk/code_executors/code_execution_utils.py | 3 +++ .../flows/llm_flows/test_code_execution.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/google/adk/code_executors/code_execution_utils.py b/src/google/adk/code_executors/code_execution_utils.py index 7cccce48be..4756f79993 100644 --- a/src/google/adk/code_executors/code_execution_utils.py +++ b/src/google/adk/code_executors/code_execution_utils.py @@ -145,6 +145,9 @@ def extract_code_and_truncate_content( first_text_part = copy.deepcopy(text_parts[0]) response_text = '\n'.join([p.text for p in text_parts]) + if not any(d[0] in response_text for d in code_block_delimiters): + return + # Find the first code block. leading_delimiter_pattern = '|'.join(d[0] for d in code_block_delimiters) trailing_delimiter_pattern = '|'.join(d[1] for d in code_block_delimiters) diff --git a/tests/unittests/flows/llm_flows/test_code_execution.py b/tests/unittests/flows/llm_flows/test_code_execution.py index e1a3de1cfb..04576d73e9 100644 --- a/tests/unittests/flows/llm_flows/test_code_execution.py +++ b/tests/unittests/flows/llm_flows/test_code_execution.py @@ -23,6 +23,7 @@ from google.adk.code_executors.base_code_executor import BaseCodeExecutor from google.adk.code_executors.built_in_code_executor import BuiltInCodeExecutor from google.adk.code_executors.code_execution_utils import CodeExecutionResult +from google.adk.code_executors.code_execution_utils import CodeExecutionUtils from google.adk.flows.llm_flows._code_execution import _DATA_FILE_HELPER_LIB from google.adk.flows.llm_flows._code_execution import response_processor from google.adk.models.llm_response import LlmResponse @@ -166,3 +167,16 @@ def test_data_file_helper_lib_defines_crop(): # Regression for #4011: explore_df raised NameError when crop was undefined. namespace['explore_df'](pd.DataFrame({'a': [1, 2], 'b': ['x', 'y']})) + + +def test_extract_code_skips_regex_when_no_code_delimiter(): + content = types.Content(parts=[types.Part(text='{"plot": "' + 'x' * 1000)]) + + with patch('google.adk.code_executors.code_execution_utils.re.compile') as re_compile: + result = CodeExecutionUtils.extract_code_and_truncate_content( + content, [('```python\n', '\n```')] + ) + + assert result is None + re_compile.assert_not_called() + assert len(content.parts) == 1