Skip to content

Commit c0e1f54

Browse files
authored
[AI Projects] Improve error message when no connection is found (#48767)
1 parent 486fc17 commit c0e1f54

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

sdk/ai/Azure.AI.Projects/src/Custom/Connection/ConnectionsClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public virtual Response<ConnectionResponse> GetDefaultConnection(ConnectionType
543543

544544
if (connections?.Value == null || connections.Value.Count == 0)
545545
{
546-
throw new InvalidOperationException("No connections found for the specified parameters.");
546+
throw new InvalidOperationException($"No connections found for '{category}'. At least one connection is required. Please add a new connection in the Azure AI Foundry portal by following the instructions here: https://aka.ms/azsdk/azure-ai-projects/how-to/connections-add");
547547
}
548548

549549
ConnectionResponse connection = connections.Value[0];

sdk/ai/Azure.AI.Projects/tests/Samples/Extensions/Sample_AIInference.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,22 @@ public void InferenceEmbedding()
5757
Console.WriteLine($"Index: {item.Index}, Embedding: <{string.Join(", ", embedding)}>");
5858
}
5959
}
60+
61+
[Test]
62+
public void ThrowsWhenNoConnection()
63+
{
64+
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
65+
var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME;
66+
AIProjectClient client = new AIProjectClient(connectionString);
67+
68+
var ex = Assert.Throws<InvalidOperationException>(() =>
69+
{
70+
ChatCompletionsClient chatClient = client.GetChatCompletionsClient();
71+
});
72+
73+
Assert.AreEqual(
74+
$"No connections found for '{ConnectionType.Serverless}'. At least one connection is required. Please add a new connection in the Azure AI Foundry portal by following the instructions here: https://aka.ms/azsdk/azure-ai-projects/how-to/connections-add",
75+
ex.Message);
76+
Console.WriteLine(ex.Message);
77+
}
6078
}

sdk/ai/Azure.AI.Projects/tests/Samples/Extensions/Sample_AzureOpenAI.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,22 @@ public void AzureOpenAIChatCompletion()
2323
ChatCompletion result = chatClient.CompleteChat("List all the rainbow colors");
2424
Console.WriteLine(result.Content[0].Text);
2525
}
26+
27+
[Test]
28+
public void ThrowsWhenNoConnection()
29+
{
30+
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
31+
var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME;
32+
AIProjectClient client = new AIProjectClient(connectionString);
33+
34+
var ex = Assert.Throws<InvalidOperationException>(() =>
35+
{
36+
ChatClient chatClient = client.GetAzureOpenAIChatClient("gpt-4o-mini");
37+
});
38+
39+
Assert.AreEqual(
40+
$"No connections found for '{ConnectionType.AzureOpenAI}'. At least one connection is required. Please add a new connection in the Azure AI Foundry portal by following the instructions here: https://aka.ms/azsdk/azure-ai-projects/how-to/connections-add",
41+
ex.Message);
42+
Console.WriteLine(ex.Message);
43+
}
2644
}

sdk/ai/Azure.AI.Projects/tests/Samples/Extensions/Sample_Search.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,22 @@ public void Search()
2929
Console.WriteLine($"{id}: {name}");
3030
}
3131
}
32+
33+
[Test]
34+
public void ThrowsWhenNoConnection()
35+
{
36+
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
37+
var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME;
38+
AIProjectClient client = new AIProjectClient(connectionString);
39+
40+
var ex = Assert.Throws<InvalidOperationException>(() =>
41+
{
42+
SearchClient searchClient = client.GetSearchClient("index");
43+
});
44+
45+
Assert.AreEqual(
46+
$"No connections found for '{ConnectionType.AzureAISearch}'. At least one connection is required. Please add a new connection in the Azure AI Foundry portal by following the instructions here: https://aka.ms/azsdk/azure-ai-projects/how-to/connections-add",
47+
ex.Message);
48+
Console.WriteLine(ex.Message);
49+
}
3250
}

0 commit comments

Comments
 (0)