fix(kosong): classify base openai.APIError as retryable for streaming disconnections#1800
Merged
fix(kosong): classify base openai.APIError as retryable for streaming disconnections#1800
Conversation
… disconnections When the OpenAI SDK encounters a mid-stream transport failure it raises the base `openai.APIError` instead of `APIConnectionError`. The existing `convert_error()` did not handle this case, causing the error to become a generic `ChatProviderError` that bypasses both `_run_with_connection_recovery` and tenacity retry logic — resulting in an unrecoverable crash. Add a guarded `case openai.APIError()` branch that heuristically maps transport-layer errors (body=None, exact APIError type) to the appropriate retryable error type based on the error message content.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an unrecoverable crash in the Kosong OpenAI integration when the OpenAI SDK raises a base openai.APIError during mid-stream transport failures (notably streaming disconnections), by classifying those base errors as retryable so existing recovery/retry logic can run.
Changes:
- Extend
convert_error()to detect exactopenai.APIErrorinstances withbody is Noneand heuristically map them toAPIConnectionError/APITimeoutError. - Add unit + integration tests covering the new base-
APIErrorclassification behavior and guardrails (body-present SSE errors and validation errors should not be reclassified). - Document the fix in package and CLI release notes / changelogs.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/kosong/src/kosong/chat_provider/openai_common.py | Adds guarded base openai.APIError handling and message-based heuristic classification for retryability. |
| packages/kosong/tests/test_openai_common.py | Adds parametrized unit tests and a streaming integration test to prevent regression. |
| packages/kosong/CHANGELOG.md | Notes the Kosong package-level fix in Unreleased. |
| CHANGELOG.md | Notes the CLI-level fix in Unreleased. |
| docs/en/release-notes/changelog.md | Adds the Unreleased release-note entry in English. |
| docs/zh/release-notes/changelog.md | Adds the Unreleased release-note entry in Chinese. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Kai <me@kaiyi.cool>
Signed-off-by: Kai <me@kaiyi.cool>
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.
Summary
openai.APIError(notAPIConnectionError). The existingconvert_error()did not handle this case — the error fell through to a genericChatProviderError, bypassing both_run_with_connection_recoveryand tenacity retry logic, causing an unrecoverable crash.case openai.APIError()branch with dual guards (type(error) is openai.APIError+error.body is None) that heuristically maps transport-layer errors to the appropriate retryable error type based on message content.APIResponseValidationErrorsubclasses are explicitly excluded from the heuristic classification.Test plan
APIResponseValidationErrorfalls throughAPIError("Network connection lost.")→APIConnectionErrormake check-kosongpasses (0 new diagnostics)