Wrong return type for openai.audio.speech.create API
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 I'm following the tutorial for TTS on the OpenAI website: https://platform.openai.com/docs/guides/text-to-speech?lang=node When I try to use that code in a typescript file, typescript always infers a return value of "never". Ignoring TS for that line makes it work. <img width="852" alt="Screenshot 2023-11-10 at 4 19 18 PM" src="https://github.com/openai/openai-node/assets/35743865/32daac7c-da62-4826-b723-033c227a045a"> To Reproduce Save the code from https://platform.openai.com/docs/guides/text-to-speech?lang=node in a .ts file. Code snippets _No response_ OS macOS Node version Node v19 Library version v4.17.17
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Type Inference for openai.audio.speech.create in TypeScript
The TypeScript type definitions for the openai.audio.speech.create API are not correctly defined, causing TypeScript to infer the return type as 'never'. This typically occurs when the function does not have a return type specified or when the TypeScript definitions do not align with the actual API response structure.
Awaiting Verification
Be the first to verify this fix
- 1
Update Type Definitions
Modify the type definitions for the openai.audio.speech.create function to correctly reflect the expected return type. This involves updating the library's TypeScript definitions to ensure that the return type is inferred correctly.
typescriptdeclare function createSpeech(params: SpeechParams): Promise<SpeechResponse>; - 2
Check for TypeScript Configuration
Ensure that your TypeScript configuration (tsconfig.json) is set up to include the necessary type definitions. Check that 'esModuleInterop' is set to true, which can help with module compatibility.
json{ "compilerOptions": { "esModuleInterop": true, "strict": true } } - 3
Reinstall OpenAI Node Library
Sometimes, type issues can arise from outdated or corrupted installations. Reinstall the OpenAI Node library to ensure you have the latest version with the correct type definitions.
bashnpm uninstall openai && npm install openai - 4
Test the Function
After making the changes, test the function in your TypeScript file to confirm that TypeScript now correctly infers the return type. You should no longer see 'never' as the inferred type.
typescriptconst response = await openai.audio.speech.create({ /* parameters */ }); console.log(response);
Validation
To confirm the fix worked, run your TypeScript file and check that TypeScript no longer infers the return type as 'never'. Additionally, ensure that the API call executes successfully and returns the expected response structure.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep