Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Agents - Chat throwing "A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again." #10281

Closed
sophialagerkranspandey opened this issue Jan 23, 2025 · 3 comments
Assignees
Labels
agents python Pull requests for the Python Semantic Kernel sk team issue A tag to denote issues that where created by the Semantic Kernel team (i.e., not the community)

Comments

@sophialagerkranspandey
Copy link
Contributor

Discussed in #10271

Originally posted by yarns123 January 23, 2025
Hi,

im trying to use this example https://github.com/microsoft/semantic-kernel/blob/main/python/samples/getting_started_with_agents/step3_chat.py with AWS Bedrock Claude Sonnet 3.5. Unfortunately I get this error:
botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again.

It seems that ChatHistory for this model cannot have two consecutive responses from the assistant. I would be grateful for any ideas on how to solve this problem

@sophialagerkranspandey sophialagerkranspandey added .NET Issue or Pull requests regarding .NET code agents python Pull requests for the Python Semantic Kernel labels Jan 23, 2025
@github-actions github-actions bot changed the title Agents - Chat throwing "A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again." .Net: Agents - Chat throwing "A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again." Jan 23, 2025
@github-actions github-actions bot changed the title .Net: Agents - Chat throwing "A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again." Python: Agents - Chat throwing "A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again." Jan 23, 2025
@TaoChenOSU
Copy link
Contributor

Although AWS didn't provide the details on their docs, I was able to find this on Anthropic's API doc: https://docs.anthropic.com/en/api/messages#body-messages

Looks like the Anthropic models do require alternating user and assistant messages.

Could you please try another model that is available on AWS Bedrock?

@moonbox3 moonbox3 assigned TaoChenOSU and unassigned crickman Jan 25, 2025
@moonbox3 moonbox3 removed the .NET Issue or Pull requests regarding .NET code label Jan 25, 2025
@evchaki evchaki added the sk team issue A tag to denote issues that where created by the Semantic Kernel team (i.e., not the community) label Jan 30, 2025
@heidarj
Copy link

heidarj commented Feb 6, 2025

I have a similar problem when running smaller models locally with C#; when the last message isn't from a user the model refuses to answer and the response content is empty.

Example Requests and responses in an agent setting:

{
    "messages": [
        {
            "role": "system",
            "content": "You check the links from the writer to ensure they are valid using the provided tools.\r\nIf there are no links to check, simply respond with \u0022Everything OK\u0022.\r\n\r\nRULES:\r\n- Always check all links.\r\n- Never offer an explanation, only provide a summary of links and if they are valid or not.\r\n- Never address the user.",
            "name": "LinkChecker"
        },
        {
            "role": "user",
            "content": "Does azure offer video generative ai services?"
        },
        {
            "role": "assistant",
            "content": "As of my last update, Microsoft Azure offers various AI services that can be utilized for creating and managing video content, but there isn\u0027t a specific service labeled as \u0022video generative AI\u0022 like some standalone tools available from other providers. However, you can leverage several Azure AI capabilities to create or augment video content:\n\n1. **Azure Media Services**: This is a comprehensive media processing solution that supports tasks such as encoding and streaming of video content. It integrates with AI services to enhance video content through features like video indexing, OCR, and more.\n\n2. **Azure Cognitive Services**: These services provide pre-built APIs for various AI functionalities. You can use services such as:\n   - **Computer Vision**: For tasks like object detection within videos.\n   - **Face API**: To detect and analyze faces in video frames.\n   - **Speech-to-Text**: For converting spoken content to text, which is useful in transcribing video dialogues.\n\n3. **Azure Custom Vision** and **Azure Machine Learning**: These services allow you to build custom models for tasks like object detection or activity recognition within videos, enabling more tailored AI solutions.\n\n4. **Azure AI Video Indexer**: This service can automatically generate a transcript, detailed summary, and insights from video content using speech-to-text, text analytics, speaker identification, and more.\n\nFor more information on these services, you can visit the [Microsoft Azure Documentation](https://docs.microsoft.com/en-us/azure/) to explore specific features and capabilities relevant to video processing.",
            "name": "Writer"
        }
    ],
    "model": "phi-4",
    "tools": [
        {
            "type": "function",
            "function": {
                "description": "Fetches content from the provided URL.",
                "name": "WebAccess-FetchUrl",
                "parameters": {
                    "type": "object",
                    "required": [
                        "url"
                    ],
                    "properties": {
                        "url": {
                            "type": "string"
                        }
                    }
                },
                "strict": false
            }
        },
        {
            "type": "function",
            "function": {
                "description": "Validates that the link provided returns content.",
                "name": "WebAccess-IsValidLink",
                "parameters": {
                    "type": "object",
                    "required": [
                        "url"
                    ],
                    "properties": {
                        "url": {
                            "type": "string"
                        }
                    }
                },
                "strict": false
            }
        }
    ],
    "tool_choice": "auto"
}

response:

{
  "id": "chatcmpl-4tr8554gn9lguun22qu9hq",
  "object": "chat.completion",
  "created": 1738875733,
  "model": "phi-4",
  "choices": [
    {
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": ""
      }
    }
  ],
  "usage": {
    "prompt_tokens": 803,
    "completion_tokens": 0,
    "total_tokens": 803
  },
  "system_fingerprint": "phi-4"
}

@TaoChenOSU
Copy link
Contributor

This is a limitation of the Bedrock service. Please make sure your chat history alternates between user and assistant messages and the last message must be a user message, when using the Bedrock service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents python Pull requests for the Python Semantic Kernel sk team issue A tag to denote issues that where created by the Semantic Kernel team (i.e., not the community)
Projects
Status: No status
Development

No branches or pull requests

7 participants