Skip to content

Python: Bug: Agent name with space causes error #9340

@TaoChenOSU

Description

@TaoChenOSU

Describe the bug
When invoking a ChatCompletionAgent with a custom name that contains spaces and an OpenAI or Azure OAI service, the following exception is raised:

raise ServiceResponseException(
semantic_kernel.exceptions.service_exceptions.ServiceResponseException: ("<class 'semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion.AzureChatCompletion'> service failed to complete the prompt", BadRequestError('Error code: 400 - {\'error\': {\'message\': "Invalid \'messages[0].name\': string does not match pattern. Expected a string that matches the pattern \'^[a-zA-Z0-9_-]+$\'.", \'type\': \'invalid_request_error\', \'param\': \'messages[0].name\', \'code\': \'invalid_value\'}}'))

To Reproduce
Steps to reproduce the behavior:

import asyncio

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent
from semantic_kernel.contents.chat_history import ChatHistory

# A simple debate simulation with a moderator agent and two participants
kernel = Kernel()

kernel.add_service(AzureChatCompletion(service_id="azure"))

# Debate moderator agent
debate_moderator_instructions = """
You are a debate moderator agent that helps facilitate debates.
"""

debate_moderator = ChatCompletionAgent(
    service_id="azure",
    kernel=kernel,
    name="Debate Moderator",
    description="A debate moderator agent that helps facilitate debates.",
    instructions=debate_moderator_instructions,
)

chat_history = ChatHistory()
chat_history.add_user_message("Welcome to the debate! It's now officially started.")

async def debate_simulation():
    async for response in debate_moderator.invoke(chat_history):
        print(response)

if __name__ == "__main__":
    asyncio.run(debate_simulation())

Expected behavior
Agent returns a response

Platform

  • OS: Windows
  • IDE: VS Code
  • Language: Python
  • Source: Semantic Kernel 1.11.0

Additional context
The OpenAI docs doesn't mention the name of the message cannot contain spaces or what format to follow.

We should probably validate the model to catch the error earlier and give a more helpful error message.

Metadata

Metadata

Assignees

Labels

agentsbugSomething isn't workingpythonPull requests for the Python Semantic Kernelsk team issueA tag to denote issues that where created by the Semantic Kernel team (i.e., not the community)

Type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions