{"record":{"id":"7aabcc49962d4fc2","repo":"microsoft/semantic-kernel","slug":"no-file-found-in-the-response","errorCode":null,"errorMessage":"No file found in the response.","messagePattern":"No file found in the response\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step02_BedrockAgent_CodeInterpreter.cs","lineNumber":54,"sourceCode":"        try\n        {\n            BinaryContent? binaryContent = null;\n            var responses = bedrockAgent.InvokeAsync(new ChatMessageContent(AuthorRole.User, UserQuery), bedrockAgentThread, null);\n            await foreach (ChatMessageContent response in responses)\n            {\n                if (response.Content != null)\n                {\n                    this.Output.WriteLine(response.Content);\n                }\n                if (binaryContent == null && response.Items.Count > 0)\n                {\n                    binaryContent = response.Items.OfType<BinaryContent>().FirstOrDefault();\n                }\n            }\n\n            if (binaryContent == null)\n            {\n                throw new InvalidOperationException(\"No file found in the response.\");\n            }\n\n            // Save the file to the same directory as the test assembly\n            var filePath = Path.Combine(\n                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,\n                binaryContent.Metadata![\"Name\"]!.ToString()!);\n            this.Output.WriteLine($\"Saving file to {filePath}\");\n            binaryContent.WriteToFile(filePath, overwrite: true);\n\n            // Expected output:\n            // Here is the bar chart for the given data:\n            // [A bar chart showing the following data:\n            // Panda   5\n            // Tiger   8\n            // Lion    3\n            // Monkey  6\n            // Dolphin 2]\n            // Saving file to ...","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step02_BedrockAgent_CodeInterpreter.cs#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the prompt explicitly asks for a file or computation that triggers the code interpreter (e.g., 'Create a bar chart of this data').","Verify the Bedrock agent has a code interpreter action group enabled and properly configured.","Check the response chunks for text content — the agent may have responded with an explanation instead of a file.","Handle the null case gracefully by reporting the text response instead of throwing."],"exampleFix":"// before\nif (binaryContent == null)\n{\n    throw new InvalidOperationException(\"No file found in the response.\");\n}\n\n// after — fall back to text content and warn\nif (binaryContent == null)\n{\n    this.Output.WriteLine(\"No file was generated. The agent may have responded with text only.\");\n    return;\n}","handlingStrategy":"validation","validationCode":"// Check response items for binary content without throwing\nvar binaryContent = responses.SelectMany(r => r.Items).OfType<BinaryContent>().FirstOrDefault();\nif (binaryContent is null)\n{ Console.WriteLine(\"No file in response. The agent responded with text only.\"); return; }","typeGuard":"bool HasBinaryContent(ChatMessageContentResponse r) => r.Items.OfType<BinaryContent>().Any();","tryCatchPattern":"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.\"); }","preventionTips":["Ensure the prompt explicitly requests file/chart output to trigger the code interpreter.","Verify the Bedrock agent has a code interpreter action group enabled.","Handle the no-file case gracefully by reporting text content."],"tags":["bedrock","agent","code-interpreter","binary-content","aws"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}