Skip to content
Dispatch Dispatch Dispatch

OpenAI

Dispatch provides a built-in integration with openai to retry failed OpenAI requests and exceptions. It’s enabled by default and it takes no extra code to set it up.

import dispatch
from openai import Client
openai = Client(api_key="<your API key>")
@dispatch.function
def ask_chatgpt(question):
chat_completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": question},
],
)
return chat_completion.choices[0].message.content
def main():
ask_chatgpt.dispatch("What is the most loved programming language?")
dispatch.run(main)

Exception handling

APIStatusError

Depending on the status_code attribute, a different Dispatch behavior is applied.

HTTP status codeDescriptionDispatch status
1xxInformationalStatus.PERMANENT_ERROR
2xxSuccessfulStatus.OK
3xxRedirectionStatus.PERMANENT_ERROR
3xxRedirectionStatus.PERMANENT_ERROR
4xxClient errorStatus.PERMANENT_ERROR
400Bad requestStatus.INVALID_ARGUMENT
401UnauthorizedStatus.UNAUTHENTICATED
403ForbiddenStatus.PERMISSION_DENIED
404Not foundStatus.NOT_FOUND
408Request timeoutStatus.TIMEOUT
429Too many requestsStatus.THROTTLED
5xxServer errorStatus.TEMPORARY_ERROR
501Not implementedStatus.PERMANENT_ERROR

APITimeoutError

Returns Status.TIMEOUT.

OpenAIError

Returns Status.TEMPORARY_ERROR, unless exception is APIStatusError or APITimeoutError, since OpenAIError is a parent class of these. Learn more in openai documentation.