Skip to content

feat(drivers-prompt-openai): wrap OpenAI SDK exceptions in PromptDriverError (#1946)#2209

Open
KeelSmith wants to merge 1 commit into
griptape-ai:mainfrom
KeelSmith:fix/1946-parse-openai-exceptions
Open

feat(drivers-prompt-openai): wrap OpenAI SDK exceptions in PromptDriverError (#1946)#2209
KeelSmith wants to merge 1 commit into
griptape-ai:mainfrom
KeelSmith:fix/1946-parse-openai-exceptions

Conversation

@KeelSmith

Copy link
Copy Markdown

What

Resolves #1946. OpenAiChatPromptDriver now raises griptape.exceptions.PromptDriverError instead of letting raw openai.* SDK exceptions propagate, so downstream apps can handle errors without importing openai or string-matching messages. The original SDK error is preserved on __cause__.

Approach

  • New GriptapeError base + PromptDriverError (carries status_code) under griptape/exceptions/, modeled on Pydantic AI's single ModelHTTPError rather than a class-per-status — kept minimal to match the codebase; subtypes can be added later non-breakingly.
  • An overridable BasePromptDriver._wrap_exception hook, applied above the retry loop in run(). Deliberate: tenacity / ignored_exception_types key on the native openai.* type, so wrapping above the loop leaves retry/fast-fail behavior unchanged. The default hook is a no-op, so non-OpenAI drivers are unaffected.
  • OpenAiChatPromptDriver overrides it to map openai.APIStatusError/OpenAIErrorPromptDriverError (chained with raise ... from e); non-openai exceptions pass through untouched so internal bugs aren't masked. Also covers the Azure/Grok/Perplexity subclasses.

⚠️ Behavior change

Callers doing except openai.SomeError around driver.run() (OpenAI driver + subclasses) will no longer catch it — it's now PromptDriverError, with the original on __cause__. I left the auto-generated CHANGELOG untouched; happy to add a migration note wherever you prefer and to flag this however you'd like for versioning.

Testing

  • make check (format/lint/types/spell) passes.
  • make test/unit passes — 7 new tests: wrap + status_code + __cause__, fast-fail vs retried call-counts, connection-error status_code=None, non-openai passthrough, and streaming (at-call and mid-iteration). Tests build real openai errors from real httpx responses.

Questions

  1. Granularity — I went minimal (one PromptDriverError + status_code). Want per-status subclasses instead, or is minimal preferred?
  2. Versioning — how would you like the behavior change flagged?

Note on authorship

This change was written with AI assistance; I've reviewed, tested, and fully understand it, and I own the outcome — happy to dig into any of the design decisions in review.

Open to any changes — happy to follow your preferred direction.

…erError

Resolves griptape-ai#1946. OpenAiChatPromptDriver previously let raw openai.* SDK
exceptions propagate to callers; it now raises
griptape.exceptions.PromptDriverError (with status_code for HTTP errors),
preserving the original error as __cause__.

- add GriptapeError base + PromptDriverError(status_code) under griptape/exceptions/
- add an overridable BasePromptDriver._wrap_exception hook applied ABOVE the
  retry loop, so tenacity / ignored_exception_types still match native openai.*
  types and retry behavior is unchanged; default is a no-op (other drivers unaffected)
- OpenAiChatPromptDriver maps openai.APIStatusError/OpenAIError -> PromptDriverError;
  non-openai exceptions pass through untouched
- tests: wrap + status_code + __cause__, fast-fail vs retried call counts,
  connection-error status_code=None, non-openai passthrough, streaming

Behavior change: callers catching openai.* off run() (OpenAI driver and its
Azure/Grok/Perplexity subclasses) now catch PromptDriverError; original SDK
error remains on __cause__.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@collindutter collindutter self-requested a review June 22, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parse OpenAi Exceptions

1 participant