Skip to content

fix(agentchat): sanitize auto-generated Handoff tool names - #8285

Open
Pujitha Paladugu (pujitha24) wants to merge 1 commit into
microsoft:mainfrom
pujitha24:auto/issue-8281
Open

Pujitha Paladugu (pujitha24) wants to merge 1 commit into
microsoft:mainfrom
pujitha24:auto/issue-8281

Conversation

@pujitha24

Copy link
Copy Markdown

Why are these changes needed?

Handoff validates a custom name with isidentifier(), but the auto-generated default transfer_to_{target} was not checked. A target like "Support Desk" produced transfer_to_support desk. Construction succeeded, then the first model call with the OpenAI client failed with ValueError: Invalid name (the client requires ^[a-zA-Z0-9_-]+$).

This PR replaces characters outside [a-zA-Z0-9_-] with _ in the default name only, so the example becomes transfer_to_support_desk.

  • target is unchanged, so routing works as before.
  • Custom names are still validated with isidentifier().
  • Default names that already matched ^[a-zA-Z0-9_-]+$ are unchanged, and hyphens are kept.

Limits:

  • Names over the 64-character limit are not handled. Only invalid characters are fixed.
  • Distinct non-Latin targets can now map to the same default name. AssistantAgent then raises its existing "Handoff names must be unique" error when the agent is built, not at call time.

Validation (run from python/):

  • uv run pytest packages/autogen-agentchat/tests/test_assistant_agent.py -k default_name_is_valid: the new test fails without the fix and passes with it. It checks that the handoff tool passes the OpenAI client's convert_tools.
  • uv run pytest packages/autogen-agentchat/tests -n auto: 377 passed, 5 skipped.
  • ruff format --check, ruff check, mypy and pyright on the changed files: clean.

This PR was written with the assistance of generative AI.

Related issue number

Closes #8281

Checks

🤖 Generated with Claude Code

Motivation
Handoff validates a custom `name` with isidentifier(), but the default
name `transfer_to_{target}` was used as-is. A target such as
"Support Desk" produced "transfer_to_support desk". Constructing the
Handoff and the AssistantAgent succeeded, but the first model call with
the OpenAI client raised `ValueError: Invalid name` from
assert_valid_name, which requires ^[a-zA-Z0-9_-]+$.

Approach
When the default name is generated, replace every character outside
[a-zA-Z0-9_-] with "_" (for example, transfer_to_support_desk). The
handoff `target` is not changed, so routing to the target agent works
as before. Custom names are still validated with isidentifier().
Default names that already matched ^[a-zA-Z0-9_-]+$ are unchanged, and
hyphens are kept.

Notes
- Only invalid characters are handled. A target long enough to push the
  name past the 64-character limit is still rejected by the OpenAI
  client, as before.
- Each non-ASCII character becomes "_", so distinct non-Latin targets
  can map to the same default name. AssistantAgent then raises its
  existing "Handoff names must be unique" error at construction. Such
  names were already rejected at call time by the OpenAI client.

Validation (run from python/)
- uv run pytest packages/autogen-agentchat/tests/test_assistant_agent.py -k default_name_is_valid
  The new test fails with the source change reverted and passes with it.
  It checks the sanitized name, that hyphens are kept, and that the
  handoff tool passes the OpenAI client's convert_tools.
- uv run pytest packages/autogen-agentchat/tests -n auto
  377 passed, 5 skipped.
- ruff format --check, ruff check, mypy and pyright on the two changed
  files: clean.

Report: microsoft#8281

Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Assisted-by: claude-opus-5-5 (via Claude Code)
@pujitha24

Copy link
Copy Markdown
Author

This is rebased and the checks are green on my side; let me know if anything would make it easier to review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handoff auto-name with spaces builds OK but fails in convert_tools / AssistantAgent.run

1 participant