| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestParseContextLimitErrorJSONAndEnglish(t *testing.T) { |
| 10 | 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)."}}` |
| 11 | got := ParseContextLimitError(&APIError{Status: 400, Body: body}) |
| 12 | if got == nil || got.WindowTokens != 1_048_576 || got.PromptTokens != 810_882 || got.CompletionTokens != 354_469 || got.RequestedTokens != 1_165_351 { |
| 13 | t.Fatalf("english/json parse = %+v", got) |
| 14 | } |
| 15 | if errors.Unwrap(got) == nil { |
| 16 | t.Fatal("Unwrap must return the original APIError") |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestParseContextLimitErrorNumericJSON(t *testing.T) { |
| 21 | body := `{"error":{"context_length":1048576,"requested_tokens":1165351,"prompt_tokens":810882,"completion_tokens":354469}}` |
| 22 | got := ParseContextLimitError(&APIError{Status: 400, Body: body}) |
| 23 | if got == nil || got.WindowTokens != 1_048_576 || got.PromptTokens != 810_882 || got.CompletionTokens != 354_469 { |
| 24 | t.Fatalf("numeric json = %+#v", got) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestParseContextLimitErrorGLMUnnumbered1261(t *testing.T) { |
| 29 | // Zhipu GLM reports a bare overflow with no token numbers. It must be |
| 30 | // trusted as a context-limit error with an unknown window rather than |
| 31 | // failing the same oversized request on every retry. |
| 32 | body := `{"error":{"code":"1261","message":"Prompt exceeds max length"}}` |
| 33 | got := ParseContextLimitError(&APIError{Status: 400, Body: body}) |
| 34 | if got == nil { |
| 35 | t.Fatal("GLM 1261 must be trusted as a context-limit error") |
| 36 | } |
| 37 | if got.WindowTokens != 0 || got.RequestedTokens != 0 || got.PromptTokens != 0 || got.CompletionTokens != 0 { |
| 38 | t.Fatalf("unnumbered overflow must carry zero token fields, got %+v", got) |
| 39 | } |
| 40 | if errors.Unwrap(got) == nil { |
| 41 | t.Fatal("Unwrap must return the original APIError") |
| 42 | } |
| 43 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"message":"prompt exceeds max length"}}`}) == nil { |
| 44 | t.Fatal("case-insensitive message variant must be trusted") |
| 45 | } |
| 46 | if ParseContextLimitError(&APIError{Status: 401, Body: `{"error":{"code":"1261","message":"Prompt exceeds max length"}}`}) != nil { |
| 47 | t.Fatal("401 must not be treated as a context limit") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestParseContextLimitErrorRejectsMalformedAndNonContext(t *testing.T) { |
| 52 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"message":"unpaired tool_calls"}}`}) != nil { |
| 53 | t.Fatal("non-context 400 must stay unparsed") |
| 54 | } |
| 55 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"context_length":-1,"prompt_tokens":10,"completion_tokens":10}}`}) != nil { |
| 56 | t.Fatal("negative numbers must be rejected") |
| 57 | } |
| 58 | if ParseContextLimitError(&APIError{Status: 400, Body: `{"error":{"context_length":100,"prompt_tokens":10,"completion_tokens":10}}`}) != nil { |
| 59 | t.Fatal("prompt+completion <= window must be rejected") |
| 60 | } |
| 61 | 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 { |
| 62 | t.Fatal("401 must not be treated as a context limit") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestParseContextLimitErrorAccepts413And422(t *testing.T) { |
| 67 | text := "This model's maximum context length is 1048576 tokens. However, you requested 1165351 tokens (810882 in the messages, 354469 in the completion)." |
| 68 | if ParseContextLimitError(&APIError{Status: http.StatusRequestEntityTooLarge, Body: text}) == nil { |
| 69 | t.Fatal("413 should parse") |
| 70 | } |
| 71 | if ParseContextLimitError(&APIError{Status: http.StatusUnprocessableEntity, Body: text}) == nil { |
| 72 | t.Fatal("422 should parse") |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestParseOutputLimitError(t *testing.T) { |
| 77 | err := ParseOutputLimitError(&APIError{Status: 400, Body: `bad request: max_tokens is too large: 384000. This model supports at most 131072 completion tokens.`}) |
| 78 | if err == nil || err.RequestedTokens != 384000 || err.MaxOutputTokens != 131072 { |
| 79 | t.Fatalf("ParseOutputLimitError = %+v, want requested 384000 max 131072", err) |
| 80 | } |
| 81 | if ParseOutputLimitError(&APIError{Status: 401, Body: `max_tokens is too large: 384000; supports at most 131072`}) != nil { |
| 82 | t.Fatal("authorization errors must not be treated as output-limit errors") |
| 83 | } |
| 84 | } |
| 85 |