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.