Skip to content

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

Description

@itjuba

Description

An agent whose name contains a space (or any of < | \ / >) cannot complete a single run on the Chat Completions path: the framework stamps the display name onto message.author_name, and OpenAIChatCompletionClient forwards it verbatim as the request-message name field. OpenAI validates that field against ^[^\s<|\\/>]+$, so every request 400s.

The forwarding was added by #2951 ("Support OpenAI message 'name' field"), whose discussion explicitly quotes the .NET implementation's SanitizeAuthorName — but the Python implementation shipped without the sanitization step. The .NET side hit the identical bug (#1195) and fixed it in dotnet/extensions#6827 by sanitizing; the Python port still sends the raw name from three sites in agent_framework_openai/_chat_completion_client.py (_prepare_message_for_openai: the system/developer path, the general path, and the pending-args path — main @ c35a63e lines ~883, ~898, ~955), with no validation anywhere in the file.

  • Expected: a human-readable agent name like "My Agent" works; the wire name is sanitized to the provider's charset (as .NET does).
  • Actual: openai.BadRequestError 400 on every request from that agent.

Suggested fix: port .NET's SanitizeAuthorName — replace characters matching [\s<|\\/>] before setting name, at all three assignment sites.

Code Sample

Serialization-only repro (no network):

from agent_framework import Content, Message
from agent_framework_openai import OpenAIChatCompletionClient

client = OpenAIChatCompletionClient(model="gpt-4o", api_key="sk-not-used")

msg = Message(role="assistant", contents=[Content.from_text("hello")], author_name="My Agent")
print(client._prepare_message_for_openai(msg))

Output — name goes out with the space, which the API rejects:

[{'role': 'assistant', 'name': 'My Agent', 'content': 'hello'}]

End-to-end, the same happens for any Agent(name="My Agent", ...) backed by OpenAIChatCompletionClient, since the agent's display name is stamped onto author_name on every response message.

Error Messages / Stack Traces

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid 'messages[1].name': string does not match pattern. Expected a string that matches the pattern '^[^\\s<|\\\\/>]+$'.", 'type': 'invalid_request_error', 'param': 'messages[1].name', 'code': 'invalid_value'}}

Package Versions

agent-framework-core: 1.10.0 (same behavior at 1.11.0), agent-framework-openai: 1.10.0 (verified identical in 1.10.1, the latest release; still present on main @ c35a63e)

Python Version

Python 3.12

Additional Context

Metadata

Metadata

Assignees

Labels

agentsUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions