Skip to content

Commit e3e4b2a

Browse files
authored
Merge pull request #133 from stealthrocket/openapi-integration
OpenAI integration
2 parents 29469bf + 116d882 commit e3e4b2a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/dispatch/integrations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Automatically register error and output types from
88
# commonly used libraries.
9-
integrations = ("httpx", "requests", "slack")
9+
integrations = ("httpx", "requests", "slack", "openai")
1010
for name in integrations:
1111
try:
1212
importlib.import_module(f"dispatch.integrations.{name}")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import openai # type: ignore
2+
3+
from dispatch.integrations.http import http_response_code_status
4+
from dispatch.status import Status, register_error_type
5+
6+
7+
def openai_error_status(error: Exception) -> Status:
8+
# See https://github.com/openai/openai-python/blob/main/src/openai/_exceptions.py
9+
match error:
10+
case openai.APITimeoutError():
11+
return Status.TIMEOUT
12+
case openai.APIStatusError():
13+
return http_response_code_status(error.status_code)
14+
15+
return Status.TEMPORARY_ERROR
16+
17+
18+
# Register base exception.
19+
register_error_type(openai.OpenAIError, openai_error_status)

0 commit comments

Comments
 (0)