Skip to content

[BUG]: NameError 'crop' is not defined when using optimize_data_file=True with CSV upload #4011

@osushinekotan

Description

@osushinekotan

Describe the bug
When optimize_data_file=True is set on a CodeExecutor and a CSV file is uploaded, ADK automatically generates preprocessing code that includes an explore_df() function. This function calls crop() which is not defined, causing a NameError.

The issue is in _DATA_FILE_HELPER_LIB in src/google/adk/flows/llm_flows/_code_execution.py (line 97):

df_unique = df.apply(lambda x: crop(str(list(x.unique()))))

The crop function is used but never defined in _DATA_FILE_HELPER_LIB.

To Reproduce

  1. Install google-adk==1.21.0
  2. Create an agent with a CodeExecutor that has optimize_data_file=True
  3. Upload a CSV file to the agent
  4. The agent will execute the auto-generated preprocessing code and fail with:
NameError: name 'crop' is not defined
Traceback (most recent call last):
  File "<stdin>", line 45
  File "<stdin>", line 23
  File "<stdin>", line 23
NameError: name 'crop' is not defined

Expected behavior
The crop function should be defined in _DATA_FILE_HELPER_LIB, or the call to crop() should be removed/replaced with an alternative.

Suggested fix
Add the crop function definition to _DATA_FILE_HELPER_LIB:

def crop(s: str, max_len: int = 100) -> str:
    """Truncate string to max_len characters."""
    return s[:max_len] + '...' if len(s) > max_len else s

Desktop (please complete the following information):

  • OS: macOS
  • Python version: 3.13 (sandbox)
  • ADK version: 1.21.0

Model Information:

  • Are you using LiteLLM: Yes
  • Which model is being used: Claude (via Bedrock)

Metadata

Metadata

Assignees

No one assigned

    Labels

    tools[Component] This issue is related to tools

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions