Skip to content

Commit

Permalink
Merge pull request #133 from stealthrocket/openapi-integration
Browse files Browse the repository at this point in the history
OpenAI integration
  • Loading branch information
chriso authored Mar 21, 2024
2 parents 29469bf + 116d882 commit e3e4b2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dispatch/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Automatically register error and output types from
# commonly used libraries.
integrations = ("httpx", "requests", "slack")
integrations = ("httpx", "requests", "slack", "openai")
for name in integrations:
try:
importlib.import_module(f"dispatch.integrations.{name}")
Expand Down
19 changes: 19 additions & 0 deletions src/dispatch/integrations/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import openai # type: ignore

from dispatch.integrations.http import http_response_code_status
from dispatch.status import Status, register_error_type


def openai_error_status(error: Exception) -> Status:
# See https://github.com/openai/openai-python/blob/main/src/openai/_exceptions.py
match error:
case openai.APITimeoutError():
return Status.TIMEOUT
case openai.APIStatusError():
return http_response_code_status(error.status_code)

return Status.TEMPORARY_ERROR


# Register base exception.
register_error_type(openai.OpenAIError, openai_error_status)

0 comments on commit e3e4b2a

Please sign in to comment.