Skip to content

Commit 888cefe

Browse files
committed
fix(api): normalize stream-iteration aborts in the xAI responses path
CodeRabbit: an abort landing after responses.create resolved escaped as the OpenAI SDK's APIUserAbortError (name "Error") from the yield* over processResponsesApiStream, outside the create catch, so callers classified the cancellation as a provider failure. Wrap the stream iteration and normalize through isRequestAborted/createAbortError to the series contract (name AbortError, "The xAI request was aborted"). Regression test pins the path; negative control: reverting the wrap fails exactly that test (1 red / 37 green).
1 parent 67b5966 commit 888cefe

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/api/providers/__tests__/xai.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,35 @@ describe("XAIHandler", () => {
537537
})
538538
})
539539

540+
it("createMessage should normalize an SDK abort raised while iterating the response stream", async () => {
541+
const controller = new AbortController()
542+
const sdkAbortError = new APIUserAbortError()
543+
544+
// responses.create resolves; the cancellation lands while the stream is
545+
// being consumed, so the SDK abort error escapes from the iterator.
546+
mockResponsesCreate.mockResolvedValueOnce({
547+
[Symbol.asyncIterator]: () => ({
548+
next: async () => {
549+
controller.abort()
550+
throw sdkAbortError
551+
},
552+
}),
553+
})
554+
555+
const stream = handler.createMessage(
556+
"test prompt",
557+
[],
558+
makeCreateMessageMetadata({ abortSignal: controller.signal }),
559+
)
560+
561+
// The raw SDK abort error (name "Error") must be normalized to the
562+
// contract AbortError even though it escapes outside the create call.
563+
await expect(stream.next()).rejects.toMatchObject({
564+
name: "AbortError",
565+
message: "The xAI request was aborted",
566+
})
567+
})
568+
540569
it("should reject with AbortError when createMessage is called with an already-aborted signal", async () => {
541570
const abortedController = new AbortController()
542571
abortedController.abort()

‎src/api/providers/xai.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,19 @@ export class XAIHandler extends BaseProvider implements SingleCompletionHandler
159159
}
160160

161161
const normalizeUsage = createUsageNormalizer()
162-
yield* processResponsesApiStream(stream, normalizeUsage)
162+
try {
163+
yield* processResponsesApiStream(stream, normalizeUsage)
164+
} catch (error) {
165+
// An abort landing after responses.create resolved surfaces here as
166+
// the SDK's APIUserAbortError thrown from stream iteration (name
167+
// "Error", message "Request was aborted."), outside the create
168+
// catch above. Normalize it to the contract AbortError too, so
169+
// callers never classify a cancellation as a provider failure.
170+
if (isRequestAborted(error, abortSignal)) {
171+
throw createAbortError(this.providerName)
172+
}
173+
throw error
174+
}
163175
} finally {
164176
// Release the listener once the stream is consumed, whether the
165177
// request completed, failed, or the generator was closed early.

0 commit comments

Comments
 (0)