fix(agentchat): sanitize auto-generated Handoff tool names - #8285
Open
Pujitha Paladugu (pujitha24) wants to merge 1 commit into
Open
Pujitha Paladugu (pujitha24) wants to merge 1 commit into
Pujitha Paladugu (pujitha24) wants to merge 1 commit into
Conversation
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)
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
Handoffvalidates a customnamewithisidentifier(), but the auto-generated defaulttransfer_to_{target}was not checked. A target like"Support Desk"producedtransfer_to_support desk. Construction succeeded, then the first model call with the OpenAI client failed withValueError: 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 becomestransfer_to_support_desk.targetis unchanged, so routing works as before.isidentifier().^[a-zA-Z0-9_-]+$are unchanged, and hyphens are kept.Limits:
AssistantAgentthen 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'sconvert_tools.uv run pytest packages/autogen-agentchat/tests -n auto: 377 passed, 5 skipped.ruff format --check,ruff check,mypyandpyrighton the changed files: clean.This PR was written with the assistance of generative AI.
Related issue number
Closes #8281
Checks
🤖 Generated with Claude Code