The bot encountered an unhandled error: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Akshay Chile 0 Reputation points
2025-03-24T10:11:58.3+00:00

I have create Microsoft Teams App (Basic AI Chat Bot) project in Visual Studio 2022.

I am using Azure OpenAI.

I am getting below error when I ask question through my chat bot :

Error : The bot encountered an unhandled error: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index'). To continue to run this bot, please fix the bot source code.

I have given correct Azure OpenAI Key, Endpoint and Deployment model name, as I have tested another app with these keys.

Can some one guide how resolve this error ? or where can I locate Bot Source Code?

Below is my config:

appsettings.TestTool.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "BOT_ID": "",
  "BOT_PASSWORD": "",
  "Azure": {
    "OpenAIApiKey": "5d**************************************GecIK",
    "OpenAIEndpoint": "https://aksha-m88bx4tg-eastus2.openai.azure.com/",
    "OpenAIDeploymentName": "GPT4Model"
  }
}

appsettings.Development.json

{
"Logging": {
	"LogLevel": {
		"Default": "Information",
		"Microsoft": "Information",
		"Microsoft.Hosting.Lifetime": "Information",
		"Microsoft.Teams.AI": "Trace"
	}
},
"AllowedHosts": "*",
"BOT_ID": "a2*************************************0d0",
"BOT_PASSWORD": "mZ*************************************p7rb6b",
"BOT_TYPE": "MultiTenant",
"Azure": {
	"OpenAIApiKey": "5dMcH*************************************OGecIK",
	"OpenAIEndpoint": "https://aksha-m88bx4tg-eastus2.openai.azure.com/",
	"OpenAIDeploymentName": "GPT4Model"
}
}

Development.json has Bot Id and Bot Password.

I tried giving same in TestTool.json . As well as keeping it blank in TestTool.json.

But still I am getting same error as mentioned above.

I have watched many video tutorials. Created this POC 2-3 times. I have not missed any step. But still getting issue.

Below are steps I followed:

  1. Create new project in visual studio with 'Microsoft Teams App' Template
  2. Selected 'Basic AI Chat Bot' category with .Net 8 framework
  3. A window will open to provide Azure OpenAI Key,endpoint , deployment Model name
  4. Then once project is created, in the debug dropdown menu (down arrow near Start), select Dev Tunnels > Create A Tunnel (Temporary tunnel, set authentication type to Public)
  5. After that right click on TeamsApp -> Teams Toolkit -> Prepare Teams App Dependencies
  6. After successful completion of step 5 , Run application.
  7. Teams App Test Tool will open. Type some input and hit enter. Getting error message instead of response.
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,843 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Prasad-MSFT 8,846 Reputation points Microsoft External Staff
    2025-03-25T06:20:42.4233333+00:00

    The error message you are encountering (Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')) indicates that your bot code is trying to access an element in a collection (e.g., an array, list, or string) using an invalid index. This is a runtime error that needs to be fixed in the bot's source code. Could you please check below document and let us know if it helps?
    https://learn.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception?view=net-9.0

    If you created the bot using Visual Studio 2022, the source code should be in the project folder you specified when creating the project. Look for a file named something like Bot.cs or TeamsBot.cs in the Bots folder of your project. This file typically contains the main logic for handling messages and interactions.

    Thanks, 

    Prasad Das

    ************************************************************************* 

    If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.