Skip to content

Python: Sanitize author_name for the Chat Completions message name field#7127

Open
itjuba wants to merge 1 commit into
microsoft:mainfrom
itjuba:fix/chat-completions-author-name-sanitize
Open

Python: Sanitize author_name for the Chat Completions message name field#7127
itjuba wants to merge 1 commit into
microsoft:mainfrom
itjuba:fix/chat-completions-author-name-sanitize

Conversation

@itjuba

@itjuba itjuba commented Jul 15, 2026

Copy link
Copy Markdown

Motivation & Context

OpenAI validates the Chat Completions request-message name against ^[^\s<|\\/>]+$, and _prepare_message_for_openai forwards message.author_name into that field verbatim. Since the framework stamps the agent's display name onto author_name, any agent whose name contains a space (e.g. Agent(name="My Agent")) fails every request on the Chat Completions path with:

Error code: 400 - Invalid 'messages[1].name': string does not match pattern.
Expected a string that matches the pattern '^[^\s<|\\/>]+$'.

The .NET client hit the identical problem (#1195) and solved it with SanitizeAuthorName (dotnet/extensions#6827); the Python implementation of the name forwarding (#2951) omitted that step.

Description & Review Guide

  • What are the major changes? Adds a _sanitize_author_name helper to _chat_completion_client.py, mirroring the .NET SanitizeAuthorName semantics: 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 three name assignment sites (system/developer path, the general per-content path, and the reasoning-only fallback).
  • What is the impact of these changes? Agents with human-readable display names work on the Chat Completions path instead of 400-ing. Names that are already valid pass through unchanged. The agent's display name itself is untouched — only the wire-level participant id is sanitized. The Responses client is unaffected (it does not send author_name).
  • What do you want reviewers to focus on? The sanitization semantics deliberately mirror the .NET client for cross-SDK consistency (remove invalid characters rather than replace, drop empty results, 64-char cap) — flag if Python should diverge from that.

Tests: test_prepare_message_sanitizes_author_name (space, invalid-only, truncation, and system-path cases) and test_prepare_message_keeps_valid_author_name in tests/openai/test_openai_chat_completion_client.py. Full test_openai_chat_completion_client* files pass locally; ruff check and ruff format --check clean.

Related Issue

Fixes #7126

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible

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>
Copilot AI review requested due to automatic review settings July 15, 2026 09:19

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.

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 name assignment 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.

@itjuba

itjuba commented Jul 15, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Python: [Bug]: agent display name is forwarded unsanitized as the Chat Completions message 'name' — any name with a space 400s every request

2 participants