Python: Sanitize author_name for the Chat Completions message name field - #7127
Conversation
OpenAI validates the Chat Completions message 'name' against ^[^\s<|\/>]+$, so an agent display name containing a space (or < | \ / >) failed every request with a 400. Sanitize at the three assignment sites, mirroring SanitizeAuthorName in the .NET client (dotnet/extensions): remove characters outside [a-zA-Z0-9_], omit the name when nothing remains, truncate to 64 characters. Fixes microsoft#7126 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a Python OpenAI Chat Completions compatibility bug where Message.author_name was forwarded verbatim into the request-message name field, causing 400s for common agent display names (e.g., names containing spaces). It introduces a small sanitization helper aligned with the .NET SDK behavior so agent display names remain human-friendly while the wire name becomes API-compliant.
Changes:
- Added
_sanitize_author_name(strip to[a-zA-Z0-9_], omit if empty, truncate to 64 chars) in the Chat Completions client module. - Applied sanitization at each of the
nameassignment sites in_prepare_message_for_openai. - Added unit tests covering space removal, invalid-only omission, truncation, system-path handling, and pass-through for already-valid names.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/packages/openai/agent_framework_openai/_chat_completion_client.py | Introduces and applies author-name sanitization before setting Chat Completions message name. |
| python/packages/openai/tests/openai/test_openai_chat_completion_client.py | Adds regression and behavior tests for author-name sanitization and valid-name pass-through. |
|
@microsoft-github-policy-service agree |
|
@itjuba there is a merge conflict, please have a look! |
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
…s-author-name-sanitize # Conflicts: # python/packages/openai/agent_framework_openai/_chat_completion_client.py
|
@eavanvalkenburg conflict resolved in 7aa0433 — branch is now up to date with main (0 behind), no code changes. Workflows are pending approval to run. Could you kick them off and take another look? Thanks! |
Motivation & Context
OpenAI validates the Chat Completions request-message
nameagainst^[^\s<|\\/>]+$, and_prepare_message_for_openaiforwardsmessage.author_nameinto that field verbatim. Since the framework stamps the agent's display name ontoauthor_name, any agent whose name contains a space (e.g.Agent(name="My Agent")) fails every request on the Chat Completions path with:The .NET client hit the identical problem (#1195) and solved it with
SanitizeAuthorName(dotnet/extensions#6827); the Python implementation of thenameforwarding (#2951) omitted that step.Description & Review Guide
_sanitize_author_namehelper to_chat_completion_client.py, mirroring the .NETSanitizeAuthorNamesemantics: characters outside[a-zA-Z0-9_]are removed, the name is omitted entirely when nothing remains, and the result is truncated to 64 characters. Applied at all threenameassignment sites (system/developer path, the general per-content path, and the reasoning-only fallback).author_name).Tests:
test_prepare_message_sanitizes_author_name(space, invalid-only, truncation, and system-path cases) andtest_prepare_message_keeps_valid_author_nameintests/openai/test_openai_chat_completion_client.py. Fulltest_openai_chat_completion_client*files pass locally;ruff checkandruff format --checkclean.Related Issue
Fixes #7126
Contribution Checklist