{"record":{"id":"b2785b87c401d2ac","repo":"microsoft/aspire","slug":"failed-to-resolve-connection-id-for-azure-ai-search-tool-b2785b","errorCode":null,"errorMessage":"Failed to resolve connection ID for Azure AI Search tool '{Name}'. The Foundry project connection may not have been provisioned correctly.","messagePattern":"Failed to resolve connection ID for Azure AI Search tool '(.+?)'\\. The Foundry project connection may not have been provisioned correctly\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/Toolbox/FoundryToolboxToolDefinition.cs","lineNumber":391,"sourceCode":"    /// </summary>\n    public AzureCognitiveServicesProjectConnectionResource Connection { get; }\n\n    /// <summary>\n    /// Gets the Azure AI Search index name.\n    /// </summary>\n    public string IndexName { get; }\n\n    public string? Description { get; }\n\n    internal override async ValueTask<ResolvedFoundryToolboxTool> ResolveAsync(CancellationToken cancellationToken)\n    {\n        // The Foundry project connection's \"id\" bicep output is only populated after provisioning,\n        // so this resolves to a real value only at deploy time. Matches AzureAISearchToolResource.\n        var connectionIdRef = new BicepOutputReference(\"id\", Connection);\n        var connectionId = await connectionIdRef.GetValueAsync(cancellationToken).ConfigureAwait(false);\n        if (string.IsNullOrEmpty(connectionId))\n        {\n            throw new InvalidOperationException(\n                $\"Failed to resolve connection ID for Azure AI Search tool '{Name}'. \" +\n                \"The Foundry project connection may not have been provisioned correctly.\");\n        }\n\n        var index = new AzureAISearchToolIndex\n        {\n            ProjectConnectionId = connectionId,\n            IndexName = IndexName\n        };\n        var options = new AzureAISearchToolOptions([index]);\n        var unnamedTool = new AzureAISearchTool(options);\n        var unnamedJson = ModelReaderWriter.Write(\n            unnamedTool,\n            ModelReaderWriterOptions.Json,\n            AzureAIProjectsAgentsContext.Default);\n        using var unnamedDocument = JsonDocument.Parse(unnamedJson);\n        using var stream = new MemoryStream();\n        using (var writer = new Utf8JsonWriter(stream))","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/Toolbox/FoundryToolboxToolDefinition.cs#L373-L409","documentation":"FoundryToolboxAzureAISearchToolDefinition.ResolveAsync reads the 'id' Bicep output of the Foundry project connection resource to populate AzureAISearchToolIndex.ProjectConnectionId. That output only exists after Azure provisioning; if it is empty at resolve time, the tool cannot reference the connection, so this InvalidOperationException is thrown (mirroring AzureAISearchToolResource behavior).","triggerScenarios":"Resolving the toolbox tool in AppHost (run) mode before `azd provision`/deployment has produced the connection's 'id' output; the connection resource failed or was skipped during provisioning; the wrong connection resource was wired into the tool.","commonSituations":"Running F5 in AppHost mode where Bicep outputs aren't materialized; a deployment that partially failed leaving the project connection unprovisioned; renaming/recreating the Foundry project connection so the cached output is missing.","solutions":["Deploy/provision the AppHost (azd provision or publish/deploy) so the connection's 'id' Bicep output is populated, then resolve the tool.","Verify the AzureCognitiveServicesProjectConnectionResource passed to the tool actually deployed successfully (check deployment logs/portal).","Only run this code path in deploy/publish mode, or guard it so it is not exercised in local run mode.","Catch InvalidOperationException and check whether the BicepOutputReference(\"id\", Connection) has a value before proceeding."],"exampleFix":"// before\nif (!app.Environment.IsPublishMode()) { ResolveToolbox(); }\n// after\nif (app.Environment.IsPublishMode()) { ResolveToolbox(); } // connection id exists only after provisioning","handlingStrategy":"try-catch","validationCode":"var idRef = new BicepOutputReference(\"id\", connection);\nvar id = await idRef.GetValueAsync(ct);\nif (string.IsNullOrEmpty(id)) throw new InvalidOperationException(\"Connection id not provisioned yet.\");","typeGuard":"static bool ConnectionProvisioned(AzureCognitiveServicesProjectConnectionResource c) =>\n    !string.IsNullOrEmpty(new BicepOutputReference(\"id\", c).Value);","tryCatchPattern":"try { await definition.ResolveAsync(ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to resolve connection ID\"))\n{ logger.LogError(ex, \"Run azd provision/deploy so the Foundry project connection 'id' output exists.\"); }","preventionTips":["Only resolve Azure AI Search toolbox tools in publish/deploy mode","Confirm the project connection resource deployed successfully before resolving","Re-provision after renaming or recreating the Foundry connection"],"tags":["foundry","azure","ai-search","bicep","provisioning","aspire"],"backgroundTag":"resource-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}