Skip to content

Commit e718db6

Browse files
authored
fix(core): consider code: context_length_exceeded as context overflow in API call errors (#17748)
1 parent 15b27e0 commit e718db6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/opencode/src/provider/error.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export namespace ProviderError {
167167

168168
export function parseAPICallError(input: { providerID: ProviderID; error: APICallError }): ParsedAPICallError {
169169
const m = message(input.providerID, input.error)
170-
if (isOverflow(m) || input.error.statusCode === 413) {
170+
const body = json(input.error.responseBody)
171+
if (isOverflow(m) || input.error.statusCode === 413 || body?.error?.code === "context_length_exceeded") {
171172
return {
172173
type: "context_overflow",
173174
message: m,

packages/opencode/test/session/message-v2.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,26 @@ describe("session.message-v2.fromError", () => {
869869
})
870870
})
871871

872+
test("detects context overflow from context_length_exceeded code in response body", () => {
873+
const error = new APICallError({
874+
message: "Request failed",
875+
url: "https://example.com",
876+
requestBodyValues: {},
877+
statusCode: 422,
878+
responseHeaders: { "content-type": "application/json" },
879+
responseBody: JSON.stringify({
880+
error: {
881+
message: "Some message",
882+
type: "invalid_request_error",
883+
code: "context_length_exceeded",
884+
},
885+
}),
886+
isRetryable: false,
887+
})
888+
const result = MessageV2.fromError(error, { providerID })
889+
expect(MessageV2.ContextOverflowError.isInstance(result)).toBe(true)
890+
})
891+
872892
test("does not classify 429 no body as context overflow", () => {
873893
const result = MessageV2.fromError(
874894
new APICallError({

0 commit comments

Comments
 (0)