Files
magpie/internal/gateway/stop_context_test.go
T
Jeremy Levartovsky 48071e9ab9 gateway: a Claude reply cut off by the context window says it was cut off (#88)
Claude 4.5 and later stop with model_context_window_exceeded when the
reply runs into the context window before max_tokens. It was read as a
normal end, so a Chat Completions client got finish_reason "stop" and a
Responses client status "completed" for a truncated reply; it now
reads as length, as max_tokens does.
2026-09-26 18:59:26 +08:00

40 lines
1.3 KiB
Go

package gateway
import (
"encoding/json"
"testing"
)
// Claude 4.5 and later stop with model_context_window_exceeded when the
// reply runs into the context window: the reply is cut short, as with
// max_tokens, and a Chat Completions or Responses client must be told so.
func TestContextWindowStopIsTruncation(t *testing.T) {
var col collector
for _, c := range []string{
`{"type":"message_start","message":{"id":"msg_1","model":"claude-sonnet-4-5","usage":{"input_tokens":10}}}`,
`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Half an ans"}}`,
`{"type":"content_block_stop","index":0}`,
`{"type":"message_delta","delta":{"stop_reason":"model_context_window_exceeded"},"usage":{"output_tokens":5}}`,
} {
decodeAnthropic(c, col.add)
}
res := col.finish()
var chat struct {
Choices []struct {
FinishReason string `json:"finish_reason"`
} `json:"choices"`
}
json.Unmarshal(renderChat(res, "m"), &chat)
if got := chat.Choices[0].FinishReason; got != "length" {
t.Errorf("chat finish_reason = %q, want length", got)
}
var resp struct {
Status string `json:"status"`
}
json.Unmarshal(renderResponses(res, "m"), &resp)
if resp.Status != "incomplete" {
t.Errorf("responses status = %q, want incomplete", resp.Status)
}
}