Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/google/adk/flows/llm_flows/_code_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class DataFileUtil:
_DATA_FILE_HELPER_LIB = '''
import pandas as pd

def crop(s: str, max_chars: int = 64) -> str:
"""Crops a string to max_chars characters."""
return s[: max_chars - 3] + '...' if len(s) > max_chars else s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of crop has a bug when max_chars is less than 3. For example, if s = 'abc' and max_chars = 2, the function returns 'ab...', which has a length of 5, exceeding the specified max_chars.

To ensure the cropped string never exceeds max_chars, the logic should handle small values of max_chars as a special case, for instance by truncating the string without adding an ellipsis.

Suggested change
return s[: max_chars - 3] + '...' if len(s) > max_chars else s
if len(s) <= max_chars:
return s
if max_chars >= 3:
return s[:max_chars - 3] + '...'
return s[:max_chars]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! Fixed to handle small max_chars values.
b3b1382


def explore_df(df: pd.DataFrame) -> None:
"""Prints some information about a pandas DataFrame."""

Expand Down