Skip to content

Python: sanitize handoff tool names for agent names with spaces - #9053

Closed
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
microsoft:mainfrom
suhasagg:fix-9034-handoff-tool-name-spaces
Closed

Suhas Aggarwal (suhasagg) wants to merge 2 commits into
microsoft:mainfrom
suhasagg:fix-9034-handoff-tool-name-spaces

Conversation

@suhasagg

Copy link
Copy Markdown
Contributor

Summary

Fixes #9034.

Handoff tool names were generated directly from the target agent ID. When an agent name contained spaces or other characters that are invalid in provider function/tool names, the generated handoff tool name could also be invalid.

For example:

Billing Agent → handoff_to_Billing Agent

This change sanitizes non-alphanumeric/non-underscore characters when generating handoff tool names:

Billing Agent → handoff_to_Billing_Agent

The original target ID is preserved for handoff routing. Only the generated tool/function name is sanitized.

Changes

  • Sanitize invalid characters in get_handoff_tool_name().
  • Preserve the original target ID used by handoff routing.
  • Add regression coverage for spaces and punctuation.
  • Add an end-to-end middleware test verifying that a sanitized handoff tool still routes to the original target ID.

Testing

  • Added focused regression tests for sanitized handoff tool names.
  • Focused regression tests: 2 passed.
  • Full test_handoff.py: 42 passed, 6 skipped.
  • Ruff lint: passed.
  • Ruff format check: passed.
  • git diff --check: passed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Sanitization can cause routing collisions and unnecessarily rewrites valid hyphens.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

Sanitizes Python handoff tool names while preserving original agent IDs for routing.

Changes:

  • Replaces unsupported characters with underscores.
  • Adds regression and middleware-routing tests.
File Description
_handoff.py Adds handoff tool-name sanitization.
test_handoff.py Tests sanitization and original-ID routing.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/packages/orchestrations/agent_framework_orchestrations/_handoff.py Outdated
Comment thread python/packages/orchestrations/tests/test_handoff.py Outdated
@eavanvalkenburg

Copy link
Copy Markdown
Member

Suhas Aggarwal (@suhasagg) Thanks for the contribution. This implements the same change as #9035, so we'll continue the work there and close this duplicate.

This branch was previously deployed

1 inactive deployment
github-app-auth — 2ef54703 Deployed Oct 5, 2026 by suhasagg via add_label #24429
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Handoff tool names are invalid when an agent name contains spaces

3 participants