openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

fix:fix response format bug

Open guaguaguaxia opened this issue 2 years ago • 9 comments

guaguaguaxia avatar Mar 07 '23 02:03 guaguaguaxia

@MattFisher please review it, i don't know how i close last same PR 😭

guaguaguaxia avatar Mar 07 '23 02:03 guaguaguaxia

I'm not a maintainer of the project or anything, just another user!

Though as I said here, I think this will fix the issue without further code changes.

The Content-Type header on the response is always set correctly (at least for the audio transcription responses), so we can deduce whether to json-decode within APIRequestor._interpret_response.

For the json and verbose_json response formats, the Content-Type is 'application/json', while for text, srt, and vtt, the Content-Type is 'text/plain; charset=utf-8'.

MattFisher avatar Mar 07 '23 03:03 MattFisher

I'm not a maintainer of the project or anything, just another user!

ok thanks

guaguaguaxia avatar Mar 07 '23 03:03 guaguaguaxia

I've manually tested the change and it works for all the different audio response formats. I can't verify that it doesn't break any other endpoints though.

MattFisher avatar Mar 07 '23 03:03 MattFisher

@MattFisher I thought about it for a second,i still think we should define a enum and add a function parameter to make code more clearly

guaguaguaxia avatar Mar 07 '23 03:03 guaguaguaxia

@guaguaguaxia that would make sense if the Audio module was the only code that consumed the APIRequestor class, but it's not. APIRequestor is used all through the codebase, so if we were going to add an extra parameter to its __init__ method, that would imply that all the other modules need to know about that parameter. In this case, that doesn't make sense, because the choices for that parameter are specific to the Audio transcribe function. Other callers of APIRequestor() would then expect to be able to pass response_format='text' and get back some textual representation of the response, which isn't going to work.

MattFisher avatar Mar 07 '23 03:03 MattFisher

@guaguaguaxia that would make sense if the Audio module was the only code that consumed the APIRequestor class, but it's not. APIRequestor is used all through the codebase, so if we were going to add an extra parameter to its __init__ method, that would imply that all the other modules need to know about that parameter. In this case, that doesn't make sense, because the choices for that parameter are specific to the Audio transcribe function. Other callers of APIRequestor() would then expect to be able to pass response_format='text' and get back some textual representation of the response, which isn't going to work.

you r right, i am not familiar with this project structure

guaguaguaxia avatar Mar 07 '23 03:03 guaguaguaxia

Might want to roll back the whitespace changes in openai/util.py

MattFisher avatar Mar 07 '23 03:03 MattFisher

@MattFisher thanks.

@logankilpatrick @mpokrass you can see this:https://github.com/openai/openai-python/issues/243#issuecomment-1457398603

guaguaguaxia avatar Mar 07 '23 04:03 guaguaguaxia

Looks awesome! Thanks for writing the PR!

hallacy avatar Mar 08 '23 20:03 hallacy

Now, response_format is in plain text and not defaults to json format as it says in docs, right?

dusdb1 avatar Mar 08 '23 23:03 dusdb1

Now, response_format is in plain text and not defaults to json format as it says in docs, right?

No,if response_format type is json or verbose_json,it still return json,other return text

guaguaguaxia avatar Mar 09 '23 01:03 guaguaguaxia