files.retrieveContent only returns strings, and not bytes/binary data
Problem
Confirm this is a Node library issue and not an underlying OpenAI API issue - [X] This is an issue with the Node library Describe the bug See this issue for the python library. I don't see an equivalent method to with_raw_response in the node sdk. To Reproduce [code block] This saves a corrupt file, though the file can be downloaded from the playground without a problem. Code snippets _No response_ OS windows 11 Node version Node.js v20.9.0 Library version "openai": "^4.16.1"
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Raw Response Handling for Binary Data in Node SDK
The Node.js OpenAI library does not currently support retrieving raw binary data directly from the API responses, which results in files being saved as corrupt strings instead of proper binary files. This is due to the absence of a method equivalent to Python's 'with_raw_response' that allows for raw response handling.
Awaiting Verification
Be the first to verify this fix
- 1
Check for Existing Raw Response Methods
Review the latest OpenAI Node.js SDK documentation and source code to confirm the absence of a method for raw response handling similar to Python's 'with_raw_response'.
- 2
Modify the SDK to Include Raw Response Handling
Implement a new method in the Node.js SDK that allows users to request raw binary data from the API. This method should handle the response as a Buffer instead of a string.
typescriptconst response = await openai.files.retrieveContent({ fileId, raw: true }); const binaryData = Buffer.from(response.data); - 3
Update Documentation
Once the new method is implemented, update the SDK documentation to include examples of how to use the raw response feature for binary file retrieval.
- 4
Test the New Functionality
Create unit tests to verify that the new method correctly retrieves binary data and that files saved using this method are not corrupt.
typescriptit('should retrieve binary data correctly', async () => { const binaryData = await openai.files.retrieveContent({ fileId, raw: true }); expect(binaryData).toBeInstanceOf(Buffer); });
Validation
Confirm that the new method retrieves binary data correctly by testing it with various file types and ensuring that the saved files are not corrupt. Additionally, verify that the documentation reflects the new functionality.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep