KeyError: "usage" for gpt-3.5-turbo-16k

Hoang, Steven 40 Reputation points
2024-02-08T22:10:09.22+00:00

I’ve recently been getting KeyErrors when using gpt-3.5-turbo-16k where the “usage” key is completely missing from the response object. What’s weird is that this happens inconsistently, without any changes to the code:

# OpenAI/Azure-specific authentication headers
headers = {
    "Authorization": await get_azure_token(),
    "OCP-Apim-Subscription-key": OPENAI_KEYS[model],
}

payload = {
	"messages": [message.model_dump() for message in messages],
    "temperature": kwargs.get("temperature", 1),
}
async with httpx.AsyncClient(
    verify=False, follow_redirects=True, timeout=360
) as client:
    resp = await client.post(url=OPENAI_URLS[model], json=payload, headers=headers)
	

Traceback (most recent call last):
...
    prompt_tokens=resp.json()["usage"]["prompt_tokens"],
                  ~~~~~~~~~~~^^^^^^^^^
KeyError: 'usage'

This is happening exclusively with 3.5. I am also using GPT4 models and they are working as expected.

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,092 questions
{count} vote

Accepted answer
  1. navba-MSFT 27,545 Reputation points Microsoft Employee Moderator
    2024-02-13T06:43:39.4066667+00:00

    @Sumit @Hoang, Steven I got an update from the Product Group team that the issue has been fixed now. Could you please test again and let me know if you are still encountering this issue ? Awaiting your reply.

    ** Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.