Skip to content

Commit a180929

Browse files
Fix the bug: when the file does not exist, it should throw the error (#471)
1 parent 138e385 commit a180929

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

sdk_v2/cs/src/Microsoft.AI.Foundry.Local.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@
9898
<PropertyGroup>
9999
<!-- default version unless overridden during dotnet build/restore command -->
100100
<FoundryLocalCoreWinMLVersion Condition="'$(FoundryLocalCoreVersion)' != ''">$(FoundryLocalCoreVersion)</FoundryLocalCoreWinMLVersion>
101-
<FoundryLocalCoreWinMLVersion Condition="'$(FoundryLocalCoreVersion)' == ''">0.9.0.2-dev-20260226T191541-2b332047</FoundryLocalCoreWinMLVersion>
102-
<FoundryLocalCoreVersion Condition="'$(FoundryLocalCoreVersion)' == ''">0.9.0.4-dev-20260226T191638-2b332047</FoundryLocalCoreVersion>
103-
</PropertyGroup>
101+
<FoundryLocalCoreWinMLVersion Condition="'$(FoundryLocalCoreVersion)' == ''">0.9.0-dev-20260227T230631-2a3af92</FoundryLocalCoreWinMLVersion>
102+
<FoundryLocalCoreVersion Condition="'$(FoundryLocalCoreVersion)' == ''">0.9.0-dev-20260227T222239-2a3af92</FoundryLocalCoreVersion> </PropertyGroup>
104103

105104
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
106105
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
@@ -109,6 +108,11 @@
109108
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
110109
</PropertyGroup>
111110

111+
<PropertyGroup>
112+
<!-- NU1604: Transitive dependency Microsoft.ML.OnnxRuntime.Gpu.Linux lacks an inclusive lower bound.
113+
This comes from the Microsoft.AI.Foundry.Local.Core package and cannot be fixed here. -->
114+
<NoWarn>$(NoWarn);NU1604</NoWarn>
115+
</PropertyGroup>
112116
<ItemGroup>
113117
<PackageReference Condition="'$(UseWinML)' == 'true'"
114118
Include="Microsoft.AI.Foundry.Local.Core.WinML" Version="$(FoundryLocalCoreWinMLVersion)" />

sdk_v2/cs/src/OpenAI/AudioClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private async IAsyncEnumerable<AudioCreateTranscriptionResponse> TranscribeAudio
138138
{
139139
var failed = false;
140140

141-
await _coreInterop.ExecuteCommandWithCallbackAsync(
141+
var res = await _coreInterop.ExecuteCommandWithCallbackAsync(
142142
"audio_transcribe",
143143
request,
144144
async (callbackData) =>
@@ -163,6 +163,16 @@ await _coreInterop.ExecuteCommandWithCallbackAsync(
163163
ct
164164
).ConfigureAwait(false);
165165

166+
// If the native layer returned an error (e.g. missing audio file, invalid model)
167+
// without invoking any callbacks, propagate it so the caller sees an exception
168+
// instead of an empty stream.
169+
if (res.Error != null)
170+
{
171+
channel.Writer.TryComplete(
172+
new FoundryLocalException($"Error from audio_transcribe command: {res.Error}", _logger));
173+
return;
174+
}
175+
166176
// use TryComplete as an exception in the callback may have already closed the channel
167177
_ = channel.Writer.TryComplete();
168178
}

sdk_v2/cs/test/FoundryLocal.Tests/AudioClientTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ public async Task AudioTranscription_NoStreaming_Succeeds_WithTemperature()
6767
Console.WriteLine($"Response: {content}");
6868
}
6969

70+
[Test]
71+
public async Task AudioTranscription_NoStreaming_InValidFile()
72+
{
73+
var audioClient = await model!.GetAudioClientAsync();
74+
await Assert.That(audioClient).IsNotNull();
75+
76+
audioClient.Settings.Language = "en";
77+
78+
var audioFilePath = Path.Combine(AppContext.BaseDirectory, "testdata/non_exist_Recording.mp3");
79+
80+
FoundryLocalException? caught = null;
81+
try
82+
{
83+
await audioClient.TranscribeAudioAsync(audioFilePath).ConfigureAwait(false);
84+
}
85+
catch (FoundryLocalException ex)
86+
{
87+
caught = ex;
88+
}
89+
90+
// Assert: a FoundryLocalException must have been thrown
91+
await Assert.That(caught).IsNotNull();
92+
Console.WriteLine($"Caught exception: {caught}");
93+
await Assert.That(caught!.Message).Contains("Audio file not found");
94+
95+
}
96+
7097
[Test]
7198
public async Task AudioTranscription_Streaming_Succeeds()
7299
{
@@ -123,4 +150,33 @@ public async Task AudioTranscription_Streaming_Succeeds_WithTemperature()
123150

124151

125152
}
153+
154+
[Test]
155+
public async Task AudioTranscription_Streaming_InvalidFiles()
156+
{
157+
var audioClient = await model!.GetAudioClientAsync();
158+
await Assert.That(audioClient).IsNotNull();
159+
160+
audioClient.Settings.Language = "en";
161+
162+
var audioFilePath = Path.Combine(AppContext.BaseDirectory, "testdata/Record.mp3");
163+
164+
FoundryLocalException? caught = null;
165+
try
166+
{
167+
await foreach (var _ in audioClient.TranscribeAudioStreamingAsync(audioFilePath, CancellationToken.None).ConfigureAwait(false))
168+
{
169+
}
170+
}
171+
catch (FoundryLocalException ex)
172+
{
173+
caught = ex;
174+
}
175+
176+
// Assert: a FoundryLocalException must have been thrown
177+
await Assert.That(caught).IsNotNull();
178+
Console.WriteLine($"Caught exception: {caught}");
179+
await Assert.That(caught!.Message).Contains("Audio file not found");
180+
181+
}
126182
}

0 commit comments

Comments
 (0)