feat(drivers-prompt-openai): wrap OpenAI SDK exceptions in PromptDriverError (#1946)#2209
Open
KeelSmith wants to merge 1 commit into
Open
feat(drivers-prompt-openai): wrap OpenAI SDK exceptions in PromptDriverError (#1946)#2209KeelSmith wants to merge 1 commit into
KeelSmith wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Resolves #1946.
OpenAiChatPromptDrivernow raisesgriptape.exceptions.PromptDriverErrorinstead of letting rawopenai.*SDK exceptions propagate, so downstream apps can handle errors without importingopenaior string-matching messages. The original SDK error is preserved on__cause__.Approach
GriptapeErrorbase +PromptDriverError(carriesstatus_code) undergriptape/exceptions/, modeled on Pydantic AI's singleModelHTTPErrorrather than a class-per-status — kept minimal to match the codebase; subtypes can be added later non-breakingly.BasePromptDriver._wrap_exceptionhook, applied above the retry loop inrun(). Deliberate:tenacity/ignored_exception_typeskey on the nativeopenai.*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.OpenAiChatPromptDriveroverrides it to mapopenai.APIStatusError/OpenAIError→PromptDriverError(chained withraise ... from e); non-openaiexceptions pass through untouched so internal bugs aren't masked. Also covers the Azure/Grok/Perplexity subclasses.Callers doing
except openai.SomeErrorarounddriver.run()(OpenAI driver + subclasses) will no longer catch it — it's nowPromptDriverError, 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/unitpasses — 7 new tests: wrap +status_code+__cause__, fast-fail vs retried call-counts, connection-errorstatus_code=None, non-openai passthrough, and streaming (at-call and mid-iteration). Tests build realopenaierrors from realhttpxresponses.Questions
PromptDriverError+status_code). Want per-status subclasses instead, or is minimal preferred?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.