handle missing finish_reason (#7296)

In some cases, the OpenAI response is missing the `finish_reason`
attribute. It seems to happen when using Ada or Babbage and
`stream=true`, but I can't always reproduce it. This change just
gracefully handles the missing key.
pull/7293/head
Bruno Bornsztein 1 year ago committed by GitHub
parent 6ff9e9b34a
commit 1a4ca3eff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -48,9 +48,9 @@ def update_token_usage(
def _update_response(response: Dict[str, Any], stream_response: Dict[str, Any]) -> None:
"""Update response from the stream response."""
response["choices"][0]["text"] += stream_response["choices"][0]["text"]
response["choices"][0]["finish_reason"] = stream_response["choices"][0][
"finish_reason"
]
response["choices"][0]["finish_reason"] = stream_response["choices"][0].get(
"finish_reason", None
)
response["choices"][0]["logprobs"] = stream_response["choices"][0]["logprobs"]

Loading…
Cancel
Save