Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI request body validation error response is weird #1140

Open
1 task done
renjithspace opened this issue Oct 19, 2024 · 0 comments
Open
1 task done

OpenAI request body validation error response is weird #1140

renjithspace opened this issue Oct 19, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@renjithspace
Copy link

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

I've sent a request to Create speech API without passing input parameter like below using the openai Node.js library.

fetch("https://api.openai.com/v1/audio/speech", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "tts-1",
    voice: "alloy"
  })
})
  .then(response => {
    if (!response.ok) {
      throw new Error(response.statusText);
    }
    console.log('Request successful');
  })
  .catch(error => console.error(error));

The error looks like below:

{
"error": {
  "message": "[{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}, {'type': 'enum', 'loc': ('body', 'voice'), 'msg': \"Input should be 'nova', 'shimmer', 'echo', 'onyx', 'fable' or 'alloy'\", 'input': '', 'ctx': {'expected': \"'nova', 'shimmer', 'echo', 'onyx', 'fable' or 'alloy'\"}}]",
  "type": "invalid_request_error",
  "param": null,
  "code": null
}
}
  1. Why the error.message is a string?
  2. Why is it a string of pure JS object and not a string of JSON?
    I tried to parse it with JSON.parse but as it's not a valid JSON, I failed to do so.
  3. How do I get the error message from this? Do I have to use regex for that?

I think an improvement is needed so the developer can get the error message property.

To Reproduce

  1. Run below request
fetch("https://api.openai.com/v1/audio/speech", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "tts-1",
    voice: "alloy"
  })
})
  .then(response => {
    if (!response.ok) {
      throw new Error(response.statusText);
    }
    console.log('Request successful');
  })
  .catch(error => console.error(error));
  1. Try to log the error message (the string message from the error message object) to the console

Code snippets

fetch("https://api.openai.com/v1/audio/speech", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "tts-1",
    voice: "alloy"
  })
})
  .then(response => {
    if (!response.ok) {
      throw new Error(response.statusText);
    }
    console.log('Request successful');
  })
  .catch(error => console.error(error));

OS

macOs

Node version

Node v21.7.3

Library version

openai 4.68.1

@renjithspace renjithspace added the bug Something isn't working label Oct 19, 2024
@renjithspace renjithspace reopened this Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant