microsoft/semantic-kernel · error · InvalidOperationException

No file found in the response.

Error message

No file found in the response.

What it means

After invoking a Bedrock agent configured with a code interpreter, the code scans the response chunks for BinaryContent items. If no BinaryContent was found across all chunks (binaryContent remains null), it throws InvalidOperationException. This indicates the agent's response did not contain any file output, which the sample expects.

Source

Thrown at dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step02_BedrockAgent_CodeInterpreter.cs:54

        try
        {
            BinaryContent? binaryContent = null;
            var responses = bedrockAgent.InvokeAsync(new ChatMessageContent(AuthorRole.User, UserQuery), bedrockAgentThread, null);
            await foreach (ChatMessageContent response in responses)
            {
                if (response.Content != null)
                {
                    this.Output.WriteLine(response.Content);
                }
                if (binaryContent == null && response.Items.Count > 0)
                {
                    binaryContent = response.Items.OfType<BinaryContent>().FirstOrDefault();
                }
            }

            if (binaryContent == null)
            {
                throw new InvalidOperationException("No file found in the response.");
            }

            // Save the file to the same directory as the test assembly
            var filePath = Path.Combine(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
                binaryContent.Metadata!["Name"]!.ToString()!);
            this.Output.WriteLine($"Saving file to {filePath}");
            binaryContent.WriteToFile(filePath, overwrite: true);

            // Expected output:
            // Here is the bar chart for the given data:
            // [A bar chart showing the following data:
            // Panda   5
            // Tiger   8
            // Lion    3
            // Monkey  6
            // Dolphin 2]
            // Saving file to ...

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure the prompt explicitly asks for a file or computation that triggers the code interpreter (e.g., 'Create a bar chart of this data').
  2. Verify the Bedrock agent has a code interpreter action group enabled and properly configured.
  3. Check the response chunks for text content — the agent may have responded with an explanation instead of a file.
  4. Handle the null case gracefully by reporting the text response instead of throwing.

Example fix

// before
if (binaryContent == null)
{
    throw new InvalidOperationException("No file found in the response.");
}

// after — fall back to text content and warn
if (binaryContent == null)
{
    this.Output.WriteLine("No file was generated. The agent may have responded with text only.");
    return;
}
Defensive patterns

Strategy: validation

Validate before calling

// Check response items for binary content without throwing
var binaryContent = responses.SelectMany(r => r.Items).OfType<BinaryContent>().FirstOrDefault();
if (binaryContent is null)
{ Console.WriteLine("No file in response. The agent responded with text only."); return; }

Type guard

bool HasBinaryContent(ChatMessageContentResponse r) => r.Items.OfType<BinaryContent>().Any();

Try / catch

try { /* invoke agent and save file */ } catch (InvalidOperationException ex) when (ex.Message == "No file found in the response.") { Console.WriteLine("Agent did not produce a file; check the prompt and code interpreter config."); }

Prevention

When it happens

Trigger: Invoking a Bedrock agent with code interpreter enabled, but the agent's response contained no file/binary output — e.g., the agent answered with text only, the code interpreter tool wasn't triggered, or the file generation failed server-side.

Common situations: The prompt didn't request a file/chart/ computation, so the agent responded with text only; the Bedrock code interpreter action group wasn't properly configured; the agent didn't decide to use the code interpreter tool; a transient Bedrock-side issue prevented file generation; the response items were consumed/iterated differently so BinaryContent was missed.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/7aabcc49962d4fc2. Report an issue: GitHub.