| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestParseContextLimitErrorJSONAndEnglish(t *testing.T) { |
| 12 | body := `{"error":{"message":"This model's maximum context length is 1048576 tokens. However, you requested 1165351 tokens (810882 in the messages, 354469 in the completion)."}}` |
| 13 | got := ParseContextLimitError(&APIError{Status: 400, Body: body}) |
| 14 | if got == nil || got.WindowTokens != 1_048_576 || got.PromptTokens != 810_882 || got.CompletionTokens != 354_469 || got.RequestedTokens != 1_165_351 { |
| 15 | t.Fatalf("english/json parse = %+v", got) |
| 16 | } |
| 17 | if errors.Unwrap(got) == nil { |
| 18 | t.Fatal("Unwrap must return the original APIError") |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestParseContextLimitErrorNumericJSON(t *testing.T) { |
| 23 | body := `{"error":{"context_length":1048576,"requested_tokens":1165351,"prompt_tokens":810882,"completion_tokens":354469}}` |
| 24 | got := ParseContextLimitError(&APIError{Status: 400, Body: body}) |
| 25 | if got == nil || got.WindowTokens != 1_048_576 || got.PromptTokens != 810_882 || got.CompletionTokens != 354_469 { |
| 26 | t.Fatalf("numeric json = %+#v", got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestParseContextLimitErrorGLMUnnumbered1261(t *testing.T) { |
| 31 | // Zhipu GLM reports a bare overflow with no token numbers. It must be |
| 32 | // trusted as a context-limit error with an unknown window rather than |
| 33 | // failing the same oversized request on every retry. |
| 34 | body := `{"error":{"code":"1261","message":"Prompt exceeds max length"}}` |
| 35 | got := ParseContextLimitError(&APIError{Status: 400, Body: body}) |
| 36 | if got == nil { |
| 37 | t.Fatal("GLM 1261 must be trusted as a context-limit error") |
| 38 | } |
| 39 | if got.WindowTokens != 0 || got.RequestedTokens != 0 || got.PromptTokens != 0 || got.CompletionTokens != 0 { |
| 40 | t.Fatalf("unnumbered overflow must carry zero token fields, got %+v", got) |
| 41 | } |
| 42 | if errors.Unwrap(got) == nil { |
| 43 | t.Fatal("Unwrap must return the original APIError") |
| 44 | } |
| 45 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"message":"prompt exceeds max length"}}`}) == nil { |
| 46 | t.Fatal("case-insensitive message variant must be trusted") |
| 47 | } |
| 48 | if ParseContextLimitError(&APIError{Status: 401, Body: `{"error":{"code":"1261","message":"Prompt exceeds max length"}}`}) != nil { |
| 49 | t.Fatal("401 must not be treated as a context limit") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestParseContextLimitErrorRejectsMalformedAndNonContext(t *testing.T) { |
| 54 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"message":"unpaired tool_calls"}}`}) != nil { |
| 55 | t.Fatal("non-context 400 must stay unparsed") |
| 56 | } |
| 57 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"context_length":-1,"prompt_tokens":10,"completion_tokens":10}}`}) != nil { |
| 58 | t.Fatal("negative numbers must be rejected") |
| 59 | } |
| 60 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"context_length":100,"prompt_tokens":10,"completion_tokens":10}}`}) != nil { |
| 61 | t.Fatal("prompt+completion <= window must be rejected") |
| 62 | } |
| 63 | if ParseContextLimitError(&APIError{Status: 401, Body: `This model's maximum context length is 1048576 tokens. However, you requested 1165351 tokens (810882 in the messages, 354469 in the completion).`}) != nil { |
| 64 | t.Fatal("401 must not be treated as a context limit") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestParseContextLimitErrorAccepts413And422(t *testing.T) { |
| 69 | text := "This model's maximum context length is 1048576 tokens. However, you requested 1165351 tokens (810882 in the messages, 354469 in the completion)." |
| 70 | if ParseContextLimitError(&APIError{Status: http.StatusRequestEntityTooLarge, Body: text}) == nil { |
| 71 | t.Fatal("413 should parse") |
| 72 | } |
| 73 | if ParseContextLimitError(&APIError{Status: http.StatusUnprocessableEntity, Body: text}) == nil { |
| 74 | t.Fatal("422 should parse") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestParseOutputLimitError(t *testing.T) { |
| 79 | err := ParseOutputLimitError(&APIError{Status: 400, Body: `bad request: max_tokens is too large: 384000. This model supports at most 131072 completion tokens.`}) |
| 80 | if err == nil || err.RequestedTokens != 384000 || err.MaxOutputTokens != 131072 { |
| 81 | t.Fatalf("ParseOutputLimitError = %+v, want requested 384000 max 131072", err) |
| 82 | } |
| 83 | if ParseOutputLimitError(&APIError{Status: 401, Body: `max_tokens is too large: 384000; supports at most 131072`}) != nil { |
| 84 | t.Fatal("authorization errors must not be treated as output-limit errors") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | const inputLengthOverflowBody = `{"error":{"code":"InvalidParameter","message":"Input length 1048826 exceeds the maximum length 1048566. Request id: 021788256632854add9fca5277b8939d33af9a0f1789d6cf09b11","param":"","type":"BadRequest"}}` |
| 89 | |
| 90 | func TestSendWithRetryInputLengthOverflowIsContextLimit(t *testing.T) { |
| 91 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 92 | w.WriteHeader(http.StatusBadRequest) |
| 93 | _, _ = w.Write([]byte(inputLengthOverflowBody)) |
| 94 | })) |
| 95 | defer server.Close() |
| 96 | _, err := SendWithRetry(context.Background(), server.Client(), SendOptions{Provider: "fixture"}, func(ctx context.Context) (*http.Request, error) { |
| 97 | return http.NewRequestWithContext(ctx, http.MethodPost, server.URL, nil) |
| 98 | }) |
| 99 | got := AsContextLimitError(err) |
| 100 | if got == nil { |
| 101 | t.Fatalf("input-length overflow reached the caller as %T %v, want a context-limit error", err, err) |
| 102 | } |
| 103 | if got.WindowTokens != 1_048_566 || got.PromptTokens != 1_048_826 || got.RequestedTokens != 1_048_826 || got.CompletionTokens != 0 { |
| 104 | t.Fatalf("input-length overflow = %+v", got) |
| 105 | } |
| 106 | if errors.Unwrap(got) == nil { |
| 107 | t.Fatal("Unwrap must return the original APIError") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestParseContextLimitErrorInputLengthNeedsOverflow(t *testing.T) { |
| 112 | for _, body := range []string{ |
| 113 | `{"error":{"code":"InvalidParameter","message":"Input length 1048566 exceeds the maximum length 1048566."}}`, |
| 114 | `{"error":{"code":"InvalidParameter","message":"Input length 1000 exceeds the maximum length 1048566."}}`, |
| 115 | `{"error":{"code":"InvalidParameter","message":"Input length 0 exceeds the maximum length 1048566."}}`, |
| 116 | } { |
| 117 | if got := ParseContextLimitError(&APIError{Status: 400, Body: body}); got != nil { |
| 118 | t.Fatalf("input within the limit parsed as overflow: %s -> %+v", body, got) |
| 119 | } |
| 120 | } |
| 121 | if ParseContextLimitError(&APIError{Status: 401, Body: inputLengthOverflowBody}) != nil { |
| 122 | t.Fatal("401 must not be treated as a context limit") |
| 123 | } |
| 124 | } |
| 125 |