Skip to content

fix: sanitize agent names for OpenAI models#2347

Closed
RachitJava wants to merge 1 commit into
agentscope-ai:mainfrom
RachitJava:fix-agent-name-openai
Closed

fix: sanitize agent names for OpenAI models#2347
RachitJava wants to merge 1 commit into
agentscope-ai:mainfrom
RachitJava:fix-agent-name-openai

Conversation

@RachitJava

Copy link
Copy Markdown

AgentScope-Java Version

[The version of AgentScope-Java you are working on, e.g. 1.0.12, check your pom.xml dependency version or run mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]

Description

[Please describe the background, purpose, changes made, and how to test this PR]

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@RachitJava

Copy link
Copy Markdown
Author

Problem
The OpenAI Chat Completions API enforces strict validation on the name field of messages, requiring it to match the regex pattern ^[a-zA-Z0-9_-]+$.
If an agent is initialized in AgentScope with a display name that contains spaces or special characters (e.g. "My Agent"), sending this name directly in the message payload causes the OpenAI API to return a 400 Bad Request error.
This causes the entire agent execution to fail unexpectedly when interacting with OpenAI models.

Solution
Added a sanitizeName helper method in OpenAIMessageConverter.java that replaces any characters outside of [a-zA-Z0-9_-] with an underscore (_).
Updated convertUserMessage and convertAssistantMessage to pass the msg.getName() through the sanitizeName() method before assigning it to the OpenAIMessage.Builder.
Added a new unit test testMessageWithSanitizedName in OpenAIMessageConverterTest.java to verify that spaces and special characters in agent names are safely escaped.

@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...model/openai/formatter/OpenAIMessageConverter.java 60.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@oss-maintainer

Copy link
Copy Markdown
Collaborator

CLA Not Signed

The Contributor License Agreement (CLA) check is currently pending on this PR (license/cla: Contributor License Agreement is not signed yet.). This PR cannot be merged until the CLA is signed.

@RachitJava please sign the CLA via the CLA assistant badge in the comment above, or visit https://cla-assistant.io/agentscope-ai/agentscope-java. Once signed, the license/cla status will turn green.


Automated check by github-manager-bot

@jujn

jujn commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

fixed in: #2346

@jujn jujn closed this Jul 23, 2026
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.

4 participants