Python: Sanitize author_name for the Chat Completions message name field#7127
Open
itjuba wants to merge 1 commit into
Open
Python: Sanitize author_name for the Chat Completions message name field#7127itjuba wants to merge 1 commit into
itjuba wants to merge 1 commit into
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>
Contributor
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. |
Author
|
@microsoft-github-policy-service agree |
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.
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