Skip to content

fix: transform_string_function_style silently transforms space-containing names without warning #2951

@kuishou68

Description

@kuishou68

Bug: transform_string_function_style silently transforms tool names with spaces without warning

Description

In src/agents/util/_transforms.py, the function transform_string_function_style has a logic bug: it replaces spaces with underscores before comparing to decide whether to emit a warning. This means tool names containing spaces are silently lowercased and space-replaced without triggering the warning.

Current behavior

def transform_string_function_style(name: str) -> str:
    name = name.replace(" ", "_")   # ← modifies `name` in-place
    transformed_name = re.sub(r"[^a-zA-Z0-9_]", "_", name)

    if transformed_name != name:    # ← compared against already-modified name
        logger.warning(...)         # ← never fires for space-only transformations

    return transformed_name.lower()

For a tool named "My Tool":

  1. name becomes "My_Tool"
  2. transformed_name becomes "My_Tool" (no further changes)
  3. transformed_name == nameno warning
  4. Returns "my_tool" — silently different from the original input "My Tool"

Expected behavior

The warning should fire whenever the final returned value differs from the original input. Names with spaces should trigger the same warning as names with other invalid characters.

Fix

Preserve the original name before any modifications:

def transform_string_function_style(name: str) -> str:
    original_name = name
    name = name.replace(" ", "_")
    transformed_name = re.sub(r"[^a-zA-Z0-9_]", "_", name)
    final_name = transformed_name.lower()

    if final_name != original_name:
        logger.warning(
            f"Tool name {original_name!r} contains invalid characters for function calling and has been "
            f"transformed to {final_name!r}. Please use only letters, digits, and underscores "
            "to avoid potential naming conflicts."
        )

    return final_name

Impact

Users defining tools with names containing spaces will silently get renamed without any log warning, making it harder to debug mismatched tool names.

Signed-off-by: Cocoon-Break 54054995+kuishou68@users.noreply.github.com

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions